1 / 30

C++ Syntax and Semantics

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

deepak
Télécharger la présentation

C++ Syntax and Semantics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Syntax and Semantics The Development Process

  2. To be able to create identifiers To declare variables andconstants To writeassignment andI/O statements To design andwrite simple programs Why Study This Chapter?

  3. 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

  4. 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

  5. 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

  6. Style Issues • Use meaningful identifiers • makes the program more self documenting • Use readable identifiers

  7. Identifiers • Check whether legal and/or readable

  8. 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

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

  10. 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’ ‘%’ ‘$’

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Using Named Constants • Characteristics • a location in memory • referenced by an identifier • value cannot be changed • Exampleconst int lines_per_page = 66;

  19. 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

  20. 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

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

  22. 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:

  23. 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.

  24. 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 . . . ;

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

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

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

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

  29. 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

  30. 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 ;

More Related