We can say two loop constructs are equally powerful if you can write one in terms of the other.
Here's the syntax for a while loop.
while ( cond ) while bodyThis is easily rewritten as a for loop.
for ( ; cond ; ) while bodyTo emulate the while loop:
for ( init ; cond ; update ) for bodyThink about what it takes to emulate this using a while loop.
init ; while ( cond ) { for body update }This is almost equivalent to a for loop. The only time it isn't is if the for body has a continue statement.
If it were in a for loop, the continue statement would jump to the update. However, when translated to while loop, it jumps to the condition.
break statements are fine in for bodies, since they exit the loop. They also exit the loop when they are in the while body.
Find a simple for loop example. Translate it to a while loop as shown above, and see if the two behave the same.
Recall the syntax of the do while loop.
do do-while body while ( cond ) ;Recall that a do while loop executes the do-while body at least once. A while loop may not execute any iterations.
To force at least one iteration of the loop body, we copy it.
do-while body while ( cond ) do-while bodyThis translation doesn't work if there's a break in the do-while body. In fact, it won't compile since break must appear in a loop. The translation also doesn't work if there's a continue statement in the do-while body for the same reason.