You are on page 1of 11

# C

+
+ Lecture 2
P
O
O

5. C/C++ statements
Every C/C++ statements has at its end a semicolon ;or a bracket }.
5.1. Empty statement
This statement is used when the presence of a statement or function that does not
execute anything is needed.
. ;
5.2. Expression statement
General syntax:
. Expression;
Examples:
y = ( x+1) *( x- 1) ; / / assi gn: v=Expression;
f ( x, y) ; / / cal l f unct i on: f(p , p p
n
);
1 2. . .
c++; - - c; / / I ncr ement / Decr ement ( Pre/ Post)

5.3. Compound statement (statement block)
This statement uses { and } to group several statements as a unit.

{
Declarations
Statements
}

Example:
{i nt i ; / l ocal var i abl e i n t hi s bl ock; /
f ( i ) ; / / i can be used j ust i n t hi s compound st at ement ;

}

; / / af t er } i s not ;

5.4. The if statement
Is a decision structure with one or two branches having the following syntax:
. if (Expression) statement
1
;
[else statement
2
; ]

As we remember, in any C/C++ program, the value 0 means FALSE, and any other value
means TRUE. Every expression has a value!!!
23.03.06 1
Example:
C
+
+ Lecture 2
P
O
O

{ / / P( x, y) f i r st quar t er ( i f x or y is negative the sign is changed)
f l oat x, y;
pr i nt f ( " ent er t he coor di nat es P( x, y) = " ) ;
scanf ( " %f %f " , &x, &y) ; / / poi nt P
i f ( x>0)
i f ( y<0) y=- y;
el se { x=- x;
i f ( y<0) y=- y;
}
pr i nt f ( " x=%5. 2f , y=%5. 2f \ n" , x, y) ;
}
5.5. The switch statement
This statement can be often used when several options depend on the value of a
single variable or expression. The general structure for switch statements follows:

switch ( <sel ect or >)
{ case <label 1> : <st at ement
1
> [ break; ]
case <label 2> : <st at ement
2
> [ break; ]
...
case <label n>: <st at ement
2
> [ break; ]
[ default : <st at ement s> ]
}
Before considering any examples, several comments are in order. The words
switch, case, break, and default are reserved.
The flow of logic within a switch statement is as follows:
1. The value of the selector is determined;
2. The first instance of value is found among the labels;
3. The statements following this value are executed.
4. break statements are optional; if a break statement occurs within these statements,
then the control is transferred to the first program statement following the entire
switch statement; otherwise, execution continues. In general, even though they are
optional, you should end every case with a break statement anyway.
The selector can have only ordinal data types.
EXAMPLES:
whi l e ( scanf ( " %d %c %d" , &o1, &o, &o2) ! =EOF) / / O1 o O2
{ switch(o&0XFF)
{ case '+' : v=o1+o2; break;
case '-' : v=o1-o2; break;
case '*' : v=o1*o2; break;
case '/' : v=o1/o2; break;
default : printf ("unknown operand!%c\n",o);
}
pr i nt f ( " %d%c%d = %d\ n" , o1, o, o2, v) ;
} / / Ct r l / Z
/ / number of days / mont hs \ \
#i ncl ude <i ost r eam. h>
#i ncl ude <coni o. h>
voi d mai n (voi d)
{ i nt m, Y;
cout <<" year: "; ci n >>y;
23.03.06 2
cout <<" number of days : " <<endl ;
C
+
+ Lecture 2
P
O
O

f or (m=1; m<=12; m++) {
cout <<m<<" : ";
switch (m) {
case 2 : cout << (y&3 ? 28 : 29); break;
case 4 :
case 6 :
case 9 :
case 11 : cout << 30 ; break;
default : cout << 31 ; // break;
}
cout <<endl ;
}
getch();
}

5.6. The while statement (while loops)
A useful pre-test loop with variable conditions in C/C++ is the while loop. The
condition controlling the loop is a C/C++ expression written between parentheses. Correct
form and syntax for such a loop are:
while (<expression>)
<statement>; / / body
Program control, when using a while loop, is in order as follows:
1. The loop expression is examined;
2. If the loop expression value is not 0 (this means true), the entire body
(i.e.<statement>) is executed before another check is made.
3. If the expression is 0 (this means false), control is transferred to the first statement
following the loop.
Example:
/ / Example while
#i ncl ude <st di o. h>;
i nt i , n; l ong f ; / / f : =n!
voi d mai n ( voi d)
{ pr i nt f ( " \ n ent er n : " ) ; scanf ( " %d" , &n) ;
i =( f =1) +1;
whi l e ( i <=n) f *=i ++; pr i nt f ( " %d! = %u \ n" , n, f ) ;
i =( f =1) ;
whi l e ( i < n) f *=++i ; pr i nt f ( " %d! = %u \ n" , n, f ) ;
scanf ( " \ n" ) ;
}

