Looping/Iterative Statements
What is Loop
one statements are executed one or more time .looping simplify the complex problems into the easy one.loop provides code reusability.
we do not need to write same code again and again.looping is used to traverse the element of data structure.
Types of loops
💥 For Loop:- In C the For loop is used to iterate a part of the program many time.In the for loop number of iteration is fixed.
Syntax:-
for(initialization,condition,increment/decrement)
{
statement
}
Flow chart of for loop
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
for(i=1;i<=10;i++)
{
printf("%d",i);
}
getch();
}
Output
1 2 3 4 5 6 7 8 9 10
💥 While loop:-While loop is also known as entry control loop Because it first check the condition than condition is true the statement are executed otherwise condition is false the statement are not executed. In the while loop the iteration are not fixed.
Syntax:-
while(condition)
{
statement
}
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
{
printf("%d",i);
i++;
}
getch();
}
💥 Do while loop:- Do while loop is also known as exit control loop.If the number of iteration is not fixed and you must have to execute the statement at least once whether the condition true or false because the checked the condition after loop body.
Syntax:-
do
{
statement
}while(condition);
Example
#include<stdio.h>
#include<conio.h>
void main()
{
do
{
printf("%d",i);
i++;
}while(i<=10);
getch();
}
Difference between for ,while and do while loop
What is Loop
one statements are executed one or more time .looping simplify the complex problems into the easy one.loop provides code reusability.
we do not need to write same code again and again.looping is used to traverse the element of data structure.
Types of loops
💥 For Loop:- In C the For loop is used to iterate a part of the program many time.In the for loop number of iteration is fixed.
Syntax:-
for(initialization,condition,increment/decrement)
{
statement
}
Flow chart of for loop
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
for(i=1;i<=10;i++)
{
printf("%d",i);
}
getch();
}
Output
1 2 3 4 5 6 7 8 9 10
💥 While loop:-While loop is also known as entry control loop Because it first check the condition than condition is true the statement are executed otherwise condition is false the statement are not executed. In the while loop the iteration are not fixed.
Syntax:-
while(condition)
{
statement
}
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
{
printf("%d",i);
i++;
}
getch();
}
Output
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
💥 Do while loop:- Do while loop is also known as exit control loop.If the number of iteration is not fixed and you must have to execute the statement at least once whether the condition true or false because the checked the condition after loop body.
Syntax:-
do
{
statement
}while(condition);
Example
#include<stdio.h>
#include<conio.h>
void main()
{
do
{
printf("%d",i);
i++;
}while(i<=10);
getch();
}
Output
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
Difference between for ,while and do while loop
very Nice ..Keep Doing
ReplyDeletehttps://www.technicalbook.in
https://www.technicalbook.in/2020/05/what-should-i-do-in-lockdown.html
ReplyDeleteOnline Earning