The ‘C’ language provides
three loop constructs for performing loop operations they are
Flow chart:
1. The while statement
2. Do-while statement
3. The for statement
The While Statement : This type of loop is also called an entry controlled,
is executed and if is true then the body
of the loop is executed this process repeated until the boolean
expression becomes false. Ones it becomes false the control is a transferred
out the loop. The general form of the while statement is
While
(boolean expression)
{
body
of the loop;
}
Ex : i = 1;
While(I<=5)
{ printf(“%d”,i);
i++; }
In the above example the
loop with be executed until the condition is false
Do-While Statement : This type of loop is also called an exist
controlled loop statement.
i.e.., The boolean expression evaluated at the bottom of the loop and if it
is true then body of the loop executed again and again until the boolean
expression becomes false. Ones it becomes false the control is do-while
statement is
Do
{
body
of the loop ;
}
while
( boolean expression)
Flow chart :
Ex : i = 1;
Do
{
printf(“%d”,i);
i++;
}
While(i<=5)
While statement Do-while statement
2. If boolean expression is
false 2. The body
of the loop executed
then the body of the loop never atleast ones even be executed if the
be
executed. Boolean expression is
either true or false
The For Statement : The for loop is another
entry control led loop that provides a more concise loop control structure the
general form of the for loop is
For
( initialisation; test condition; increment)
{
body
of the loop;
}
Where initialization is used to initialize
some parameter that controls the looping action, ‘test condition’
represents if that condition is true
the body of the loop is executed, otherwise the loop is terminated After
evaluating information and the new value
of the control variable is again tested the loop condition. If the condition is
satisfied the body of the loop is again
executed it this process continues until the value of the control variable
false to satisfy the condition.
Flow chart :
Ex : for (I=1; I<=5; I++)
{ Output :
1 2
3 4 5
printf(“%d”,i);
}
Jumping From The Loops :
Break Statement : The break statement can be accomplish by using to
exist the loop. when break is encountered inside a loop, the loop is immediately exited and
the program continues with the statement which is followed by the loop. If
nested loops then the break statement inside one loop transfers the control to
the next outer loop.
Ex : for (I=1; I<5; I++)
{ Output : 1 2 3
if ( I == 4)
break;
printf(“%d”,i);
}
Continue Statement : The continue statement which is like break statement. Its work is to
skip the present statement and
continues with the next iteration of the
loop .
Ex : for (I=1; I<5; I++)
{ Output : 1 2
4 5
if ( I == 3)
continue;
printf(“%d”,i);
}
In the above example when
I=3 then the continue statement
will rise and skip statement in
the loop and continues for the next iteration i.e.., I=4.
No comments:
Post a Comment