ED601-Session3-Controlling Program Flow in C
ED601-Session3-Controlling Program Flow in C
Shoukath Cherukat
Scientist E
Control Structures
• Used to control the execution of various
statements in the program.
• By default, instructions in the program are
executed sequentially.
• Control Structures are classified into three
categories.
➢ Decision Making
➢ Loops
➢ Switch Case
9/9/2022 NIELIT 2
Decision Making
• When we want a set of instruction to be executed
at one situation, and entirely different set of
instruction to be executed in another situation.
• Implemented using the following:
➢ The if statement
9/9/2022 NIELIT 3
if statement
9/9/2022 NIELIT 4
if statement – syntax
• General form of if statement is:
if ( this condition is true )
{
statement 1;
statement 2;
.
.
statement n;
}
9/9/2022 NIELIT 5
If statement – example
main( )
{
int num ;
printf ( "Enter a number less than 10 “) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient student you are !" ) ;
}
9/9/2022 NIELIT 6
If-else statement
• If statement does nothing when the expression
evaluates to false.
• General form of if-else statement:
if(expression)
{
statements;
}
else
{
statements;
}
9/9/2022 NIELIT 7
If-else statement example
if(A>B)
{
printf(“A is big”);
}
else
{
printf(“B is big”)
}
9/9/2022 NIELIT 8
Logical operators
• C allows use of logical operators.
AND &&
OR ||
NOT !
9/9/2022 NIELIT 9
Logical Operators
main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ("%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division" ) ;
if ( ( per >= 50 ) && ( per < 60 ) )
printf ( "Second division" ) ;
if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Third division" ) ;
If ( per < 40 )
printf ( "Fail" ) ;
}
9/9/2022 NIELIT 10
Logical Operators
• ! Operator – Reverses the result of the operator it
operates on.
• For example, if the expression evaluates to non-zero,
then by applying ! Operator we can make it it zero
and vice-versa.
• Along with the hierarchy of other operators, logical
operators also has a priority which is mentioned in
the table.
9/9/2022 NIELIT 11
Hierarchy of logical operators
9/9/2022 NIELIT 13
Ternary operators-Example
int x, y ;
scanf ( "%d", &x ) ;
y=(x>5?3:4);
Equivalent if statement will be
if ( x > 5 )
y=3;
else
y=4;
9/9/2022 NIELIT 14
Loops
• Used to perform set of operations repeatedly.
• Three statements are used to achieve looping
➢ While statement
➢ For statement
➢ Do-while statement
9/9/2022 NIELIT 15
While statement
• General form of while statement
initialize loop counter
while(test loop counter for a condition)
{
statement 1;
statement 2;
..
..
statement n;
increment or decrement loop counter;
}
9/9/2022 NIELIT 16
While statement example
main()
{
int i=1;
while(i<=10)
{
printf(“%d\n”,i);
i=i+1;
}
}
9/9/2022 NIELIT 17
Increment and Decrement operator
• ++ operator is used to increment the value by 1.
i=i+1 => i++;
• -- operator is used to decrement the value by 1.
i=i-1 => i--;
Similarly, compound assignment += operator can
be used.
j+=10 => j=j+10
9/9/2022 NIELIT 18
Increment operator
• Consider the statement:
While(i++<10)
First, comparison of i with value 10 is
performed, and then the incrementing
takes place. Here,++ is called post-
increment operator.
while(++i<10)
First, incrementing takes place, then the
comparison of i with value 10 is
performed. here,++ is called pre-increment
operator.
9/9/2022 NIELIT 19
do-while loop
• When you are not sure about how many times
the loops has to be executed.
• General form of do-while statement is:
do
{
statements;
}while(condition)
Here statements inside the loop is getting
executed at least once.
9/9/2022 NIELIT 20
do-while loop-example
main( )
{
char another ;
int num ; do
{
printf ( "Enter a number " ) ;
scanf ( "%d", &num ) ;
printf ( "square of %d is %d", num, num * num ) ;
printf ( "\n Want to enter another number y /n " ) ;
scanf ( " %c", &another ) ;
} while ( another == 'y' ) ;
}
9/9/2022 NIELIT 21
for loop
9/9/2022 NIELIT 22
for loop – syntax
9/9/2022 NIELIT 23
for loop
• It allows us to specify three things in a single
line.
➢ Setting a loop counter to an initial value
9/9/2022 NIELIT 24
for loop
main( )
{
int i ;
for ( i = 1 ; i <= 10 ; i = i + 1 )
printf ( "%d \n", i ) ;
}
• Consider the example
initial value is i=1;
testing loop counter i<=10
increment the loop counter i=i+1
9/9/2022 NIELIT 26
Continue statement
• To take the control to the beginning of the loop
and bypassing the statements inside the loop.
9/9/2022 NIELIT 27
goto statement
• Used to transfer the control to anywhere in the
program.
• General form of goto is
goto label;
..
..
label:
9/9/2022 NIELIT 28
Switch – Case
•allows us to make a decision from the number of choices.
9/9/2022 NIELIT 29
Switch – Case Syntax
• General form of switch statement is
9/9/2022 NIELIT 30
Switch statement-example
main( )
{
int i = 2 ;
switch ( i )
{
case 1 :
printf ( "I am in case 1 \n" ) ;
break;
case 2 :
printf ( "I am in case 2 \n" ) ;
break;
case 3 :
printf ( "I am in case 3 \n" ) ;
break;
default :
printf ( "I am in default \n" ) ;
}
}
9/9/2022 NIELIT 31
References – Books
• Let Us C, Yashavant P. Kanetkar, BPB
Publications, ISBN:978-81-8333-163-0
9/9/2022 NIELIT 32
Thank You