340 likes | 600 Views
C++ Syntax and Semantics. The Development Process. To be able to create identifiers. To declare variables and constants. To write assignment and I/O statements. To design and write simple programs. Why Study This Chapter?. C++ Program Structure. All sub programs are called functions
E N D
C++ Syntax and Semantics The Development Process
To be able to create identifiers To declare variables andconstants To writeassignment andI/O statements To design andwrite simple programs Why Study This Chapter?
C++ Program Structure • All sub programs are called functions • Every program has a function called main( ) • Other modules (sub programs or functions) are declared by the programmer
Syntax & Semantics • Syntax <=> The formal rules governing how valid instructions are written in a programming language • Semantics <=> the set of rules determining the meaning of instructions written in the programming language
Naming Identifiers • Identifiers are used to name things • Made up of • letters (upper, lower case) • numerals (0-9) • under score _ • Must begin with letter or underscore
Style Issues • Use meaningful identifiers • makes the program more self documenting • Use readable identifiers
Identifiers • Check whether legal and/or readable
Data and Data Types • Distinction made between integers, characters, rationals, etc. • Data type <=> specific set of data values along with a set of operations on those values Structured- struct- array- union- class Address- reference- pointers Simple- integers- float- char
Address pointer reference C++ Data Types Simple Structured Integral Floating array struct union class char short int long enum float double long double
Integer Types • Whole numbers with no fractional part 23 876 -915 • No commas allowed • Other variations of integer • short, long • different sizes available (for memory use) • Also char‘a’ ‘Z’ ‘8’ ‘%’ ‘$’
Power of ten Leading sign Significantdigits Sign ofpower of ten Floating Point Types • Represent rational numbers • float, double, long double • Stored in the computer in scientific notation +3.94x10-3
Scientific Notation 2.7E4 means 2.7 x 10 4 = 2.7000 = 27000.0 2.7E-4 means 2.7 x 10 - 4 = 0002.7 = 0.00027
Declarations • Statement in a program that associates a name (an identifier) with a memory location • Use the name to access or alter the contents of the memory location
Value for y initialized at declaration Unknown or “garbage” value for x Variables • Characteristics • a location in memory • referenced by an identifier • contents of the location can be changed • Example: • int x, y = 0; x : ? y : 0
Variables • Characteristics • a location in memory • referenced by an identifier • contents of the location can be changed • Example: • int x, y = 0;x = 5; x : 5 y : 0
Old value of 0 lost Variables • Characteristics • a location in memory • referenced by an identifier • contents of the location can be changed • Example: • int x, y = 0;x = 5;y = 3; x : 5 y : 3
Value stored in y accessed, added to 7, result stored in x Variables • Characteristics • a location in memory • referenced by an identifier • contents of the location can be changed • Example: • int x, y = 0;x = 5;y = 3;x = y + 7; x : 10 y : 3
Using Named Constants • Characteristics • a location in memory • referenced by an identifier • value cannot be changed • Exampleconst int lines_per_page = 66;
Using Named Constants • Characteristics • a location in memory • referenced by an identifier • value cannot be changed • Exampleconst int lines_per_page = 66;lines_per_page = 123; ERROR Cannot alter a constant
Style : Capitalization of Identifiers • Used as a visual clue to what an identifier represents • Standard for our text Functions:begin with upper case, cap successive words Named Constants:all caps, use underscore between words Variables:begin with lower case, cap successive words
Executable Statements • Output : send results of calculations, etc. to screen, printer, or file • Example:
Only a single cout statement used. Repeat use of << operator Executable Statements • Output : send results of calculations, etc. to screen, printer, or file • Alternate Example:
Insertion Operator ( << ) • Variable cout is predefined to denote an output stream that goes to the standard output device (display screen). • The insertion operator << called “put to” takes 2 operands. • The left operand is a stream expression, such as cout. The right operand is an expression of simple type or a string constant.
Output Statements SYNTAX These examples yield the same output. cout << “The answer is “ ; cout << 3 * 4 ; cout << “The answer is “ << 3 * 4 ; cout << ExprOrString << ExprOrString . . . ;
/* comments between */ // Comments follow Program Comments • Purpose • for the human reader • the compiler ignores • Your programs should contain info as shown:
Variable declaration Assignment statement Return for int function main( ) Output statement Program Construction • Shown is a typical program with one function, the main( ) function
Compound Statements • Body of a function is an example of a block or compound statement • They are enclosed between a pair of curly brackets { }
The C++ Preprocessor • #include statements tell the preprocessor where to get certain declarations • Preprocessor runs before the compiler • All preprocessor commands preceded by # sign
Program Entry and Execution • Source code is a text file created by text editor • Program is compiled • Compiler notifies you of syntax (and some semantic) errors • Program is linked • Code from #include’s is linked to your compiled code • Program is then run. You must check for remaining semantic, logic errors
Testing and Debugging Hints • Watch for misspelled or undeclared identifiers • C++ is case sensitive, watch for improper Caps • Watch for integer divisionint_x / int_y yields an integer result • Don’t confuse 0’s (zeros) with O’s in your source code • Make sure statements end with semicolons ;