Example of Iteration in C
The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language. The Iterations can be able to modify the control flow of the program thus they referred as “Control Statements.”
Loops:
The Continuous repetition of statements until a condition is satisfies are called loops.
The loops consist of initialization, condition and updating. In initialization, a variable is declared a variable with a value. In Condition, a specific condition is provided so that the repetition of the code is controlled to specific number of times. In Updating, the value which is initialized earlier is being increased/ decreased according to the condition.
The Loops are classified into two types:
- Pre-test Loop.
- Post-Test Loop.
Pre-Test Loop:
In the Pre-Test Loop, the checking of the condition whether its true or false is done at the start of loop and then enters the loop body.
The Pre-Test Loops are:
While Loop:
In while loop, the condition is checked first and allows the flow of execution into the loop body if the condition is true. The Initialization of the variable in while loop id done before creating a loop body. The Condition is mentioned after the “while” keyword in the Parenthesis i.e., (). The Updating is done inside Loop block.
Syntax:
while(condition)
{
//Loop statements and Updating(Increasing/ Decreasing)
}
Example:
//Program for while loop
#include<stdio.h>
#include<conio.h>
void main (void)
{
int n=3 , i=0; // initialization
while(i<n) //conditon
{
printf(“Loop Executed successfully\n”);
i=i+1; // Updating
}
}
Output:
Loop Executed Successfully
Loop Executed Successfully
Loop Executed Successfully
For Loop:
The For loop is ideal among all the loops as, the initialization, condition and updating can be done in a single line.
Syntax:
for(initialization; condition ; updating )
{
// statements
}
Example:
// Program for “for Loop”
#include<stdio.h>
#include<conio.h>
void main (void)
{
int i, n =3;
for(i=0;i<n;i++)
{
printf(“For Loop Successfully Executed\n”);
}
}
Output:
For Loop Successfully Executed
For Loop Successfully Executed
For Loop Successfully Executed
Post-Test Loop:
In the post test loop, the loop condition is checked after execution of block of code.
The Pre-test loops are:
Do While Loop:
The Do-while loop is same as while loop but the block of code is executed at least once before checking the loop condition. The initialization is done outside the loop body. The condition is written after the block of code. The Increment/ Decrement (updating) is done inside the block.
Syntax:
do
{
//statements
} while(Condition);
Example:
// Program for do-while loop
#include<stdio.h>
#include<conio.h>
void main (void)
{
int i , n=3; // initialization
do
{
printf(“do-while loop successfully executed\n”);
i++; // updation
} while(i<3); // conditon
}
Output:
do-while loop successfully executed
do-while loop successfully executed
do-while loop successfully executed
Summary:
The iterations in c language are the statements which are executed multiple number of times until a condition is reached which is referred as loops. These loops are of two types of Pre-test and Post-test loops. The Pre-test loops are “For Loop” and “While Loop”. The Post-Test Loops are “Do-while Loop”.