Control Statements (Chapter 5)
Control Statements (Chapter 5)
Chapter 5
Control Statements
Introduction
A control statement is a statement that causes program statements to execute in a manner that
doesn’t relate to the order it appears in the source code. These statements help to jump from one
part of the program to another. The control transfer may be conditional or unconditional. C
supports three types of control statements namely selection, repetition and jump statements.
Selection Statements
These are also known as branching, decision making, or conditional statements. These
statements execute program statements depending upon a given condition. C supports two
selection statements: if and switch. There are different forms of if statements: if only, if-else-if,
and if-else-if ladder.
Control Statements
Chapter 5 Page 2 Nawaraj Paudel
Control Statements
Chapter 5 Page 3 Nawaraj Paudel
OR
if (expression)
{
statement 1
statement 2
…
statement n
}
else
{
statement 1
statement 2
…
statement n
}
If expression evaluates to true (any non-zero value), statement or the block of statements in the
if body is executed. If expression evaluates to false (0), statement or the block of statements in
the else body is executed. For example,
if(amount >= 1000)
discount = amount * 0.05;
else
discount = amount * 0.03;
Using if-else, you can specify an action to be performed both when the condition is true and
when it is false.
The if else statement resembles with the conditional operator (?:). Conditional operator is a
ternary operator (demands 3 operands), and is used in certain situations, replacing if-else
statement. Its general form is:
exp1 ? exp2 : exp3;
If exp1 is true, exp2 is executed and whole expression is assigned with value of exp2. If the exp1
is false, then exp2 is executed, and whole expression is assigned with exp2 value. For example,
c = a > b ? a+b : a-b;
Control Statements
Chapter 5 Page 4 Nawaraj Paudel
is equivalent to
if(a>b)
c=a+b;
else
c=a-b;
Control Statements
Chapter 5 Page 5 Nawaraj Paudel
else
statement n
OR
if (expression)
{
statement 1
statement 2
…
statement n
}
else if
{
statement 1
statement 2
…
statement n
}
…………………..
else
{
statement 1
statement 2
…
statement n
}
The conditions are evaluated from the top downward. As soon as a true condition is found, the
statement associated with it is executed and the rest of the ladder is bypassed. If none of the
conditions are true, the final else is executed. If the final else is not present, no actions take place
if all other conditions are false. For example,
if(amount >= 5000)
discount = amount * 0.1;
Control Statements
Chapter 5 Page 6 Nawaraj Paudel
Expre False
ssion1
Body of if
True
Statement
following else
body
Control Statements
Chapter 5 Page 7 Nawaraj Paudel
Control Statements
Chapter 5 Page 8 Nawaraj Paudel
case 'B':
printf("VERY GOOD");
break;
case 'c':
case 'C':
printf("GOOD");
break;
case 'd':
case 'D':
printf("SATISFACTORY");
break;
case 'e':
case 'E':
printf("FAIL");
break;
default:
printf("Wrong entry!");
}
Things to remember with switch:
A switch statement can only be used to test for equality of an expression. We cannot check
for other comparisons like <, <=, >, >=
switch expression must evaluate to an integral value
No two case constants can be same
Omission of a break statement causes execution to go to next case label
The default label is executed when no case constants matches the expression value
Control Statements
Chapter 5 Page 9 Nawaraj Paudel
Expression True
result equals First case
first-case body
constant
False
Expression True
result equals Second
second-case case body
constant
False
Expression True
result equals nth case
nth-case body
constant
False Default
body
Statement
following
switch
Control Statements
Chapter 5 Page 10 Nawaraj Paudel
Repetition Statements
Repetition is the process of executing a statement or a block of statements enclosed between
braces more than one time as long as some condition remains true. Repetition in C can be
implemented using three control statements: for, while, and do – while statements. These
statements are also known as iteration or looping statements.
Control Statements
Chapter 5 Page 11 Nawaraj Paudel
exp1
exp2 False
?
True
exp3
Control Statements
Chapter 5 Page 12 Nawaraj Paudel
Control Statements
Chapter 5 Page 13 Nawaraj Paudel
False
expression
?
True
Statement following
Body of while
while body
OR
do
{
statement 1
statement 2
…
statement n
} while(expression);
First body of do-while is executed, and expression is evaluated. If the value of expression is
nonzero (true), then control passes back to the beginning of the do statement and process repeats
itself. When expression is zero (false), control passes to next statement following the do-while
statement. For example,
Control Statements
Chapter 5 Page 14 Nawaraj Paudel
int i = 1, s = 0;
do
{
s = s + i;
i++;
}while(i <= 10);
printf("Sum = %d", s);
Note that a semicolon is present after the closing parenthesis of the expression. Also note that
loop body will always be executed at least once, since the test for repetition does not occur until
the end of the first pass through the loop. The do-while is most appropriate when the body
associated with the loop must be executed at least once.
Body of do while
False
expression
?
Statement following
True do-while body
Control Statements
Chapter 5 Page 15 Nawaraj Paudel
Nested loops
Like selection statements, a loop can be nested within another. The inner and outer loops need
not be same. It is essential, however, that one loop be completely embedded within the other—
there can be no overlap. When one loop is nested inside another loop, the inner loop is first
terminated and again restarted when the first loop starts for the next incremented value. The
outer loop is terminated last. For example,
for(i=0;i<10;i++)
{
printf(“Hi”)
for(j=0;j<5;j++)
printf(“Hello”);
}
Jump Statements
For the transfer of control from one part to another, C also supports various jump statements.
These are break, continue, goto, and return (discussed later).
Control Statements
Chapter 5 Page 16 Nawaraj Paudel
if( i == 5 )
break;
printf(" i=%d”, i );
}
printf("oho char pachhi khai ta”);
Control Statements