INTRODUCTION
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.
That is, Loops are used in performing repetitive task in
programming.
Why Use Loops???
There may be a situation, when you need to execute a block of code
several number of times. Consider the scenarios:
You want to execute some code/s 100 times…
It’s not wise to write the same code 100 times.
You want to calculate the value of n!, for any value of n…
If n = 1000, it’s not elegant to write the code as 1*2*3*…
*1000.
3
Classification of Loops
Loops are broadly classified into two categories
ENTRY
EXIT CONTROL
CONTROL
LOOPS
LOOPS
The body of the loop is executed
The types of loop where the
without testing the condition for
test condition is tested before
the first time. Then the condition is
the execution of the loop.
tested.
If it is true, then the loop is
If the test condition is true,
executed again and continues till
then the loop gets the execution,
the result of the test condition is
otherwise not.
not false.
4
Types of Loop Control Statements
There are three types of loop control statements available in C
Exit Control
Loop
Entry Control
Loops
5
The while loop
A while loop is an entry control loop having the following
syntax:
Working Principle:
while (condition) If the condition is true, the
statement/s is executed.
Body {
of Then the condition is evaluated
Statements;
the again, and if it is still true, the
loop }
statement is executed again.
The statement is executed
repeatedly until the condition
becomes false.
6
Logic of a while loop
Syntax: condition
evaluated
while (condition)
{
true false
Statements;
}
statement
Out of Body of Loop
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
OUTPUT: The condition (count <=5) remains
HELLO TRUE for :
HELLO count=1 count =2 count =3
count =4 count = 5
HELLO
HELLO For count = 6….the condition
HELLO becomes FALSE
8
Another Example
Condition is TRUE for
count = 1, 2, 3, 4,5
int count = 1;
while (count <= 5)
{
printf(“count=%d\t”,count);
count++;
}
OUTPU
T
1 2 3 4 5
Prints the value of count
until the condition becomes
false
9
Contd…
Condition is FALSE
from the first time.
int count = 10;
while (count <= 5)
{
printf(“HELLO\n”);
count++; NO
}
OUTPUT
• If the condition of a while loop is FALSE initially, the
statement is never executed.
• Therefore, the body of a while loop will execute zero times.
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
Before loop starts rem=garbage value sum=0 n =1452
After 1st iteration rem=1452%10=2 sum=0+2=2 n =145
After 2nd iteration rem=145%10=5 sum=2+5=7 n =14
After 3rd iteration rem=14%10=4 sum=7+4=11 n =1
After 4th iteration rem=1%10=1 sum=11+1=12 n =0
For the 5th iteration as n = 0 now….the condition becomes FALSE and
we are out of the loop body
13
Infinite Loops
The body of a loop eventually must make the condition false and
terminate at a point.
If not, it is called an infinite loop, which will execute until the user
interrupts the program or until an underflow error occurs.
This is a common logical (semantic) error.
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.
}
How many times will the above code print “Hello”???
17
The do-while loop
A do-while loop is an exit control loop having the following
syntax:
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
Out of Body of Loop
19
Example of do-while
int count = 0;
do{
count++;
printf(“count=
%d\t”,count);
}while (count < 5);
The body of a do loop executes at least once
20
Comparing while and do
The while Loop The do Loop
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
for ( initialization ; condition ; increment )
{
statement;
}
The increment portion is executed at
the end of each iteration
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:
for (count=1; count <= 5; count++)
{
printf(“count %d=“,count);
}
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
Loop repetition condition
Update Expression
count_emp is set to 0 initially.
count_emp should not exceed the value of number_emp.
count_emp is increased by one after each iteration.
break revisited
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.
The value The value of j
of j is is not
increased increased
THANK YOU