0% found this document useful (0 votes)
13 views

ED601-Session3-Controlling Program Flow in C

Uploaded by

ER. MANISH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

ED601-Session3-Controlling Program Flow in C

Uploaded by

ER. MANISH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

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

➢ The if-else statement

➢ The conditional operators

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 !" ) ;
}

In c, non-zero value is considered to be true, zero value is


considered to be false

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”)
}

If-else statements can also be nested

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

Note: In ‘C’ assignment operator ‘=‘ is, different from


relational operator ‘==‘.
9/9/2022 NIELIT 12
Conditional or ternary operator
• Conditional operators ? And : are called ternary
operators, since they take three arguments.
• General form of ternary operator is:
Expression1?expression2:expression3
• If expression 1 is true, value returned is
expression2,else expression 3 will be returned.

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

General form of for statement:


for(initialize counter; test counter; increment
counter)
{
Statements;
}

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

➢ Testing a loop counter to determine

whether it has reached number of


repetitions
➢ Increase the loop counter each time when

the program segment within the loop has


been executed

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

Note: nesting of for loops can also be done.


9/9/2022 NIELIT 25
Break statement
• Used to jump out of the loop instantly.
• Control is passed to the first statement after the
loop.
• This statement is used with for and while loops
while ( j++ <= 200 )
{
if ( j == 150 )
break ;
else
printf ( "%d %d \n", i, j ) ;
}

9/9/2022 NIELIT 26
Continue statement
• To take the control to the beginning of the loop
and bypassing the statements inside the loop.

for ( i = 1 ; i <= 2 ; i++ )


{
for ( j = 1 ; j <= 2 ; j++ )
{
if ( i == j )
continue ;
printf ( "\n%d %d\n", i, j ) ;
}
}

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

switch ( integer expression )


{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}

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

• The C Programming Language, Brian W.


Kernighan, With: Dennis M. Ritchie Dennis M.
Ritchie, Prentice Hall, ISBN: 9780131101630
• C in a Nutshell, 2e: The Definitive Reference,
by Peter Prinz and Tony Crawford, O′Reilly;
2nd edition,

9/9/2022 NIELIT 32
Thank You

Shoukath Cherukat , Scientist E

You might also like