Professional Documents
Culture Documents
TO
LOOPs
1
OUTLINE
What is a Loop?
Classification of Loops
Types of Loop Control Statements
The while loop
The do while loop
The for loop
Infinite Loops
Nested Loops
Programming Examples
2
Introduction to Loops
Loops allow program to execute a statement or group of statements
multiple times repeatedly until certain test condition is false.
ENTRY
EXIT CONTROL
CONTROL
LOOPS
LOOPS
Exit Control
Loop
Entry Control
Loops
5
The while loop
A while loop is an entry control loop having the following
syntax:
Working Principle:
6
Logic of a while loop
Syntax: condition
evaluated
while (condition)
{
true false
Statements;
}
statement
7
An Example of while loop
count
int count = 1; 5
4
3
2
1
6
while (count <= 5)
{ Con
ditio
n ev
printf(“HELLO\n”); alua
ted
count++;
}
FALSE
TRUE
OUTPU
T
1 2 3 4 5
9
Contd…
Condition is FALSE
from the first time.
int count = 10;
while (count <= 5)
{
printf(“HELLO\n”);
count++; NO
}
OUTPUT
10
Contd…
Loop control variable is the
variable whose value
controls loop repetition
int count = 1;
while (count <= 5)
{
In this
printf(“HELLO\n”);
example,
count++;
count is the
}
loop control
variable.
11
Program To Find Sum of Digits of A Number
main( ) Output :
{
Enter a number : 1452
int n, sum=0, rem;
Sum of digits = 12
printf(“Enter a number : ”);
scanf(“%d”, &n);
while(n>0)
Taking a number as input
{
rem = n%10;
sum = sum + rem; Checking thecondition:
n = n/10; True or False
}
printf(“Sum of digits =%d\n”, sum);
}
12
Lets see the working of the loop
Consider the input given as
n = 1452
Example:
int count = 1; Here the condition is
while (count <= 25) always true so it is an
{ infinite loop.
printf(“%d\t”,count);
count = count - 1;
}
14
Some More Examples of Infinite Loops
while(1) while(100)
{ {
printf(“Good Morning\n”); printf(“Good Morning\n”);
} }
while(‘A’) while(-525)
{ {
printf(“Good Morning\n”); printf(“Good Morning\n”);
} }
15
Nested Loops
When the body of one loop contains another loop, then it is called a
Nested Loop.
Nested loops contain an outer loop with one or more inner loops.
Here for each iteration of the outer loop ,the inner loop iterates
completely.
16
Example Outer while
loop
count1 = 1;
while (count1 <= 10)
{ Inner while loop
count2 = 1;
while (count2 <= 20)
{ For each value of count1…
printf("Hello"); count2 will be executed 20 times.
count2++;
count1 has 10 values & count2
}
has 20 values, therefore Hello
count1++; will be printed 10 * 20 times.
}
Working Principle:
do
{ Initially, the statement/s is
executed once and then the
Body Statements; condition is checked.
of
} while
the If the condition is true, the
loop (condition);
statement is executed again.
The statement is executed
repeatedly until the condition
becomes false.
18
Logic of a do-while loop
Syntax:
statement
do
{
Statements; true
} while condition
(condition); evaluated
false
20
Comparing while and do
statement
condition
evaluated true
condition
true false
evaluated
statement
false
21
The for Statement
A for statement has the following syntax :
The initialization The statement is
is executed once executed until the
before the loop begins condition becomes false
22
Logic of a for loop
initialization
condition
evaluated
true false
statement
increment
The for Statement
A for loop is functionally equivalent to the following while loop structure:
initialization;
while ( condition )
{
statement;
increment;
}
The for Statement
An example of a for loop:
Like a while loop, the condition of a for loop is tested prior to executing
the loop body
Therefore, the body of a for loop will execute zero or more times
An Example of the for Loop
Initialization Expression
Update Expression
Remember the break keyword that we used to stop a switch statement from
executing more than one statement?
break can also be used to exit an infinite loop
But it is almost always best to use a well-written while loop
27
Comparison of Prefix and Postfix Increments
The value of the expression (that uses the ++/--
operators) depends on the position of the operator.