while loops keep running their code for as long as the condition stays true. They're handy when you don't know ahead of time how many times something needs to run — like repeatedly asking a user for the correct password, or reading a file until you hit the end.
c
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
printf("%d
", i);
i++;
}
return 0;
}At first, i is 0. Every time the loop runs, i++ bumps it up by 1. i < 5 is no longer true, the loop stops.
You should see
0 1 2 3 4Info
There needs to be an update inside the loop that eventually makes the condition false. In this example, that's i++.