2. 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
3. 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. 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. 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
8. 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
Structured
- struct
- array
- union
- class
Address
- reference
- pointers
9. 9
C++ Data Types
Structured
array struct union class
Address
pointer reference
Simple
Integral Floating
char short int long enum
float double long double
10. 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. 11
Floating Point Types
Represent rational numbers
float, double, long double
Stored in the computer in scientific notation
+3.94x10-3
Leading
sign Significant
digits
Sign of
power of ten
Power of ten
12. 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. 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. 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
16. 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 : 3
Old value
of 0 lost
17. 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
19. 19
Using Named Constants
Characteristics
a location in memory
referenced by an identifier
value cannot be changed
Example
const int lines_per_page = 66;
lines_per_page = 123;
ERROR
Cannot alter a
constant
20. 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
Functions:
begin with
upper case,
cap
successive
words
Named
Constants:
all caps, use
underscore
between words
22. 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
23. 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. 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. 25
Program Comments
Purpose
for the human reader
the compiler ignores
Your programs
should contain
info as shown:
/* comments between */
// Comments follow
26. 26
Program Construction
Shown is a typical program with one
function, the main( ) function
Variable
declaration
Assignment
statement
Output statement
Return for int
function main( )
27. 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. 28
The C++ Preprocessor
#include statements tell the preprocessor
where to get certain declarations
Preprocessor runs before the compiler
All preprocessor commands preceded by #
29. 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. 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 ;