Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 C++ Syntax and Semantics The Development Process.

Similar presentations


Presentation on theme: "1 C++ Syntax and Semantics The Development Process."— Presentation transcript:

1

2 1 C++ Syntax and Semantics The Development Process

3 2 Why Study This Chapter? To be able to create identifiers To declare variables and constants To write assignment and I/O statements To design and write simple programs

4 3 C++ Program Structure 8All sub programs are called functions 8Every program has a function called main( ) 8Other modules (sub programs or functions) are declared by the programmer

5 4 Syntax & Semantics 8Syntax The formal rules governing how valid instructions are written in a programming language 8Semantics the set of rules determining the meaning of instructions written in the programming language

6 5 Naming Identifiers 8Identifiers are used to name things 8Made up of 8letters (upper, lower case) 8numerals (0-9) 8under score _ 8Must begin with letter or underscore

7 6 Style Issues 8Use meaningful identifiers 8makes the program more self documenting 8Use readable identifiers

8 7 Identifiers 8Check whether legal and/or readable

9 8 Data and Data Types 8Distinction made between integers, characters, rationals, etc. 8Data type specific set of data values along with a set of operations on those values Simple - integers - float - char Structured - struct - array - union - class Address - reference - pointers

10 9 C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double

11 10 Integer Types 8Whole numbers with no fractional part No commas allowed 8Other variations of integer 8short, long 8different sizes available (for memory use) 8Also char ‘a’ ‘Z’ ‘8’ ‘%’ ‘$’

12 11 Floating Point Types 8Represent rational numbers 8float, double, long double 8Stored in the computer in scientific notation +3.94x10 -3 Leading sign Significant digits Sign of power of ten Power of ten

13 12 2.7E4 means 2.7 x 10 4 = = E-4 means 2.7 x = = Scientific Notation

14 13 Declarations 8Statement in a program that associates a name (an identifier) with a memory location 8Use the name to access or alter the contents of the memory location

15 14 Variables 8Characteristics 8a location in memory 8referenced by an identifier 8contents of the location can be changed 8Example: 8int x, y = 0; x : ? y : 0 Unknown or “garbage” value for x Value for y initialized at declaration

16 15 Variables 8Characteristics 8a location in memory 8referenced by an identifier 8contents of the location can be changed 8Example: 8int x, y = 0; x = 5; x : 5 y : 0

17 16 Variables 8Characteristics 8a location in memory 8referenced by an identifier 8contents of the location can be changed 8Example: 8int x, y = 0; x = 5; y = 3; x : 5 y : 3 Old value of 0 lost

18 17 Variables 8Characteristics 8a location in memory 8referenced by an identifier 8contents of the location can be changed 8Example: 8int x, y = 0; x = 5; y = 3; x = y + 7; x : 10 y : 3 Value stored in y accessed, added to 7, result stored in x

19 18 Using Named Constants 8Characteristics 8a location in memory 8referenced by an identifier 8value cannot be changed 8Example const int lines_per_page = 66;

20 19 Using Named Constants 8Characteristics 8a location in memory 8referenced by an identifier 8value cannot be changed 8Example const int lines_per_page = 66; lines_per_page = 123; ERROR  Cannot alter a constant ERROR  Cannot alter a constant

21 20 Style : Capitalization of Identifiers 8Used as a visual clue to what an identifier represents 8Standard for our text Variables: begin with lower case, cap successive words Functions: begin with upper case, cap successive words Named Constants: all caps, use underscore between words

22 21 Executable Statements 8Output : send results of calculations, etc. to screen, printer, or file 8Example:

23 22 Executable Statements 8Output : send results of calculations, etc. to screen, printer, or file 8Alternate Example: Only a single cout statement used. Repeat use of << operator

24 23 8Variable cout is predefined to denote an output stream that goes to the standard output device (display screen). 8The insertion operator << called “put to” takes 2 operands. 8The left operand is a stream expression, such as cout. The right operand is an expression of simple type or a string constant. Insertion Operator ( << )

25 24 SYNTAX These examples yield the same output. cout << “The answer is “ ; cout << 3 * 4 ; cout << “The answer is “ << 3 * 4 ; Output Statements cout << ExprOrString << ExprOrString... ;

26 25 Program Comments 8Purpose 8for the human reader 8the compiler ignores 8Your programs should contain info as shown: /* comments between */ // Comments follow

27 26 Program Construction 8Shown is a typical program with one function, the main( ) function Variable declaration Assignment statement Output statement Return for int function main( )

28 27 Compound Statements 8Body of a function is an example of a block or compound statement 8They are enclosed between a pair of curly brackets { }

29 28 The C++ Preprocessor  #include statements tell the preprocessor where to get certain declarations 8Preprocessor runs before the compiler 8All preprocessor commands preceded by # sign

30 29 Program Entry and Execution 8Source code is a text file created by text editor 8Program is compiled 8Compiler notifies you of syntax (and some semantic) errors 8Program is linked 8Code from #include’s is linked to your compiled code 8Program is then run. You must check for remaining semantic, logic errors

31 30 Testing and Debugging Hints 8Watch for misspelled or undeclared identifiers 8C++ is case sensitive, watch for improper Caps 8Watch for integer division int_x / int_y yields an integer result 8Don’t confuse 0’s (zeros) with O’s in your source code 8Make sure statements end with semicolons ;


Download ppt "1 C++ Syntax and Semantics The Development Process."

Similar presentations


Ads by Google