Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

UNIT II

CONTROL STATEMENTS:

Introduction To Decision Control Statements:


A C program is a set of statements, which are normally executed sequentially from the first
line of the program to its last line i.e., the second statement is executed after the first, the third
statement is executed after the second, and so on.
When a program breaks the sequential flow and jumps to another part of the code is called
branching.
C supports two types of decision control statements that can alter the flow of a sequence of
instructions. These include conditional type branching and unconditional type branching.

CONDITIONAL BRANCHING STATEMENTS:


The conditional branching statements help to jump from one part of the program to another
depending on whether a particular condition is satisfied or not. These decision control statements
include:
 if statement
 switch statement
 Conditional operator statement
if statement
The if statement is a powerful decision making statement and used to control the flow of
execution of statements. The if statement may be implemented in different forms depending on the
complexity of conditions to be tested. They are:
 Simple if statement
 if ... else statement
 Nested if .. else statement
 else if ladder
1) Simple if statement:
The if statement is the simplest form of decision control statements that is frequently used in
decision making. The general form of a simple if statement
is,
if(test expression) Test Expression
{
statement1;
. . . . . .
Statement n;
} Statement Block 1
Statement x;
The if statementmay include one statement or n statements
Statement x
enclosed within curly brackets. First the text expression is
evaluated. If the text expression is true, the statement of block (statement1 to n) is executed
otherwise these statements will be skipped and the execution will jump to statement x. When the
condition is true both the statement-block and the statement-x are executed in sequence.

2) if…..else statement
The if .. else statement is an extension of the simple if
statement. test expression

