- A set of rules that defines how valid instructions are written in a
programming language
- No ambiguity is allowed in the syntax of a programming language
- They are used to describe the syntax rules of a programming language,
that is, the way valid instructions should be written
|
- Comments
* Words which appear in a box indicate that they are optional
* The three dots indicate that the preceding symbol can be repeated
* Words in boldface type must always be present
- Surprise: main() is also a function !!!
- Who calls main() ? the operating system
|
- Comments
* The operating system expects main to return a value when main
finishes executing
* By convention, we return the value 0 meaning that everything went OK
(we can choose to return other values like 1,2 etc. to inform the operating
system that something went wrong)
* If we choose to return a value to the operating system then we must
use put the word int before the word main
- Programs are made up of functions (one of them has to be main)
|
- Example
//****************************************************************** // Sample program // This program computes the square and cube of 27 //****************************************************************** #include <iostream.h> int Square( int ); int Cube( int ); int main() { cout << "The square of 27 is " << Square(27) << endl; cout << "and the cube of 27 is " << Cube(27) << endl; return 0; } int Square( int n ) { return n * n; } int Cube( int n ) { return n * n * n; }
- Names that we use to refer to things (e.g., variables, constants, functions etc.) in our programs
- Names are meaningless to the computer; it is recommended that you choose names that will be meaningful to a person reading the program
- Identifiers are composed of
* Letters (A-Z, a-z)
* Digits (0-9)
* Underscore (_)
- Identifiers must begin with a letter or underscore (not with a digit !!)
|
- The capital and small letters are different
- Examples of valid identifiers
x, sum_of_squares, J9, box22A, GetData, count, COUNT
- Examples of invalid identifiers
40Hours (starts with a digit)
Get Data (includes a blank)
box-22 (includes the hyphen -)
const_in_$ (includes the character $)
- Identifiers that have been reserved for special purpose
- Examples of reserved words (see also Appendix A)
int, if, const, else, float, friend, return
- The data type determines how the data is represented in the computer and the kinds of processing that the computer can perform on it
- Basic data types: int, float, char
- int
* Used to represent integer values (e.g., 22, 16, 0, -4600)
* The computer usually allocates 16 bits (2 bytes) for integers which
means that the range of values is [-32768, 32767]
* Variations of int: short int (8 bits (1 byte) - range of
values is [-128, 127]) and long int (32 bits (4 bytes) range of values is
[-2147483648, 2147483647])
* Integer overflow occurs when we try to compute values outside the
alowable range of values
br
* Sizes (i.e., number of bits) are machine dependent
- float
* Used to represent real numbers (e.g., 18.0, 127.54, 4., .8)
* Variations of float(single precision - 4 bytes and 6 decimal
digits): double (double precision - 8 bytes and 16 decimal digits),
long double (even greater precision)
* Real numbers can also be expressed in scientific notation: mEx = m 10 ^ x
(e.g., 3.504E12 = 3.504 10 ^ 12, -2.5E-4 = -2.5 10 ^ -4)
- char
* Used to represent alphanumeric symbols (letters, digits, special
symbols)
* examples: 'A', 'a', '8', '+' '$', '*', ' '
* characters need to be enclosed in single quotes so that the compiler can
differentiate between the character data '8' and the integer value 8
* 8 bits (1 byte) are used to represent characters
NullStatement
|
- It is just a semicolon (;) and it does absolutely nothing at execution time
- It is a statement that associates an identifier with a memory location and the contents of this memory location
- In one of our previous examples, we used the statement int x, y, sum; to declare x, y, and sum as variables whose contents are of type int
- Variables: An identifier associated with a memory location whose contents can change
|
- Constant: An identifier associated with a memory location whose contents can not change
- The keyword const must be used to declare a constant
- Examples
const float PI=3.14159;
const int MAX=12;
const char star='*';
const DataType Identifier = LiteralValue ;
|
- It stores the value of an expression into a variable
Variable = Expression ; |
- Assuming the following declarations
int x,i,j,num;
float y,w;
char c;
x=10; num=5+x; y=1.2-w; c='A'; i=j+2; are valid assignments
- Expressions are made up of constants, variables, and operators
- Operators: +, -, *, /, % (modulus - remainder from integer division)
x+y, alpha+2, rate-6.0, alpha/num+2 5%2 are valid expressions
- Warning: integer versus float division
6/2=3, 7/2=3, 1/2=0 but 7.0/2.0=3.5, 1.0/2.0=0.5
<Table from page 66>
- Mixing data types in the same expression
* The compiler applies certain rules for converting operands from one type
to another
* Rule: convert lower type to higher type
* Hierarchy: long double, double, float, long int, int, short int
5.0+10 --> 5.0+10.0 --> 15.0
- Important: the left-hand side of an assignment must be a variable
5=num+2; 'A'=c; j+2=i; i*j=num; are invalid assignments !!
- Note: the symbol '=' means here "store a value" (not equal)
i=i+2 is a meaningful assignment !!!
- Switching values between two variables
- Mixing types in the same assignment statement
* The value of the right-hand side is converted to the data type of the
variable in the left-hand side
* Using the above declarations
i=2.2; will result i=2 (truncation), y=x+8; will result y=18.0