C While Loop
A C while loop is a powerful programming construct used to execute a block of code repeatedly as long as a certain condition remains true. It's a fundamental building block for controlling program flow and performing repetitive tasks.
It's a pre-tested loop, meaning the condition is checked before each iteration, and the loop body is only executed if the condition holds true.
While Loop
Here's a more detailed explanation of the while loop in C programming:
Syntax
while (condition) {
// code block to be executed
}
Example
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Output
0
1
2
3
4
Note: you need to modify the variable used in the condition to eventually make it false. The most common way is to increment it (e.g., i++). Termination: When the condition becomes false, the loop stops executing.