0% found this document useful (0 votes)
13 views8 pages

Looping

Uploaded by

bikashbogati001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Looping

Uploaded by

bikashbogati001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Looping (While, do while and for)

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;
}

Where initialization means starting point,


condition means stopping point and
increment/decrement means counter.

Example: -Program to print “C is best “ ten times using while loop


#include<stdio.h>
void main ()
{
int i=1;
while(i<=10)
{
printf(" C is the best\n");
i++;
}
}

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;
}
}

Example: -Program to print multiple of 5 using while loop


#include<stdio.h>
void main()
{
int i=1,m;
while(i<=10)
{
m=5*I;
printf("\n5 * %d = %d",I,m);
i++;
}
}

Eg: Program to find the sum of digit in a given number


#include<stdio.h>
void main()
{
int n,r,sum=0;
printf("Enter a number = ");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n//10;
}
Printf(“Sum of number is %d”,sum)
}

Eg: Program to find the reverse of given number


#include<stdio.h>
void main()
{
int n,r,c=0;
printf("Enter a number = ");
scanf("%d",&n);
while(n!=0)
{
r=n%10;
c=c*10+r;
n=n/10;
}
Printf(“Reverse of number is %d”,c)
}

Eg: Program to check a palindrome number


#include<stdio.h>
void main()
{
int a,b,r,c=0;
printf("Enter a number = ");
scanf("%d",&a);
b=a;
while(b!=0)
{
r=b%10;
c=c*10+r;
b=b/10;
}
if(c==a)
printf("\n Entered number is palindrome");
else
printf("\n Entered number in not palindrome");
}

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 display " C is the best" 10 times using do while


#include<stdio.h>
void main()
{
int i=1;
do
{
printf(" C is the best\n");
i++;
} while(i<=10);
}

Example: -Program to print all the number from 1 to 10 using do while


#include<stdio.h>
void main()
{
int i=1;
do
{
printf("\n%d",i);
i++;
} while(i<=10);
}

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);
}

Example: -Program to print multiple of 5 using do while


#include<stdio.h>
void main()
{
int i=1,m;
do
{
m=5*I;
printf("\n5 * %d = %d",i,m);
i++;
} 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

For (initialization; condition; increment/decrement)


{
Satements;
}

Example : - program to display " C is the best" 10 times


#include<stdio.h>
void main()
{
Int I;
For(i=1;i<=10;i++)
{
printf(" C is the best\n");
}
}

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);
}
}

Example : - program to display multiple of 5


#include<stdio.h>
void main()
{
Int i,m;
For(i=1;i<=10;i++)
{
M=5*I;
printf("\n 5 * %d = %d",i,m);
}
}

Difference between while loop and do while loop


while loop do while loop
Condition is checked in the beginning. In do while loop, condition is checked at the end.
It is also called as pre-test or entry control loop. It is also known as post-test or exit control loop
It is not terminated with semicolon. It is terminated with semi colon.
In while loop, statements are not executed if the In do while loop, statements are executed once even the
condition is false. condition is false.
It uses keyword ‘while’ It uses two keywords ‘do’ and ‘while’
Syntax:- Syntax: -
Initialization; Initialization;
While(condition) do
{ {
Satements; Satements;
Increment/decrement; Increment/decrement;
} } While(condition);

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:
}

You might also like