Tutorial by Examples

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 (nu...

For

for loops are simplified syntax for a very common loop pattern, which could be accomplished in more lines with a while loop. The following is a common example of a for loop, which will execute 100 times and then stop. for (int i = 0; i < 100; i++) { // do something } This is equivalen...
A do while loop is the same as a while loop, except that it is guaranteed to execute at least one time. The following loop will execute 100 times. int i = 0; do { i++; } while (i < 100); A similar loop, but with a different condition, will execute 1 time. int i = 0; do { i++; ...
There are some ways to break or change a loop's flow. break; will exit the current loop, and will not execute any more lines within that loop. continue; will not execute any more code within the current iteration of the loop, but will remain in the loop. The following loop will execute 101 times ...

Page 1 of 1