You are on page 1of 35

Loops

Loops
• Loops used for the program to be executed
repeatedly while expression is true.
• When the expression becomes false ,the loop
terminates and the control passes on to the
statement following the loop.
• Consists of two segments:
-control statements
-body of the loop
• Loop control instruction is method of repeating
some portion of the program either a specified
number of times or until a particular condition
is being satisfied

• Steps in looping
1)Initialing counter
2)Execution of statement in loop
3)Test specific condition for execution of loop
4)Increment counter
Kinds of Loops:

• for
• while
• do-while
for loop
• Is useful while executing a statement a
number of times.
syntax
for(initial expression; test expression; update
expression)
statement/compound statement
The for allows us to specify steps of looping
in a single line:
• Setting a loop counter to an initial value.
• Testing loop counter to determine
whether its value has reached the number
of desired repetitions.
• Increasing the value of loop counter each
time the program segment within the loop
has been executed.
For loop
Int n;
for( n=1; n<=10; )
{
printf(“%d”,n);
n++;
}
• the incrementation is done within the body of
the for loop and not in the for statement.
For loop
int n=1;
for( ; n<=10 ; )
{
printf(“%d”,n);
n++;
}
• the initialization is done in the declaration
statement and the incrementationis done within
the body of the for loop and not in the for
statement.
Initialization expression

Test expression
Exit

Body of loop

Update expression
For loop
//A program to display the first 10 multiples 5 on
a single line
#include<stdio.h>
main()
{
int i;
for(i=1;i<=10;i++)
printf(“%d”,5*i);
}
-for keyword is followed by the components
enclosed within round braces(),separated by
semicolons.
i=1
i<=10
i++
• First component i=1 is executed only once
prior to the statements with in the for loop
called initialization expression.
• Second component i<=10 is evaluated once
before every execution of the statements with
in the while loop called test expression.
• If the expression is true ,the statements with
in the loop executes.
• If it is false ,the loop terminates and the
control of execution is transferred to the
statements following the for loop .
• The third component i++ is executed once
after every execution of the statements with
in the loop.
• The expression increments value of i by 1
called update expression.
Keyword initialization expression
update exp

for(i=0;i<=10;i++)
statement;
test exp
• for (i=1;i<=10;i++)
{
statement1;
statement 2;
}
for(i=1;i<=10;i++) with no body
/* program to calculates sum of even*/
#include<stdio.h>
main()
{
int i;
int sum=0;
for(i=2;i<=30;i++)
{
sum+=i;
}
printf(“sum of first 15 even numbers=%d”,sum);
}
while loop
• Execution of body continues repeatedly
until test condition becomes false and
control is transferred out of loop
• Entry controlled loop:
the test condition is checked first and if
that condition is true than the block of
statement in the loop body will be
executed
Test
Exit
expression

Body of loop
While syntax
while(test expression)
{
statement;
statement;
……………

}
keyword

while(n!=0)
statement;

test expression
keyword

while(n!=0)
statement;
………………
statement; test expression
do-while loop
• while loop is top tested, it evaluates the
condition before executing any statements in
its body.
• In do-while evaluates the condition after the
execution of the statements
• do-while loop are executed at least once.
• do-while loop is bottom tested.
Do while loop
• Exit controlled loop:
The body of loop will be executed first
and at the end the test condition is
checked, if condition is satisfied than
body of loop will be executed again

• Body of loop is executed at least once


syntax
do
statement;
while(test expression);
do
{
statement;
statements;
……………………
}
while(test expression);
Flow chart: Do while
break statement
• terminates the execution of the loop and the
control is transferred to the statement
immediately following the loop.

• Program to find factorial of input number


Normal loop
return

Condition
break
with in loop

End of
loop
continue statement
• Is used to bypass the remainder of the current
pass through a loop.
• The loop does not terminates when the
continue statements is encountered.
syntax
continue;
Normal loop
return
Condition continue
with in loop

Remaining part of
the loop
Program to calculate avg of marks
#include<stdio.h>
main()
{
int n,count=1;
float x, avg, sum=0;
printf(“how many number:”);
scanf(“%d”,&n);
while(count<=n)
{
printf(“x=”);
scanf(“%d”,&x);
sum+=x;
count++;
}
avg=sum/n;
printf(“the avg is %f\n”,avg);
}

You might also like