Subscribe to MentalFloss Minutes

## Loops

For this post we shall consider Java, but it will apply equally well to C++.

Knowing what loop to use for a given situation is an important skill. There are many options, and picking the right one can be confusing at first.

for(type var_name = value; condition; increment)

Whenever we have to count something, a for loop is a great choice. We also might want to maintain an index to an array or some other collection. For loops are very versatile too. We might increment/decrement or even increase by some number. For example, if you want to list all numbers divisible by three up to some value, you could do a standard for loop, and check its modulus:

```for(int i = 0; i <= 100; i++)
{
if( i % 3 == 0) // remainder of zero, so divisible by 3.
{
System.out.println(i);
}
}

```

But we can also do a simple for loop that increments by our divisible number.

```for(int i = 0; i <= 100; i+=3)
System.out.println(i);

```

for(type var_name : collection)

A for each loop is useful for when we want to iterate over a collection of items, and we do not care about maintaining an index. The problem with not using a for each when you should is that you might go outside the bounds of the collection by mistake, but this will not happen in a for each loop.

```String[] data = { "one", "two", "three", "four" };

for(int i = 0; i < data.length; i++)
System.out.println(data[i]);

```

Can be written more safely as:

```String[] data = { "one", "two", "three", "four" };

for(String entry : data)
System.out.println(entry);

```

while(condition)

A while loop is great for looping over a block of code 0 or more times where you do not know the upper limit ahead of time. There must be some condition that is satisfied, and until it is not, the loop will run. We might try to implement this as an infinite for loop:

```for(;;) {
/* doing things */

// some condition to be met:
if(!isValid)
break;
}

```

But we see that this is much better suited as a while loop.

```while(isValid) {
/* doing things */
}

```

do { } while (condition);

Do-while loops might not get a lot of use, but any time that you must run some block of code at least once, a do-while loop is a good choice. A good example would be a menu prompt in a console application. You know for certain that the menu must print at least once, so it makes some sense to use a do-while.

```boolean quit = false;
do {
/* get user choice */

quit = choice.equals("Q");

} while (!quit);

```

Recursion

This is a very special loop where a function calls itself with a smaller piece of its given problem until the problem can be reduced no further, and upon rejoining everything together, provides an answer to the main problem.

They are very challenging to get correct, and most of the cases where you need to use it have already been discovered. Eventually, you'll branch out to problems that don't have ready solutions, but they look close enough to the problems you've already solved/learned that they can be tailored to fit your needs.

S M T W T F S
1
234567 8
9101112131415
16171819202122
23242526272829
3031