Looping
Looping
Looping is the process of executing the same program statement or block of program statements repeatedly
for specified number of times or till the given condition is satisfied. Loop structure is used to carry out looping.
Mainly there are 3 types of loop: for, while, do while loop.
While loop:
It executes the program statements repeatedly until the given condition is true it checks the condition at first;
if it is found true then it executes the statements otherwise it just gets out from the loop. It is known as entry control
or pre-test loop.
Syntax Flowchart
Initialization;
While(condition)
{
Satements;
Increment/decrement;
}
Example: -Program to print all the number from 1 to 10 using while loop
#include<stdio.h>
void main()
{
int i=1;
while(i<=10)
{
printf("\n%d",i);
i++;
}
}
Example: -Program to print “all the even numbers from 2 to 50 using while loop
#include<stdio.h>
void main()
{
int i=2;
while(i<=50)
{
printf("\n%d",i);
i=i+2;
}
}
Do while loop: -
It also executes program statements repeatedly until given condition is true. It executes the program
statements once at first then only condition is checked. If condition is found true then it executes the program
statement again, otherwise it gets out from the loop structure. It is also known as post-test loop.
Syntax Flowchart
Initialization;
do
{
Satements;
Increment/decrement;
} While(condition);
Example: -Program to print “all the even numbers from 2 to 50 using do while
#include<stdio.h>
void main()
{
int i=2;
do
{
printf("\n%d",i);
i=i+2;
} while(i<=10);
}
For loop: -
It is used to execute program statement or block of program statements repeatedly for specified number of
time.
It is definite loop.
Syntax Flowchart
Example : - program to display all the numbers from 1 to 10 using for loop
#include<stdio.h>
void main()
{
Int I;
For(i=1;i<=10;i++)
{
printf("\n%d",i);
}
}
Example : - program to display all the even numbers 1 to 50 using for loop
#include<stdio.h>
void main()
{
Int I;
For(i=1;i<=10;i++)
{
If( i % 2 == 0)
printf("\n %d",i);
}
}
Nested loop
We can write an entire loop structure inside another loop structure. So loop inside another loop is called
nested loop.
Syntax: -
For( initialization; condition; increment/decrement)
{
For (initialization; condition; increment/decrement)
{
Satements;
}
}
Example: -
#include<stdio.h> Output: -
Void main() 1
{ 1 2
Int i, j; 1 2 3
For( i=1; i<=5; i++) 1 2 3 4
{ 1 2 3 4 5
For( j=1; j<=i; j++)
{
Printf(“%d”; j );
}
Printf(“\n”);
}
}
Example: -
#include<stdio.h> Output: -
Void main() 1
{ 2 2
Int i, j; 3 3 3
For( i=1; i<=5; i++) 4 4 4 4
{ 5 5 5 5 5
For(j=1; j<=i ;j++)
{
Printf(“%d”; i );
}
Printf(“\n”);
}
}
Infinite loop: -
A loop which never terminates is called infinite loop that is it executes the program statements repeatedly
which doesn’t meet any ending point.
Example: -
#include<stdio.h>
Void main()
{
Int I;
For(i=1;i>=0;i++)
Printf(“%d”,i);
}
Jumping Statement: -
Jump statements are particularly used to jump execution of program statement from one place to another
place inside a program. These statements may execute same program statement repeatedly or skip some program
statement.
Following are jumping statement used in c programming language
break
continue
goto
break statement: -
it is used to break the normal flow of program statement execution in loop and switch case statement. It is
used to exit from innermost enclosing loop or switch statement as soon as certain condition is satisfied.
Example.
#include<stdio.h>
Void main()
{
Int I;
For(i=1;i>=5;i++)
{
If(i==4)
break;
printf(“%d\t”,i);
}
}
Continue statement: -
It is used to continue the normal flow of program statement execution in loop; skipping particular iteration in
loop as soon as certain condition is satisfied.
Example: -
#include<stdio.h>
Void main()
{
Int I;
For(i=1;i>=5;i++)
{
If(i==4)
continue;
printf(“%d\t”,i);
}
}
Goto statement: -
When goto statement is encountered in a program then it transfers the control of the program statement
execution unconditionally to the location specified by the goto statement within a current function.
Syntax: - goto label;
Example
#include<stdio.h>
Void main()
{
Int I=1;
Label 1:
Printf(“%d\n”,i);
I++;
If(i<=10)
Goto label 1:
}