Syn:
if(test expression)
{
statement block 1;
statement block statement block
} 1 2
else
{
statement block 2; statement x
}
statement x;
In the above syntax, first the text expression is evaluated. If the expression is true, statement
block 1 is executed and statement block 2 is skipped. Otherwise, if the test expression is false,
statement block 2 is executed and statement block 1 is ignored. In both cases the control is will pass
to statement x. Therefore, statement x is executed in every case.
3. Nested if .. else statement
When a series of decisions are involved, we may have to use more than one if … statements in
nested form.
Syntax: if(expression-1)
{
if(expression-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
if(expression-3)
{
statement-3;
}
else
{
statement-4;
}
}
The control comes to the expression-2, if the value of expression-1 is true. If expression-2 is
true then the statement-1 is executed, otherwise statement-2 is executed. If the expression-1 is false
then the expression-3 is executed. If the expression-3 is true then statement-3 is executed otherwise
statement-4 is executed.

4. The Else if Ladder:

2
There is another way of putting ifs together when multipath decisions are involved. A
multipath decision is a chain of ifs in which the statement associated with each else is an if.
Syntax : if(condition-1)
Statement-1;
else if(condition-2)
statement-2;
else if(condition-3)
statement-3;
else if(condition-n)
statement-n;
else
default-statement;
statement-x;
This construct is known as the else if ladder. The conditions are evaluated from the top of
the ladder, downwards. As soon as a true condition is found, the statement associated with it is
executed and the control is transferred to the statement-x. When all the n conditions become false,
then the final else containing the default-statement will be executed.

switch case
A switch case statement is multi-way decision statement. The general form of a switch
statement is shown below.

switch (expression) TURE


value 1
{
case value 1: FALSE
statement block1
statement block1; TURE value 2
break;
case value 2: FALSE
statement block2; statement block2
break; FALSE
…………………… TURE
value N
case value N:
statement blockN; FALSE
statement block1
break;
default: default statement block
default statement block;
break;
statement x
}
Statement x;

The switch case statement compares the value of the expression given in the switch statement
with the value of each case statement that follows. When the value of the switch and the case
statement matches, the statement block of that particular case is executed.
Default is also a case that is executed when the value of the expression does not match with
any of the case statement, i.e., the default case is executed when there is no match found between the
value of switch and case statements and thus there are no statements to be executed.
In the syntax of switch case, the keyword break is used at the end of each case because if it
were not used, then all the cases from the one met will be executed.

ITERATIVE STATEMENTS (LOOP CONTROL STATEMENTS):


Iterative statements are used to repeat the execution of a list of statements, depending on the
value of an integer expression.The process of repeatedly executing a list of statements is known as
looping. The statements in the list may be executed any number of times.
A program loop therefore consists two segments, one known as the body of the loop and the
other known as the control statement.
Depending on the position of the control statement in the loop, a control structure may be
classified either as the entry-controlled loop or as exit-controlled loop.
In the entry-controlled loop, the control conditions are tested before the start of the loop
execution. If the conditions are not satisfied, then the body of the loop will not be executed.
In the case of an exit-controlled loop, the test is performed at the end of the body of the loop
and therefore the body is executed unconditionally for the first time.
C language supports three types of iterative statements also known as looping statements.
They are:
 while
 do-while
 for

THE WHILE STATEMENT


The while loop provides a mechanism to repeat one or more statements while a particular
condition is true.
The syntax of while loop is:
statement x;
while (condition)
{
statement block;
}
statement y;
The while is an entry controlled loop statement. The test condition is evaluated and if the
condition is true, then the body of the loop is executed. After execution of the body, the test
condition is once again evaluated and if it is true, the body is executed once again. This process of
repeated execution of the body continues until the test condition finally becomes false and the
control is transferred out of the loop.
Eg: To print 10 natural Numbers using while statement.
#include<stdio.h>
main()
{
int n=1;
while(n<=10)
{

4
printf(“%d “,n);
n++;
}
}

THE DO STATEMENT:
The do..while loop, sometimes simply referred to as the do loop in C, differs from the while
loop.
In while statement, it checks a test condition before the loop is execute. Therefore, the body
of the loop may not be executed at all if the condition is not satisfied at the very first attempt. On
some occasion it might be necessary to execute the body of the loop before the test condition is
performed. Such situations can be handled with the help of do while statement.
Syntax:
Initialization;
do
{
body of the loop;
}while(test-condition);

In this statement, the program proceeds to evaluate the body of the loop first. At the end of the
loop, the test-condition in the while statement is evaluated. If the condition is true, the program
continues to evaluate the body of the loop once again. This process continues as long as the
condition is true. When the condition becomes false, the loop will be terminated and the control goes
to the statement that appears after the while statement.
The do..while constructs provides an exit-controlled loop and therefore the body of the loop is
always executed at least once.

Eg: To print 10 natural Numbers using do-while statement


#include<stdio.h>
main()
{
int n=1;
do
{
printf(“%d “,n);
n++;
}while(n<=10);
}

THE FOR STATEMENT


The for loop is another entry-controlled looping structure available in C. The for loop is
typically repeat statement for a fixed number of times.
The following is the syntax of for loop:
for(initialization ; test condition ; increment/decrement)
{
Body of the loop
}
 The ‘for’ statement specifies three expressions to control the looping process. These expressions
should be separated by semicolon and should be enclosed in parenthesis. The expressions
are:
o The initial value
o The conditional test for existing the loop
o Increment or decrement
 The body of the loop can be either a simple statement or a compound statement. If more than one
statement executed under for loop, these statements are enclosed with curly braces.
 When expression1 is used to initialize some parameter that controls the looping action,
expression2 represents a condition that must be satisfied for the loop to continue execution and
experssion3 is used to alter the value of the parameter initially assigned by expression1.
 The initialization of counter is executed once before the loop statement is executed. Next the test
condition is evaluated.
 If the test condition is true, the statements in the body of the loop executed once.
 Then the control is transferred to expression3. The counter is assigned a new value using the
increment or decrement parameter.
 Then again conditional expression will be executed, if it is true, the body of the loop executed
again. The loop is repeated until the condition become false.

Eg: To print 10 natural Numbers using for statement


#include<stdio.h>
main()
{
int n;
for(n=1;n<=10;n++)
{
printf(“%d “,n);
}
}

NESTED LOOPS:
C allows its users to have nested loops, i.e., loops that can be places inside other loops.
Although this feature will work with any loop like while, do-while, and for but it is most commonly
used with the for loop, because this is easiest to control.
In C, loops can be nested to any desired level, for loops can be nested as follows:
..............
..............
for(i=1;1<10;1++) // Outer Loop
{
for(j=1;j<10;j++) //Inner Loop
{
............

6
............
}
}

JUMP CONTROL STATEMENT:


The break statement:
In C, the break statement is used to terminate the execution of the nearest enclosing loop in
which it appears. The break statement is widely used with switch statement, for loop, while loop, and
do-while loop. When compiler encounters a break statement, the control passes to the statement that
follows the loop in which the break statement appears. Its syntax is quite simple, just type keyword
break followed with a semi-colon.
break;
The below example shows the manner in which break statement is used to terminate the
statement in which it is embedded.
Eg:
#include<stdio.h>
main()
{
int i=0;
while(i<=10)
{
if(i==5)
break;
printf(“\n %d”,i);
i=i+1;
}
}
The break statement is used to exit a loop from any point within its body. The following
shows the transfer of control when the break statement is encountered.
while(...) do
{ {
...... ......
if(condition) if(condition)
break; break;
....... .......
} }while(...);
........ ........

Transfers control out Transfers control out


of the while loop of the do-while loop

for(...)
for(...)
{ {
...............
......for(...)
if(condition)
{
break; .......
....... if(condition)
break;
} .......
........
}
........
Transfers control out
}
of the for loop
Transfers control out
of the for loop
The continue statement:
Like the break statement, the continue statement can only appear in the body of a loop. When
the compiler encounters a continue statement then the rest of the statements in the loop are skipped
and the control is unconditionally transferred to the loop-continuation portion of the nearest
enclosing loop. Its syntax is:
continue;
When the continue statement is encountered in the while loop and in the do-while, the control
is transferred to the code that tests the controlling expression. In a for loop the continue statement
causes a branch to the code that updates the loop variable.
Eg:
#include<stdio.h>
main()
{
int n=1;
while(n<=20)
{
if(n%2==1)
{
n++;
continue;
}
printf(“%d ”, n);
n++;
}
}
The following is illustrate the use of continue statement in loops.
while(...) do
{ {
...... ......
if(condition) if(condition)
continue; continue;
....... .......
} }while(...);
........ ........

Transfers control to the Transfers control to the


condition of the while condition of the do-while

for(...) for(...)
{ {
...... ...............
for(...)
if(condition) {
continue; .......
....... if(condition)
} continue;
........ .......
}
........
Transfers control to the
condition of the for }
Transfers control to
the condition of the
for

GOTO STATEMENT:
8
The goto statement is used to transfer control to a specified label. However, the label must
reside in the same function and can appear only before one statement in the same function. The
syntax of goto statement is as shown below.

goto label label:


............. statements
............. .............
label: .............
statements goto label
statements

Forward jump Backward jump

Here, label is an identifier that specifies the place where the branch is to be made. Label can
be any valid variable name that is followed by a colon(:). The label is placed immediately before the
statement where the control has to be transferred.
The label can be placed anywhere in the program either before or after the goto statement.
Whenever the goto statement is encountered the control is immediately transferred to the statements
following the label. Therefore, the goto statement breaks the normal sequential execution of the
program. If the label is placed after the goto statement, then it is called a forward jump and in case it
is located before the goto statement, it is said to be a backward jump.
Eg-1:
#include<stdio.h>
main()
{
int n,sum=0;
for(n=1;n<=20;n++)
{
sum=sum+n;
if(n==10)
{
goto addition;
}
addition:
printf(“%d ”, sum);
}

Eg-2:
#include<stdio.h>
main()
{
int n=1;
naturals:
printf(“%d ”,n)
n++;
if(n<=10)
goto naturals;
}

You might also like