In this tutorial we learn about what is loop statement , how loop statement works in the program , in the case you want to repeated a some statements in the program for condition you use loop statement
Looping Statements:-
What is Loop in C?
Looping Statements in C execute the sequence of statements many times until the stated condition becomes false. A loop in C consists of two parts, a body of a loop and a control statement.
Definition of Loop:-
in loop ,a program executes the sequence/block of statements many times until the stated condition become false.There are three types of loops available in C :-
While:-
In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed.
in while loop in c first check the condition if condition is true control goes to inside the while loop, execute the loop body statement. But condition is false the control goes to outside the body.
Syntax:-
while (Condition)
{
Loop body; // Statement;
increment or decrement (++ , --);
}
If while loop condition never false then loop become infinite loop.
Ex.
Program for reverse Integer:
in while loop in c first check the condition if condition is true control goes to inside the while loop, execute the loop body statement. But condition is false the control goes to outside the body.
Syntax:-
while (Condition)
{
Loop body; // Statement;
increment or decrement (++ , --);
}
If while loop condition never false then loop become infinite loop.
Ex.
Program for reverse Integer:
#include<studio.h>
{
int i=10;
while(i<=10)
{
printf("%d \n",i);
i++;
}
return 0;
}
for loop:-
In a for loop, the initial value is performed only once, then the condition tests and compares the counter to a fixed value after each iteration, stopping the for loop when false is returned.
for is a keyword.in c programming language for loop is used to repeat a block of statements for a given number of times until the given condition is false.
Syntax:-
The syntax of the for loop:-
for(initialization; test condition; increment/decrement operator)
{
//Statement 1;
//Statement 2;
//Statement 3;
//Statement n;
}
in for loop has the expressions separated by the semi-colons (denote by " ; " symbol ).
The flow of control ike
Initialization:-
in for loop start with the initialization statement. The initialization part or section is expected only once at the beginning at and initialization of counters variable is done first or set counter value.Test Condition:-
The value of the counter variable tested against the test Condition.Test Condition is true.it will execute the statement inside the loop, if the condition is false the for loop will be skips or terminate.
Increment / Decrement:-
In this expression executed after the end of each iteration. This operation helps to increase or decrease the counter variable as per requirement.In for loop condition is always true in such case the loop will ran infinite times.
Flowchart:-
Ex.
#include<studio.h>
int main()
{
For(i=0; i>0; i++)
{
printf("Saurabh1999.blogspot.com");
}
Ex.
#include<studio.h>
int main()
{
int i=10;
For(i=10; i>0; i--)
{
printf("%d \n", i);
}
return 0;
}
OUTPUT:-
10
9
8
7
6
5
4
3
2
1
do.... while loop :-
In a do...while loop, the condition is always executed after the body of a loop. It is also called an exit-controlled loop.
The do... while loop is similar to the while loop with one main difference. The body of do... while loop is expected at least once.Irrespective or test condition.
Syntax:-
do
{
// Loop body;
//Expressions;
}
while (expression);
Flowchart :-
Ex.
#include<studio.h>
int main()
{
int i=0;
do
{
printf ("Hi Friends");
i++;
}
while (i<1);
return 0;
}