Download

Understanding while and do-while

The while loop has a form similar to the if conditional:while(condition){statement1statement2…}As long as condition holds, the block of statements will be repeatedly executed. If there is only one statement, the curly braces may be omitted.

do{statement1statement2…}while(condition);The block of statements is executed and then, if the condition holds, the program returns to the top of the block. Curly braces are always required. Also note the semicolon after the while condition.