c++ syntax and semantics

30
1 C++ Syntax and Semantics The Development Process

Upload: deepak

Post on 04-Jan-2016

86 views

Category:

Documents


2 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: C++ Syntax and Semantics

1

C++ Syntax and Semantics

The Development Process

Page 2: C++ Syntax and Semantics

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

Page 3: C++ Syntax and Semantics

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

Page 4: C++ Syntax and Semantics

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

Page 5: C++ Syntax and Semantics

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

Page 6: C++ Syntax and Semantics

6

Style Issues

Use meaningful identifiers makes the program more self documenting

Use readable identifiers

Page 7: C++ Syntax and Semantics

7

Identifiers Check whether legal and/or readable

Identifier legal?

readable?

r2d2

electric Bill

23_skidoo

Total_Amount

Page 8: C++ Syntax and Semantics

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

Page 9: C++ Syntax and Semantics

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

Page 10: C++ Syntax and Semantics

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

Page 11: C++ Syntax and Semantics

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

Page 12: C++ Syntax and Semantics

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

Page 13: C++ Syntax and Semantics

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

Page 14: C++ Syntax and Semantics

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

Page 15: C++ Syntax and Semantics

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

Page 16: C++ Syntax and Semantics

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

Page 17: C++ Syntax and Semantics

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

Page 18: C++ Syntax and Semantics

18

Using Named Constants

Characteristics a location in memory referenced by an identifier value cannot be changed

Exampleconst int lines_per_page = 66;

Page 19: C++ Syntax and Semantics

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

Page 20: C++ Syntax and Semantics

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

Page 21: C++ Syntax and Semantics

21

Executable Statements

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

Example:

Page 22: C++ Syntax and Semantics

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

Page 23: C++ Syntax and Semantics

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 ( << )

Page 24: C++ Syntax and Semantics

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

Page 25: C++ Syntax and Semantics

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

Page 26: C++ Syntax and Semantics

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( )

Page 27: C++ Syntax and Semantics

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 { }

Page 28: C++ Syntax and Semantics

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

Page 29: C++ Syntax and Semantics

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

Page 30: C++ Syntax and Semantics

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 ;