Java Programming/Loop blocks

Loops are a handy tool that enables programmers to do repetitive tasks with minimal effort. Say we want a program that can count from 1 to 10, we could write the following program.

The task will be completed just fine, the numbers 1 to 10 will be printed in the output, but there are a few problems with this solution:
 * Flexibility: what if we wanted to change the start number or end number? We would have to go through and change them, adding extra lines of code where they're needed.
 * Scalability: 10 repeats are trivial, but what if we wanted 100 or even 1000 repeats? The number of lines of code needed would be overwhelming for a large number of iterations.
 * Maintenance: where there is a large amount of code, one is more likely to make a mistake.
 * Feature: the number of tasks is fixed and doesn't change at each execution.

Using loops we can solve all these problems. Once you get you head around them they will be invaluable to solving many problems in programming.

Open up your editing program and create a new file saved as. Now type or copy the following code:

If we run the program, the same result is produced, but looking at the code, we immediately see the advantages of loops. Instead of executing ten different lines of code, line 5 executes ten times. Ten lines of code have been reduced to just four. Furthermore, we may change the number 10 to any number we like. Try it yourself, replace the 10 with your own number.

While
loops are the simplest form of loop. The  loop repeats a block of code while the specified condition is true. Here is the structure of a  loop:

The loop's condition is checked before each iteration of the loop. If the condition is false at the start of the loop, the loop will not be executed at all. The code section 3.28 sets in  the smallest integer whose square exceeds 200.

Do... while
The - loop is functionally similar to the  loop, except the condition is evaluated AFTER the statement executes It is useful when we try to find a data that does the job by randomly browsing an amount of data.

For
The loop is a specialized   loop whose syntax is designed for easy iteration through a sequence of numbers. It consists of the keyword  followed by three extra statements enclosed in parentheses. The first statement is the variable declaration statement, which allows you to declare one or more integer variables. The second is the condition, which is checked the same way as the  loop. Last is the iteration statement, which is used to increment or decrement variables, though any statement is allowed.

This is the structure of a  loop:

To clarify how a for loop is used, here is an example:

The  loop is like a template version of the   loop. The alternative code using a  loop would be as follows:

The code section 3.31 shows how to iterate with the  loop using multiple variables and the code section 3.32 shows how any of the parameters of a   loop can be skipped. Skip them all, and you have an infinitely repeating loop.

For-each
Arrays haven't been covered yet, but you'll want to know how to use the enhanced for loop, called the  loop. The  loop automatically iterates through a list or array and assigns the value of each index to a variable.

To understand the structure of a  loop, look at the following example:

The example iterates through an array of words and prints them out like a sentence. What the loop does is iterate through  and assign the value of each index to , then execute the code block.

Here is the general contract of the  loop:

Make sure that the type of the array or list is assignable to the declared variable, or you will get a compilation error. Notice that the loop automatically exits after the last item in the collection has been examined in the statement block.

Although the enhanced for loop can make code much clearer, it can't be used in some common situations.


 * Only access. Elements can not be assigned to, e.g., not to increment each element in a collection.
 * Only single structure. It's not possible to traverse two structures at once, e.g., to compare two arrays.
 * Only single element. Use only for single element access, e.g., not to compare successive elements.
 * Only forward. It's possible to iterate only forward by single steps.
 * At least Java 5. Don't use it if you need compatibility with versions before Java 5.

Break and continue keywords
The keyword exits a flow control loop, such as a for loop. It basically breaks the loop.

In the code section 3.34, the loop would print out all the numbers from 1 to 10, but we have a check for when  equals 5. When the loop reaches its fifth iteration, it will be cut short by the  statement, at which point it will exit the loop.

The keyword jumps straight to the next iteration of a loop and evaluates the boolean expression controlling the loop. The code section 3.35 is an example of the  statement in action:

As the  and   statements reduce the readability of the code, it is recommended to reduce their use or replace them with the use of   and   blocks. Some IDE refactoring operations will fail because of such statements.

Question 3.2: Consider the following code:

What will be printed in the standard output?

The snippet is searching the 5th prime number, that is to say: 11. It iterates on each positive integer from 2 (2, 3, 4, 5, 6, 7, 8, 9, 10, 11...), among them, it counts the prime numbers (2, 3, 5, 7, 11) and it stops at the 5th one.

So the snippet first iterates on each positive integer from 2 using the  loop:

For each iteration, the current number is either a prime number or not. If it is a prime number, the code at the left will be executed. If it is not a prime number, the code at the right will be executed.

The prime numbers are counted using. When  is equal to   (5), the program go out of the   loop. contains the last number, that is to say the 5th prime number:

Labels
Labels can be used to give a name to a loop. The reason to do this is so we can break out of or continue with upper-level loops from a nested loop.

Here is how to label a loop:

To break out of or continue with a loop, use the  or   keyword followed by the name of the loop.

For example:

You needn't worry if you don't understand all the code, but look at how the label is used to break out of the outer loop from the inner loop. However, as such a code is hard to read and maintain, it is highly recommended not to use labels.

Try... catch blocks

 * See also Throwing and Catching Exceptions.

The - blocks are used to catch any exceptions or other throwable objects within the code.

Here's what  blocks looks like:

The code listing 3.6 tries to print all the arguments that have been passed to the program. However, if there not enough arguments, it will throw an exception.

In addition to the try and catch blocks, a block may be present. The finally block is always executed, even if an exception is thrown. It may appear with or without a catch block, but always with a try block.

Here is what a finally block looks like:

Examples
The code listing 3.7 receives a number as parameter and print its binary representation.

The code listing 3.8 is a simulation of playing a game called Lucky Sevens. It is a dice game where the player rolls two dice. If the numbers on the dice add up to seven, he wins $4. If they do not, he loses $1. The game shows how to use control flow in a program as well as the fruitlessness of gambling.