2. Introduction
• It is a tedious process perform such kind of
tasks repeatedly with pen/pencil and paper.
• Computer Programming language and
packages, the task becomes easy, accurate
and fast.
3. What is a “Loop” ?
• A loop is defined as a block of statements,
which are repeatedly executed for a certain
number of times.
• The loops are of two types
– Counter Controlled Repetition
– Sentinel Controlled Repetition
4. Counter Controlled Repetition
• It is also called the definite repetition action i.e. the
number of iteration to be performed is defined in
advance in the program itself.
• Steps in this type of loop
– Loop Variable
• Variable used in the loop
– Initialization
• It is first step in which starting and final values are assigned to the
loop variable.
• Each time the value updated is checked by the loop itself.
– Increment and Decrement
• It is the numerical value added or subtracted to the variable in
each round of the loop
• The updated value is compared with the final value and it is found
less than the final value the steps in the loop are executed
5. Sentinel-Controlled Repetition
• It is also called the indefinite repetition action
i.e. One cannot estimate how many iteration
are to be performed.
• In this type, Loop termination happens on the
basis of certain condition using decision-
making statement.
7. for Loop
• for loop allows to execute a set of instruction
until certain condition is satisfied.
• Condition may be predefined or open-ended.
for(initialize counter; test condition; re-evaluation parameter)
{
statement 1;
statement 2;
.
statement N;
}
8. for Loop
• The initialize counter sets to an initial value. This
statement is executed only once.
• The test condition is a relational expression that
determines the number of iterations desired or
determines when to exit from the loop.
• The “for” loop continues to execute as long as
conditional test is satisfied.
• When condition becomes false the control of
program exists from the body of the “for” loop
and executes next statement after the body of the
loop.
9. for Loop
• The re-evaluation parameter decides how to make
changes int the loop (increment or decrement
operations are to be used quite often).
• The body of loop may contain either a single
statement or multiple statements.
10. for Loop
Various formats of ‘for’ loop
Syntax Output Remarks
for( ; ; ) Infinite loop No Arguments
for( a=0; a<=20 ; ) Infinite loop ‘a’ is neither incremented nor
decremented
for( a=0; a<=10 ; a++) Displays value from 0 to 10 ‘a’ is increment from 0 to 10.
printf(“%d”, a); Curly braces are not necessary.
Default scope of the for loop is
one statement after the for loop.
for( a=10; a>=0 ; a--) Displays value from 10 to 0 ‘a’ is decremented from 10 to 0.
printf(“%d”, a);
11. Print the first five numbers starting from 1 together with their squares
void main()
{
int I;
clrscr();
for(i=1;i<=5;i++)
{
printf(“Number :%d it’s Square : %d”, i, i * i);
}
}
OUTPUT :
Number : 1 it’s Square: 1
Number : 2 it’s Square: 4
Number : 3 it’s Square : 9
Number : 4 it’s Square: 16
Number : 5 it’s Square: 25
12. Print the first five numbers starting from 1 together with their squares
void main()
{
int i;
clrscr();
for(i=1;i<=5;i++)
printf(“Number :%d it’s Square : %d”, i, i * i);
}
OUTPUT :
Number : 1 it’s Square: 1
Number : 2 it’s Square: 4
Number : 3 it’s Square : 9
Number : 4 it’s Square: 16
Number : 5 it’s Square: 25
13. Print the first five numbers starting from 1 together with their squares
void main()
{
int i=1;
clrscr();
for(;i<=5;i++)
printf(“Number :%d it’s Square : %d”, i, i * i);
}
OUTPUT :
Number : 1 it’s Square: 1
Number : 2 it’s Square: 4
Number : 3 it’s Square : 9
Number : 4 it’s Square: 16
Number : 5 it’s Square: 25
14. Print the first five numbers starting from 1 together with their squares
void main()
{
int i=1;
clrscr();
for(;i<=5;)
{
printf(“Number :%d it’s Square : %d”, i, i * i);
i++;
}
}
OUTPUT :
Number : 1 it’s Square: 1
Number : 2 it’s Square: 4
Number : 3 it’s Square : 9
Number : 4 it’s Square: 16
Number : 5 it’s Square: 25
15. Print the first five numbers starting from 1 together with their squares
void main()
{
int I;
clrscr();
for(i=1;i<=5;i++);
{
printf(“Number :%d it’s Square : %d”, i, i * i);
}
printf(“Number :%d it’s Square : %d”, i, i * i);
getch();
}
16. Nested for loops
• We can use loop inside loop, which is known
as nested loop
• In nested for loops one or more for
statements are included in the body of the
loop.
• The numbers of iterations in this type of
structure will be equal to the number of
iteration in the outer loop multiplied by the
number of iterations in the inner loop.
17. Print the first five numbers starting from 1 together with their squares
void main()
{
int a, b, sub;
clrscr();
for(a=3;a>=1;a--);
{
for(b=1; b<=2; b++)
{
sub = a – b;
printf(“a = %d b = %d a – b = %dn”, a, b, sub);
}
}
getch();
}
Output:
a=3b=1a–b=2
a=3b=2a–b=1
a=2b=1a–b=1
a=2b=2a–b=0
a=2b=2a–b=0
a=1b=1a–b=0
a = 1 b = 1 a – b = -1
18. while loop
• The while loop is frequently used in programs
for the repeated execution of statement in a
loop.
• The loop continues until a certain condition is
satisfied.
20. while loop
• The test condition is indicated at the top and its tests the
value of the expression before processing the body of
the loop.
• The loop statements will be executed till the condition is
true.
• When the condition becomes false the execution will be
out of the loop.
• The braces are needed only if the body of the loop
contains more than one statement.
• However, it is a good practice to use braces even if the
body of the loop contains only one statement.
21. • Steps of while loops are as
Entry
follows
– The test condition is
Initialize evaluated and if it is true, the
body of the loop is executed.
– On execution of the body,
F
Test test condition is repetitively
Condition checked and if it is true the
body is executed.
Stop
T – The process of execution of
the body will be continued till
Body of the loop the test condition becomes
true.
– The control is transferred out
Update of the loop if test condition
fails.
22. Print “You are in MESICS” string for nine times
OUTPUT :
You are in MESICS
You are in MESICS
void main() You are in MESICS
{ You are in MESICS
You are in MESICS
int i=1; You are in MESICS
clrscr(); You are in MESICS
You are in MESICS
while(i<=9) You are in MESICS
{
printf(“You are in MESICSn”);
i++;
}
}
24. do…while loop
• The difference between the while and do-while loop
is the place where the condition is to be tested.
• In the while loops the condition is tested following
the while statement, and then the body gets
executed.
• In the do…while the condition is checked at the end
of the loop.
• The do…while loop will execute at least one time
even if the condition is false initially.
• The do…while loop executes until the condition
becomes false.
25. Comparison of while and do..while
Sr. No while loop do…while loop
Condition is specified at the Condition is mentioned at the
1.
top. bottom.
Body statement/s is/are
Body statement/s executes even
2. executed when condition is
when the condition is false.
satisfied.
No brackets for a single Brackets are essential even when a
3.
statement. single statement exists.
4. It is an entry-controlled loop. It is an exit-controlled loop.
26. Print “You are in MESICS” string for nine times
OUTPUT :
You are in MESICS
You are in MESICS
void main() You are in MESICS
{ You are in MESICS
You are in MESICS
int i=1;
clrscr();
do
{
printf(“You are in MESICSn”);
i++;
} while(i<=5);
}