5.7. The dowhile statement (dowhile loops)
C/C++ language provides a second form of variable repetition, a dowhile loop, which is
a post-test, or exit-controlled, loop. Its basic syntax is:
do <statement>;
while (expression);
Program control, when using a dowhile loop, is in order as follows:
1. The expression must have a value before it is used at the end of the loop;
23.03.06 3
2. The loop must be entered at least once because the expression is not evaluated until
after the loop body has been executed;
C
+
+ Lecture 2
P
O
O

3. When the expression is evaluated, if its true, control is transferred back to the top
of the loop; if it is false, control is transferred to the next program statement.

Examples:
do
{ = %b; a=b; b=r ; } r a
while (r ); / / b! =0 ! ! !
gcm=a;
i f ( b) / / or
do r =a%b, a=b, b=r ;
while (b);
gcm=a;

5.8. The for statement (The for loop)
The typical form of a for loop is:

for ( <expr essi on >; <expr essi on
2
>; <expr essi on
3
>)
1
<st at ement >;

The internal logic of a for loop that is as follows:
1. The first expression (i.e. expression
1
) is evaluated;
2. The second expression (i.e. expression
2
, named termination condition) is evaluated;
3. If the termination condition is true then
a. the body (i.e. <statement>) of the loop is executed;
b. the third expression is evaluated;
Step 3. is repeated until the termination condition became false
4. If the termination condition is false then the control of the program is transferred to
the first statement following the loop.
The for statement has the same effect with the following structure:
<expression >;
1
while ( <expr essi on
2
>)
{<statement>;
<expression
3
>;
}

Vice versa (see the while statement):
for ( ; <expr essi on>; )
<statement >;

The do while loop (see do while statement) is similar with the following:

for ( ; ; )
23.03.06 4
{<st at ement >;
C
+
+ Lecture 2
P
O
O

if(!<expression>)
break;
}

Example:
i nt i , n; l ong f ; / / f n!
pr i nt f ( " \ n l et s n: " ) ; scanf ( " %d" , &n) ;
f =1;
f or ( i =1; i <=n; i ++) f *=i ; pr i nt f ( " %d! = %u \ n" , n, f ) ;
f =1;
f or ( i =1; i <=n; ++i ) f *=i ; pr i nt f ( " %d! = %u \ n" , n, f ) ;
f =i =1;
f or ( ; i <=n; i +=1) f *=i ; pr i nt f ( " %d! = %u \ n" , n, f ) ;
f or ( f =i =1; i <=n; ) f *=i ++; pr i nt f ( " %d! = %u \ n" , n, f ) ;
5.9. The break statement
This statement is uses to finish a loop structure. It is mainly used to end a
repetitive structure:
break;
Example:
f or ( ; ; )
{ . . .
i f ( expression) break;
. . .
}
5.10. The continue statement
The continue statement transfer the control to the end of loop-body.
This instruction jumps to the end of a repetitive structure (for while or dowhile at
the re-evaluation of the expression and for the for loop at expression
3
) and has the syntax
given below:
continue;
Example:
f or ( . . . ; . . . ; expression
3
)
{ . . .
i f ( expression ) cont i nue;
3
/ / expression
3
wi l l be eval uat ed
. . .
}
5.11. The go to statement
This instruction makes an unconditional jump and without returning to the
instruction of the specified label, having the syntax:
goto label;
Example:
23.03.06 5
while (expr
1
) { ...
C
+
+ Lecture 2
P
O
O

if (expr
2
) goto et;
... }
...
et: ... // statement

6. Function call
Call function is a particular case of the expression instruction:
<function name> (arguments list);
A function can be also called inside an expression of a statement:
<function name (arguments list) ... ;
A function can be used only if it has been defined or at least its prototype has been
declared in one of the following ways:
a) <type> <function name> ( List of formal parameters) ;
b) <type> <function name> ( List of types of formal parameters) ;
c) <type> <function name> ( Void) ; / / t he l i st i s empt y
d) <type> <function name> ( ) ; / / no t ypes ver i f i cat i ons

