Module 6 – Control Flow: Branching
and Looping – Part 2
BITS Pilani Dr. Jagat Sesh Challa
Pilani Campus
Department of Computer Science & Information Systems
Module Overview
• Control Flow: Looping
• while Loop
• do… while Loop
• for Loop
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Control Flow: Looping
Looping – basics
• Many activities repetitive in nature
• Examples:
– Decimal-to-binary → repeated division
– Computing an average → repeated addition
• We need a mechanism to perform such repetitive tasks
• The repetition framework should also include the halting
mechanism
Loops in C support repetitive tasks
BITS Pilani, Pilani Campus
Loops in C
while loop
do… while loop
for loop
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
while loop
while loop
while (exp is true)
statement;
OR
while (exp is true)
{
statement1;
statement2;
…
}
BITS Pilani, Pilani Campus
Control expressions: Examples
• while (answer == ‘Y’)
• while (count)
• while (5)
• while (base>0 && power>=0)
• while (--num > 0)
• while (printf(“Enter the value of N: ”))
BITS Pilani, Pilani Campus
Example
int var=1;
while (var <=10)
{
printf("%d \n", var++);
}
printf(“While loop over”);
This program prints first 10 positive integers
BITS Pilani, Pilani Campus
A null body
while(num > 0);
is different from
while(num > 0)
BITS Pilani, Pilani Campus
Exercises
Write a program to calculate the sum of 10 numbers entered by the
user
BITS Pilani, Pilani Campus
Nested while loops
while(exp1){ int i = 10, j, count;
… while(i) {
while(exp2){ j = 5;
… while(j) {
} printf(“Hello!\n”);
} j--;
}
i--;
}
How many times is “Hello” printed?
BITS Pilani, Pilani Campus
Examples
int i = 10, j, count=0; int i = 10, j, count=0;
while(i) { while(i) {
j = 5; j = 5;
while(j) { while(j) {
printf(“%d ”, count++;
count++); j--;
j--; }
} i--;
i--; }
} printf(“%d ”, count);
BITS Pilani, Pilani Campus
Exercises
Print this pattern on your screen using loops. No. of rows are user input.
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
i =1;
while(i<=rows){
j =1;
while(j<=i){
printf("* ");
j++; }
printf("\n");
i++;
}
BITS Pilani, Pilani Campus
Exercises
Print this pattern on your
while(k != 2*i-1){
screen using loops.
printf("* ");
No. of rows are user input. ++k;
int i, space, rows, k=0; }
printf("Enter number of printf("\n");
rows: ");
i++;
scanf("%d",&rows);
k=0;
i=1;
}
while(i<=rows) {
space=1;
while(space<=rows-i){
printf(" ");
space++;
}
BITS Pilani, Pilani Campus
Practice tasks
*********
*******
*****
***
*
1
23
456
7 8 9 10
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Break and Continue
Break and continue
• break – immediate suspension of the current loop
• continue – restart a new iteration
BITS Pilani, Pilani Campus
Break and continue (Work out
on board)
Write a program to calculate the sum of at most 10 numbers
entered by the user
• When the user enters a negative value, prompt the user to
enter a positive integer
• When the user enters zero, terminate the loop
• Display the sum of the valid numbers entered by the user
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
The do… while loop
The do … while loop
do {
statement(s);
} while( condition );
• Conditional expression appears at the end
• Statements in the loop execute once before the condition tested
• Useful in scenarios where you want the code to execute at least
once
• While loop: first satisfy the condition then proceed
• Do while loop: proceed first, then check the condition
BITS Pilani, Pilani Campus
Example
int var=1;
do {
printf("%d ", var++);
} while (var <=10);
printf(“Do… While loop over”);
This program prints first 10 positive integers
BITS Pilani, Pilani Campus
while vs do… while
while loop do…while loop
This is entry-controlled loop. It This is exit-controlled loop.
checks condition before entering Checks condition when coming
into loop out from loop
The while loop may run zero or Do-While may run more than
more times one times but at least once.
The variable of test condition The variable for loop condition
must be initialized prior to may also be initialized in the
entering into the loop loop also.
while(condition){ do{
//statement //statement
} }while(condition);
Dept. of Computer Science & Information Systems, BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
The for loop
For loop
for (initialization; condition; increment or decrement)
{
//Statements to be executed repeatedly
}
• Initialization executed first, and only once
• Condition evaluated. If true, loop body executed
• Increment/decrement executed to update control variable
• Condition evaluated again. The process repeats.
• Condition false → loop terminates
BITS Pilani, Pilani Campus
Examples
int main() int main()
{ {
int i; int i;
for (i=1; i<=3; i++) for (i=1; i<=3; i++)
{ printf("%d\n", i);
printf("%d\n", i); return 0;
} }
return 0;
}
BITS Pilani, Pilani Campus
Various forms of for loop
✓ for (num=10; num<20; num=num+2)
✓ int num=10;
for ( ;num<20; num++)
✓ for (num=10; num<20; ) { … … num++; }
✓ for( ; ; )
✓ for (i=1,j=10; i<10 && j>0; i++, j--)
BITS Pilani, Pilani Campus
Exercises
What does this code print?
int count;
for (count=1; count<=3; count++);
printf("%d\n", count);
BITS Pilani, Pilani Campus
Exercises
Repeat this exercise using for loop:
Write a program to calculate the sum of max 10 numbers entered
by the user
• When the user enters a negative value, prompt the user to
enter a positive integer
• When the user enters zero, terminate the loop
• Display the sum of the valid numbers entered by the user
BITS Pilani, Pilani Campus
Exercises
Print this pattern on your screen using For loops. No. of rows are
user input.
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i)
{
for(j=1; j<=i; ++j)
printf("* ");
printf("\n");
}
BITS Pilani, Pilani Campus
Exercises
Homework
BITS Pilani, Pilani Campus
“for” vs “while” loop
BASIS FOR
FOR WHILE
COMPARISON
Declaration for(initialization; while ( condition) {
condition; iteration){ statements; //body of loop
//body of 'for' loop } }
Format Initialization, condition checking, iteration Only initialization and condition checking is
statement are written at the top of the done at the top of the loop.
loop.
Use The 'for' loop used only when we already The 'while' loop used only when the number
knew the number of iterations. of iteration are not exactly known.
Condition If the condition is not put up in 'for' loop, If the condition is not put up in 'while' loop,
then loop iterates infinite times. it provides compilation error.
Initialization In 'for' loop the initialization once done is In while loop if initialization is done during
never repeated. condition checking, then initialization is
done each time the loop iterate.
Iteration In 'for' loop iteration statement is written at In 'while' loop, the iteration statement can
statement top, hence, executes only after all be written anywhere in the loop.
statements in loop are executed.
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus