![]() |
VOOZH | about |
The while loop in C allows a block of code to be executed repeatedly as long as a given condition remains true. It is often used when we want to repeat a block of code till some condition is satisfied.
GfG GfG GfG GfG GfG
Explanation: The above loop prints the text “GfG” till the given condition is true i.e. i is less than 5. In each execution of the loop’s statement, it increments i till i is less than 5.
where,
Let's understand the working of while loop in C using the flowchart given below:
We can understand the working of the while loop by looking at the above flowchart:
The below examples show how to use a while loop in a C program:
55
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
Explanation: In the above program, one while loop is nested inside anther while loop to print each value in the table. This is called nesting of loops and why can nest as many while loops as we want in in C.
An infinite while loop is created when the given condition always remains true. It is generally encountered in programs where, the condition variable is not correctly updated or has some logic error.
Output
GfG
GfG
.
.
infinite
As seen in the above example, the loop will continue till infinite because the loop variable will always remain the same resulting in the condition that is always true.