1 C++ Syntax and Semantics The Development Process.

30
1 C++ Syntax and Semantics The Development Process

Transcript of 1 C++ Syntax and Semantics The Development Process.

1

C++ Syntax and Semantics

The Development Process

2

Why Study This Chapter?

To be able to create identifiers

To declare variables and

constants

To writeassignment andI/O statements

To design andwrite simple programs

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

Identifier legal?

readable?

r2d2

electric Bill

23_skidoo

Total_Amount

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

Simple- integers- float- char

Simple- integers- float- char

Structured- struct- array- union- class

Structured- struct- array- union- class

Address- reference- pointers

Address- reference- pointers

9

C++ Data TypesC++ Data Types

Structured

array struct union class

Address

pointer reference

Simple

Integral Floating

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

Floating Point Types

Represent rational numbers float, double, long double

Stored in the computer in scientific notation

+3.94x10-3

Leading sign

Leading sign

Significantdigits

Significantdigits

Sign ofpower of ten

Sign ofpower of ten

Power of tenPower of ten

12

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

Scientific Notation

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

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

Unknown or “garbage” value for x

Value for y initialized at declaration

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

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 : 3Old value of 0 lost

17

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

Value stored in y accessed, added to 7, result stored in x

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

ERROR Cannot alter a

constant

20

Style : Capitalization of Identifiers

Used as a visual clue to what an identifier represents

Standard for our text

Variables:begin with lower case, cap successive words

Variables:begin with lower case, cap successive words

Functions:begin with upper case, cap successive words

Functions:begin with upper case, cap successive words

Named Constants:all caps, use underscore between words

Named Constants:all caps, use underscore between words

21

Executable Statements

Output : send results of calculations, etc. to screen, printer, or file

Example:

22

Executable Statements

Output : send results of calculations, etc. to screen, printer, or file

Alternate Example:

Only a single cout statement used. Repeat use of << operator

Only a single cout statement used. Repeat use of << operator

23

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.

Insertion Operator ( << )

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

25

Program Comments Purpose

for the human reader the compiler ignores

Your programs should contain info as shown:

/* comments between *//* comments between */

// Comments follow// Comments follow

26

Program Construction

Shown is a typical program with one function, the main( ) function

Variable declarationVariable declaration

Assignment statementAssignment statement

Output statementOutput statementReturn for int function main( )Return for int function main( )

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 division

int_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 ;