3.control Statements
3.control Statements
07/07/2024 1
Control Statements
• Enable to specify the flow of program control; i.e. the
order in which the instructions in a program must be
executed.
• Flow of control can be of basic three types:
• Sequential
• Selection
• Repetition
07/07/2024 2
Control Statements…
• Sequential - Default mode. Sequential execution of
code statements. i.e. One line after another.
Example
07/07/2024 3
Control Statements…
• Selection - Used for decisions making, branching. i.e.
choosing between two or more alternative paths.
• if
• if…else
• switch
if(expression) {
//statement(s)
}
07/07/2024 5
‘if’ Statement…
Example
07/07/2024 7
‘if…else’ Statement…
if(expression) {
//statement(s)
}
else{
//statement(s)
}
07/07/2024 8
‘if…else’ Statement…
Example
07/07/2024 15
‘if…else if…else’ Statement…
Example
Output: Value of a is 20
07/07/2024 Exact value of a is: 20 16
‘if…else if…else’ Statement…
Example
07/07/2024 18
Nested ‘if’ Statement…
Syntax
if(expression) {
//statement(s)
if(expression) {
//statement(s)
}
}
07/07/2024 19
Nested ‘if’ Statement…
Note: You can nest else if...else in the similar way as if
statements.
07/07/2024 20
Nested ‘if’ Statement…
Example
Output: y = 6400
07/07/2024 21
Nested ‘if’ Statement…
Example
Output:
Greater than -50
07/07/2024 22
‘switch’ Statement
• switch case statements are a substitute for long if…else
if statements that compare a variable to several integral
values.
• The switch statement is a multi-way branch statement. It
provides an easy way to dispatch execution to different
parts of code based on the value of the expression.
• switch is a control statement that allows a value to
change control of execution.
07/07/2024 23
‘switch’ Statement…
Syntax Flow Diagram
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
……….
case p: // code to be executed if n = p;
default: // code to be executed if n
// doesn't match any cases
//default is optional
}
07/07/2024 24
‘switch’ Statement…
Example
Output:
n = 311
07/07/2024 25
‘switch’ Statement…
Notes:
• The expression used in switch must be integral type ( int, char and
enum). Any other type of expression is not allowed.
Example
Output:
07/07/2024 26
‘switch’ Statement…
Notes:
• The expression used in switch must be integral type ( int, char and
enum). Any other type of expression is not allowed.
Example
Output:
p = 599
07/07/2024 27
‘switch’ Statement…
Notes:
• All the statements following a matching case execute until a
break statement is reached.
Example
Output:
m = 300
n = 311
07/07/2024 28
‘switch’ Statement…
Notes:
• The default block can be placed anywhere. The position of default
doesn’t matter, it is still executed if no match found.
Example
Output:
No cases matched
07/07/2024 29
‘switch’ Statement…
Notes:
• The default statement is optional. Even if the switch case statement
do not have a default statement, it would run without any problem.
Example
Output:
07/07/2024 30
‘switch’ Statement…
Notes:
• The statements written above cases are never executed. After the switch statement,
the control transfers to the matching case, the statements written before case are not
executed.
Example
Output:
n = 311
07/07/2024 31
‘switch’ Statement…
Notes:
• Two case labels cannot have same value.
Example
Output:
07/07/2024 32
‘switch’ Statement…
Notes:
• Any number of case statements can be used within a
switch.
07/07/2024 33
Nested ‘switch’ Statement…
• It is possible to have a switch as a part of the statement
sequence of an outer switch.
07/07/2024 34
Nested ‘switch’ Statement…
Syntax
switch(x1) {
case 1:
printf("This case 1 is part of outer switch" );
switch(x2) {
case 1:
printf("This case 1 is part of inner switch" );
break;
case 2: /* case code */
}
break;
case 2: /* case code */
}
07/07/2024 35
Nested ‘switch’ Statement…
Example
07/07/2024 36
Nested ‘switch’ Statement…
Output
07/07/2024 37
THANKS
07/07/2024 38