A while
loop will evaluate its condition, and if true
, it will execute the code inside and start over. That is, as long as its condition evaluates to true
, the while
loop will execute over and over.
This loop will execute 100 times, each time adding 1 to the variable num
:
int num = 0;
while (num < 100) {
// do something
num++;
}
The above loop is equivalent to a for
loop:
for (int i = 0; i < 100; i++) {
// do something
}
This loop will execute forever:
while (true) {
// do something
}
The above loop is equivalent to a for
loop:
for (;;) {
// do something
}