Loops can be written as while
loops, do-while
loops, and for
loops.
while
loops iterate until a condition is met.
while (a < 10) {a++;}
do-while
loops are while
loops that initially execute the body once before checking the condition.
do {printf("not true!");} (while 2 == 3);
for
loops complete a set number of iterations before meeting a condition.
for (int i = 0; i <= 10; i++) {printf("Hello!");}
All loops can utilize keywords like continue
and break
. continue
restarts the loop and break
breaks out of (or ends) the loop.
A for
loop can always be re-written as a while
loop; most while
loops can be re-written as a for
loop.