The implicit call for the simple variables is by value and for arrays is by reference. The
call by reference is obtained using pointer variables and the address operator &.
The return from a function is done using return instruction or is done automatically
after the last instruction of the function (case when no values are returned) being returned
the value of the expression (if exists).
return [expression];
Example 1:
#i ncl ude <gr aphi cs. h> / / some gr aphi cs user f unct i ons
#i ncl ude <mat h. h>
i nt u1, v1, u2, v2;
f l oat a, b, c, d;
i nt u ( f l oat x) {r et ur n ( ( x- a) / ( b- a) *( u2- u1) +u1) ; }
i nt v ( f l oat y) {r et ur n ( ( y- d) / ( c- d) *( v2- v1) +v1) ; }
voi d I ni t Gr af ( voi d)
{i nt Gd = DETECT, Gm; i ni t gr aph ( &Gd, &Gm, "c: \ \ Bc\ \ Bgi ") ; }
voi d Vi ewPor t ( i nt x1, i nt y1, i nt x2, i nt y2)
{u1=x1; v1=y1; u2=x2; v2=y2; } / *r ect angl e ( u1, v1, u2, v2) ; */
voi d Wi ndow ( f l oat x1, f l oat y1, f l oat x2, f l oat
y2) {a=x1; d=y1; b=x2; c=y2; }
voi d Rect angl e ( f l oat x1, f l oat y1, f l oat x2, f l oat y2)
{r ect angl e ( u( x1) , v( y1) , u( x2) , v( y2) ) ; }
voi d Bar ( f l oat x1, f l oat y1, f l oat x2, f l oat y2)
{bar ( u( x1) , v( y1) , u( x2) , v( y2) ) ; }
voi d Li ne ( f l oat x1, f l oat y1, f l oat x2, f l oat y2)
{l i ne ( u( x1) , v( y1) , u( x2) , v( y2) ) ; }
voi d Move ( f l oat x, f l oat y) {movet o ( u( x) , v( y) ) ; }
voi d Dr ag ( f l oat x, f l oat y) {l i net o ( u( x) , v( y) ) ; }
voi d Rot at e ( f l oat &x, f l oat &y, f l oat x0, f l oat y0, f l oat Al f a)
{ f l oat xp;
xp=( x- x0) *cos ( Al f a) - ( y- y0) *si n ( Al f a) +x0;
y =( x- x0) *si n ( Al f a) +( y- y0) *cos ( Al f a) +y0;
x = xp; }
Example 2:
23.03.06 6
/ / l ast nonzero di gi t of n!\ \
C
+
+ Lecture 2
P
O
O

#i ncl ude <i ost r eam. h>;
#i ncl ude <coni o. h>;
i nt Sf (i nt&f , i nt k)
{ i nt p=0;
whi l e (!(f %k)) {f / =k; p++; }
return p;
}
mai n ()
{ cl rscr();
i nt n, f 2=0, Uc=1;
cout <<" n: "; ci n >>n;
f or (i nt i =2; i <=n; i ++)
{ i nt f =i ;
f 2+=Sf (f , 2); f 2- =Sf (f , 5); Uc=Uc*f %10;
}
cout <<" Uc=" <<Uc*((f 2&=3, i nt (f 2 ? f 2*1. 4: 3))<<1)%10;
getch ();
}
Example 3:
/ / Comput es AB, AB \ \
#i ncl ude <i ost r eam. h>;
#i ncl ude <coni o. h>;
i nt Card (i nt A[]) {return A[0]; }
i nt Apart (i nt x, i nt A[])
{f or (i nt i =1; i <=Card(A); i ++)
i f (x==A[i ]) return 1;
return 0;
}
voi d n (i nt A[], i nt B[], i nt C[])
{C[0]=0;
f or (i nt i =1; i <=Card(A); i ++) i f (Apart(A[i ], B)) C[++C[0]]=A[i ];
}
voi d u (i nt A[], i nt B[], i nt C[])
{i nt i ;
f or (i =0; i <=Card(B); i ++) C[i ]=B[i ];
f or (i =1; i <=Card(A); i ++) i f (!Apart(A[i ], B)) C[++C[0]]=A[i ];
}
voi d Ti p (char *Mul t, i nt A[])
{i nt i ; cout <<Mul t <<' {' ;
f or (i =1; i <=Card(A); i ++) cout <<A[i ] <<", "; cout <<"\ b}" <<endl ;
}
voi d mai n (voi d)
{ cl rscr ();
i nt A[]={5, 1, 3, 5, 7, 9};
Ti p (" A: ", A);
i nt AuB[10]; u (A, B, AuB); Ti p (" AuB =", AuB);
i nt AnB[10]; n (A, B, AnB); Ti p (" AnB =", AnB);
getche();
}
6.1. The address operator (&)
This operator (&) can be also used to define a reference type by a declaration of the
form type & (similar with a construction of the form type *, for pointers).With the help of
this operator we can rename a variable, we can make a call by reference or we can declare
a variable by reference in this way:
<type> &formalParameter; // par. ref. (address)

