In C, while loop is defined as the conditional statement which allows to run the blocks of code in a defined number or infinite number of times. Basically the while loop is used to run the run in repetitive times until the condition is true.
Syntax:
while(condition)
{ // initialization
blocks of Code
// updation
}
Algorithm:
Step 1: Start
Step 2: Initialize the value
Step 3: check the condition
Step 4: If true, execute the statements and repeat from step 3
#include <stdio.h>
int main()
{
int i=1; //intialization
while(i<=5) {
printf("PrepInsta\n");
i++; //updation
}
return 0;
}
Output:
PrepInsta
PrepInsta
PrepInsta
PrepInsta
PrepInsta
Working of while loop
Initialization:
It is the starting statement of the while loop which accepts the initialization of expression.
Conditional Statement:
A conditional statement determines whether the block of code will be executed or not.
When the condition is satisfied, the loop will end. It will continue to test the condition and check until the block of code under the while loop is satisfied.
Body:
It is the main part of the code which needs to be executed in every iteration for performing the given condition.
It refers to a collection of statements which includes variable functions etc.
Updation:
It is the statement of the while loop which includes the increment and decrement expression according to the conditional statement.
Login/Signup to comment