You may encounter
situations, when a block of code needs to be executed several number of times. A loop is used for executing a block of statements repeatedly
until a given condition returns false.
There are three types of loop
constructs in C. These are as follows-
(1). for loop
(2). while loop
(3). do while loop
(1).for
loop
It is used mainly when number of
iterations (number of times loop will execute) are known beforehand.
Syntax of for loop
The for loop can be written in
several ways.
Syntax 1:
for(initialization;test_condition;updateStatement){
//loop body
}
Syntax 2:
Initialization;
for(;test_condition;updateStatement){
//loop body
}
Syntax 3:
initialization
for(;test_condition;){
//loop body
updateStatement;
}
Note
:
updateStatement means increment or decrement statement
How for loop works?
- The initialization statement is executed only once.
- Then, the test_condition is evaluated. If the test_condition is false (0), for loop is terminated.
- But if the test_condition is true (nonzero), codes inside the body of for loop is executed and the updateStatement is updated.
This
process repeats until the test_condition is false.
Examples:
//program to print integer number from 1
to 10 in ascending order
#include<stdio.h>
void main(){
for(int i=1;i<=10;i++){
printf(“%d\t”,i);
}
}
Output: 1 2 3 4 5 6 7 8
9 10
//program to print integer number from 1
to 10 in descending order
#include<stdio.h>
void main(){
for(int i=10;i>=1;i--){
printf(“%d\t”,i);
}
}
Output: 10 9 8 7 6 5 4 3
2 1
(2).
While loop
The while statement is used
when the program needs to perform repetitive tasks.
The general form of a while statement
is:
while (condition){
statement;
}
The program will repeatedly execute
the statement inside the while until the condition becomes false (0).
(If the condition is initially false, the statement will not be executed.)
- The while loop evaluates the condition.
- If the condition is true (nonzero), codes inside the body of while loop is executed. The condition is evaluated again. The process goes on until the condition is false.
- When the condition is false, the while loop is terminated.
Example
:
//program to print numbers from 1 to 10 in
ascending order
#include<stdio.h>
void main(){
int i=1;
while(i<=10){
printf(“%d\t”,i);
i++;
}
}
(3).do
while loop
The do..while loop
is similar to the while loop with one important difference. The body
of do...while loop is executed once, before checking the test
expression. Hence, the do...while loop is executed at least once.
Syntax:
initialization;
do{
//loop body
updateExpression;
}while(testExpression);
How do...while loop works?
- The code block (loop body) inside the braces is executed once.
- Then, the testExpression is evaluated. If the testExpression is true, the loop body is executed again. This process goes on until the testExpression is evaluated to 0 (false).
- When the testExpression is false (nonzero), the do...while loop is terminated.
·
Example:
//program to print numbers from 1 to 10.
#include<stdio.h>
void main(){
int i=1;
do{
printf(“%d\t”,i);
i++;
}while(i<=10);
}
Above discussed loops can be
categorized into two groups based on-when test condition is tested.
(1).
Entry controlled loop
- The types of loop where the test condition is stated before the body of the loop, are known as the entry controlled loop.
- In this category of loops, test condition is evaluated before entering into the loop body.
- Example- for loop and while loop.
(2).
Exit controlled loop
- The types of loop where the test condition is stated at the end of the body of the loop, are known as the exit controlled loops.
- So, in the case of the exit controlled loops, the body of the loop gets execution without testing the given condition for the first time.
- Then the condition is tested. If it comes true, then the loop gets another execution and continues till the result of the test condition is not false.
- Example : do while loop
Please
comment if you find anything incorrect, or you want to improve the topic
discussed above.