<type> & nameReferanceVariable;
Examples:
#i ncl ude <coni o. h>; / / Redefi ni ng a vari abl e
#i ncl ude <i ost ream. h>;
23.03.06 7
voi d mai n (voi d)
C
+
+ Lecture 2
P
O
O

{i nt a[1]; i nt * b =a; i nt & c =a[0];
cout <<" Enter a: "; ci n >>a[0];
cout <<" a, b, c =" <<a[0] <<*b <<c <<endl ;
i nt x; i nt * y =&x; i nt &z =*y; i nt &w=x;
cout <<" Enter x: "; ci n >>x;
cout <<" x, y, z, w=" <<x <<*y <<z <<w<<endl ;
get ch( ) ;
}

#i ncl ude <coni o. h>; / / Redefi ni ng a funct i on
#i ncl ude <i ost ream. h>;
#i ncl ude <math. h>;
typedef doubl e Functie (doubl e);
voi d mai n (voi d)
{ Functi e & Sin=si n; doubl e x;
cout <<"Enter x: "; ci n >>x;
cout <<"Si n(" <<x <<")=" <<Sin(x) <<endl ;
get ch( ) ;
}

#i ncl ude <coni o. h>; / / Reference cal l
#i ncl ude <i ost ream. h>;
voi d suma (i nt x, i nt y, i nt *z) // x, y z
{ *z =++x
*
++y; }
voi d Suma (i nt x, i nt y, i nt &z) // x, y z
{ z =++x
*
++y; }
voi d mai n (voi d)
{ i nt x, y, z;
cout <<" Enter x, y: "; ci n >>x >>y;
suma (x, y, &z); cout <<"(x+1)*(y+1)=" <<z <<endl ;
Suma (x, y, z); cout <<"(x+1)*(y+1)=" <<z <<endl ; //more simple!
get ch ( ) ;
}

23.03.06 8

C
+
+ Lecture 2
P
O
O

7. Constants declaration
Another way to define a constant is by using the Const modifier::
[ <t ype>] const <name Var> [ = Value];
or const [ <t ype>] <name Var [ = Value] ;
The value of a formal parameter declared with Const modifier cannot be changed
inside a subprogram.
Example 1:
#i ncl ude <st di o. h>
#i ncl ude <coni o. h>;
i nt Di gi t ( const i nt c)
{const char Zero=' 0' ;
i nt const C40H=0x40;
i f ( c<10) r et ur n c | Zero;
el se r et ur n ( c- 9) | C40H;
}
voi d mai n ( voi d)
{const Blue=BLUE;
const White=WHI TE;
const C0F=0xF;
const Esc=0x1B; i nt c;
t ext backgr ound( Blue) ; t ext col or ( White) ;
cl r scr ( ) ;
do {pr i nt f ( " Gi ve a char #Esc : " ) ;
c=get ch ( ) ;
pr i nt f ( " . . . Hexa = %c%c \ n" , Di gi t ( c>>4) , Di gi t ( c&C0F) ) ; }
whi l e ( c! =Esc) ;
}
Example 2:
/ / Pr ogr amEr r or Const
# i ncl ude <i ost r eam. h>
# i ncl ude <coni o. h>
i nt Const ( const i nt i , i nt const j )
{i =1; // Error CONST_.CPP 7: Cannot modify a const object
j =1; // Error CONST_.CPP 8: Cannot modify a const object
r et ur n i +j ;
}
voi d mai n ( )
{cl r scr ( ) ;
const i nt i =10;
i =5; // Error CONST_.CPP 13: Cannot modify a const object
i nt const j =5;
j =5; // Error CONST_.CPP 14: Cannot modify a const object
cout << Const ( 3, 4) ;
get che( ) ;
}
In list of formal parameters
const <Type
*
>t Pf; Constant value (cannot modify its value)

const <Type
*
> const Pf ; Constant address (cannot modify its address)

23.03.06 9
const <Type
*
> const Pf ; Constant value and address
C
+
+ Lecture 2
P
O
O

Example 3:
/ / Pr ogr amExampl e Const
#i ncl ude <i ost r eam. h>
#i ncl ude <coni o. h>

i nt Const ( const i nt * x, i nt * const y, const i nt * const z)
{x[ 0] =y[ 0] ; // Error CONST_.CPP 7: Cannot modify a const object
y=x; // Error CONST_.CPP 8: Cannot modify a const object
z[ 0] =y[ 0] ; // Error CONST_.CPP 9: Cannot modify a const object
z=x; // Error CONST_.CPP 10: Cannot modify a const object
y[ 0] =z[ 0] ; // Correct, y[i] can be modified
x=y; // Correct, x address can be modified
r et ur n x[ 0] +y[ 0] +z[ 0] ;
}
voi d mai n ( )
{ cl r scr ( ) ;
i nt a[ 10] , b[ 10] , c[ 10] ; / /
cout <<Const ( a, b, c) ; / /
get che ( ) ;
}

Example 4: (using Const modifier)

// Const \\
#i ncl ude <coni o. h>
#i ncl ude <i ost r eam. h>
#i ncl ude <i omani p. h>
I ni t (i nt * const v, i nt n, i nt f )
{f or (i nt i =1; i <=n; i ++) v[i ]=i *f ;
return 1;
}
Ti p(const i nt* const v, const i nt n)
{f or (i nt i =1; i <=n; i ++) cout <<setw(3) <<v[i ];
cout <<endl ;
return 1;
}
Copy (i nt * const s, i nt n, i nt * const d)
{f or (i nt i =1; i <=n; i ++) d[i ]=s[i ];
return 1;
}
copy (i nt * const s, i nt n, const i nt * d)
{ cout <<". . . " <<endl ;
Ti p(s, n); Ti p(d, n);
cout <<". . . " <<endl ;
d=s;
Ti p(s, n); Ti p(d, n);
return 1;
}
voi d mai n ()
{ cl rscr ();
i nt x[10], y[10], n=9;
I ni t (x, n, 1); Ti p (x, n);
I ni t (y, n, 2); Ti p (y, n); cout <<endl ;
Copy (x, n, y); Ti p (y, n); cout <<endl ;
I ni t (y, n, 2); Ti p (y, n);
copy (x, n, y); Ti p (y, n); cout <<endl ;
I ni t (x, n, 3);
I ni t (y, n, 4);
Ti p (x, n);
Ti p (y, n); cout <<endl ;
getche();
}

23.03.06 10
Resulls
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
...
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
...
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
C
+
+ Lecture 2
P
O
O

8. Introduction synonym for an existing data type
Assigning a data type a name is realised as follows:
typedef <type> newName;
Example:
typedef int Integer;
typedef float Real;
Integer i , j ;
Real X[ 10] ;
typedef int Sir[100];
typedef float Matr[10][10]
Sir X,Y;
Matr A,B;

9. Contents
pag.
5. C/C++ statements ................................................................................ 1
5.1. Empty statement....................................................................................... 1
5.2. Expression statement................................................................................ 1
5.3. Compound statement (statement block)................................................... 1
5.4. The if statement........................................................................................ 1
5.5. The switch statement................................................................................ 2
5.6. The while statement (while loops)........................................................... 3
5.7. The dowhile statement (dowhile loops) ....................................... 3
5.8. The for statement (The for loop).............................................................. 4
5.9. The break statement ................................................................................. 5
5.10. The continue statement......................................................................... 5
5.11. The go to statement .............................................................................. 5
6. Function call ........................................................................................ 6
6.1. The address operator (&) ......................................................................... 7
7. Constants declaration......................................................................... 9
8. Introduction synonym for an existing data type............................ 11
9. Contents ............................................................................................. 11
23.03.06 11