Professional Documents
Culture Documents
Control Statement
Control Statement
www.ustudy.in
www.ustudy.in
www.ustudy.in
If
If else
Else if
Nested if
switch
www.ustudy.in
www.ustudy.in
www.ustudy.in
www.ustudy.in
Example
A very simple program:
#include <stdio.h>
int main ( )
{
int a = 1, b = 2, c ;
if (a > b) c = a;
else c = b;
}
www.ustudy.in
no semi-colon!
www.ustudy.in
Example
Often, the earlier example is written this way:
#include <stdio.h>
int main ( )
{
int a = 1, b = 2, c ;
if (a > b)
c=a;
else
c=b;
}
www.ustudy.in
www.ustudy.in
else if syntax
The actual syntax for the multiple if / else if / else selection
structure is as follows:
if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else if ( this logical expression is true )
statement ;
else
statement ;
www.ustudy.in
www.ustudy.in
www.ustudy.in
www.ustudy.in
Nested if statement
The if statement may itself contain another if statement is
known as nested if statement.
Syntax:
if (condition1)
if (condition2)
statement-1;
else
statement-2;
else
statement-3;
www.ustudy.in
switch
Useful when a variable or expression is tested for all the values it can
assume and different actions are taken.
Format
Series of case labels and an optional default case
switch ( value ){
case '1':
actions
case '2':
actions
default:
actions
case a
true
case a action(s)
break
case b action(s)
break
case z action(s)
break
false
case b
true
false
.
.
.
case z
true
false
default action(s)
www.ustudy.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1. Initialize variables
2. Input data
);
33
34
35
++fCount;
36
break;
37
38
39
break;
3. Print results
40
41
default:
42
43
44
break;
45
46
}
}
47
48
49
50
51
52
53
54
55
56 }
return 0;
Program Output
Iterative(looping) Statements
It allow a set of instructions to be executed or performed several times
until certain conditions are met.
www.ustudy.in
No
semicol
on after
last
expressi
on
2
3
4
5
int main()
8
9
10
sum += number;
11
12
13
14
return 0;
15 }
Sum is 2550
Program Output
action(s)
true
condition
false
www.ustudy.in
www.ustudy.in
2
3
1. Initialize variable
4
5
int main()
2. Loop
int counter = 1;
3. Print
8
9
do {
10
printf( "%d
11
", counter );
12
13
return 0;
14 }
10
Program Output
Branching statements
1.break statement
2.continue statement
3.goto statement
www.ustudy.in
www.ustudy.in
for structure
Increment expression is executed, then the loop-continuation test is
evaluated
www.ustudy.in
Syntax
statement: labeled-statement
jump-statement
jump-statement: goto identifier ;
labeled-statement: identifier : statement
www.ustudy.in
2
3
4
5
int main()
1. Initialize variable
int x;
2. Loop
8
9
10
11
3. Print
if ( x == 5 )
12
continue;
13
14
15
16
17
18
19
return 0;
20 }
1 2 3 4 6 7 8 9 10
Used continue to skip printing the value 5
Program Output