3. LOOPING STATEMENTS
3
• Loops in programming come into use when we need to repeatedly
execute a block of statements.
• For example: Suppose we want to print “Hello World” 10 times.
• DEFINITION:A loop is a sequence of instructions that is repeated
until a certain condition is reached.
5. While Loop
5
•A while loop is a control flow statement that allows code
to be executed repeatedly based on a given Boolean
condition.
•The while loop can be thought of as a repeating if
statement.
•While loop has the following syntax:
while ( condition )
{
statements;
}
6. Trace while Loop
7
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Initialize count
7. Trace while Loop, cont.
8
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
(count < 2) is true
8. Trace while Loop, cont.
9
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Print Welcome to C
9. Trace while Loop, cont.
10
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Increase count by 1
count is 1 now
10. Trace while Loop, cont.
11
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
(count < 2) is still true since count is 1
11. Trace while Loop, cont.
12
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Print Welcome to C
12. Trace while Loop, cont.
13
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
Increase count by 1
count is 2 now
13. Trace while Loop, cont.
14
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
(count < 2) is false since count is 2 now
14. Trace while Loop, cont.
15
int count = 0;
while (count < 2)
{
printf("Welcome to C!");
count++;
}
The loop exits. Execute the next statement after
the loop.
16. do while Loop
16
• The do..while loop is similar to the while loop with
one important difference. The body
of do...while loop is executed at least once.
• Only then, the test expression is evaluated.
SYNTAX:
do
{
//Statements
}while(condition test);
17. 17
Write a program to check the given number is palindrome
do while - Program
#include <stdio.h>
int main() {
int num;
scanf("%d",&num);
int rev=0,temp,d;
temp=num;
do{
d=temp%10;
rev=(rev*10)+d;
temp/=10;
}while(temp!=0);
if(num==rev)
printf("The given number is palindrome");
return 0;
}
20. 20
Points to remember:
• The initialization step occurs one time only, before the loop
begins.
• The condition is tested at the beginning of each iteration of the
loop.
• If the condition is true ( non-zero ), then the body of the loop
is executed next.
• If the condition is false ( zero ), then the body is not
executed, and execution continues with the code following
the loop.
•The incrementation happens AFTER the execution of the body,
and only when the body is executed.
25. Nested Loops
• Similar to nested if statements, loops can be nested as well
• That is, the body of a loop can contain another loop
• For each iteration of the outer loop, the inner loop iterates
completely
25
26. Nested Loops
How many times will the string "Here" be printed?
26
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
printf ("Here");
count2++;
}
count1++;
}
10 * 20 = 200
27. Looping- nested for statements
What are nested for loops?
for(initialization; condition test ; updation)
{
//outer loop statement;
for(initialization; condition test ; updation)
{
//inner loop statements;
}
//outer loop statements;
}
Loop nested within loops
For example:
28. Looping- nested for statement working
for(int i=1; i<=2;i++)
{
int j;
for( j=1 ;i<=3 ; j++)
{
printf(“%d
%d”,i,j);
}
printf(“n”);
}
OUTER FOR
INNER FOR
How many
statements inside
outer for?
How many
statements inside
inner for?
29. Looping- nested for statement working
for(int i=1; i<=2;i++)
{
int j;
for( j=1 ;i<=3 ; j++)
{
printf(“%d %d”,i,j);
}
printf(“n”);
}
i=1 2( for true)
j= 1 2 3 (for true)
i j printf
1 1 1 1
1 2 1 2
1 3 1 3
2 1 2 1
2 2 2 2
2 3 2 3
How many
times i and
j are
printed?
30. Looping- nested for statement working
for(int i=1; i<=2;i++)
{
int j;
for( j=1 ;i<=3 ; j++)
{
printf(“%d %d”,i,j);
}
printf(“n”);
}
How many times outer for
loop got executed ?
How many times inner for
loop got executed ?
6
2
For each execution of the
outer loop the inner loop
is executed 3 times,
hence
2*3=6
34. break
• The keyword break allows the programmer to terminate the loop.
• The break skips from the loop or block in which it is defined and
the control automatically goes to the first statement after the loop
or block.
• If we use break statement in the innermost loop, then the control
of the program is terminated only from the innermost loop.
• Syntax:
break;
36. continue
• The continue statement is exactly opposite to break.
• The continue statement is used for continuing next iteration of loop
statement.
• When it occurs in the loop, it does not terminate, but it skips the
statements after this statement.
• It is useful when we want to continue the program without
executing any part of the program.
38. 38
#include <stdio.h>
int main() {
int i;
for(i=1;i<5;i++){
if(i==3)
break;
printf("%dt",i);
}
return 0;
}
#include <stdio.h>
int main() {
int i;
for(i=1;i<5;i++){
if(i==3)
continue;
printf("%dt",i);
}
return 0;
}
1 2
1 2 4
41. Looping-Problem Solving
41
Problem Statement: Read the amount of money you have and the prices of the
items you intend to buy. Determine whether you have enough money to buy
everything you selected or whether you are short of money. If you do not have
enough money, indicate the amount of the shortfall. Be sure to include 8% tax
when figuring the amount, you need.
Input: The first line in the data set is an integer that represents the number of data
collections that follow. There are an unknown number of
money amounts in each data set. The value –1 is used to indicate the end of the
collection of prices.
Output: All letters are to be upper case. Include
the amount of shortfall if you do not have enough money. This money amount is
to have a dollar sign ($) in front of the amount and it is to be rounded to 2
decimal places.