Loop blocks

From Wikibooks, open books for an open world
Jump to: navigation, search

Conditional blocks Java Programming
Loop blocks
Boolean expressions
Navigate Language Fundamentals topic: v  d  e )


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.

Computer code Code listing 3.4: Count.java
  1. class Count {
    
  2.     public static void main(String[] args) {
    
  3.         System.out.println('1 ');
    
  4.         System.out.println('2 ');
    
  5.         System.out.println('3 ');
    
  6.         System.out.println('4 ');
    
  7.         System.out.println('5 ');
    
  8.         System.out.println('6 ');
    
  9.         System.out.println('7 ');
    
  10.         System.out.println('8 ');
    
  11.         System.out.println('9 ');
    
  12.         System.out.println('10 ');
    
  13.     }
    
  14. }
    
Standard input or output Output for code listing 3.4
1
2
3
4
5
6
7
8
9
10


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 Loop.java. Now type or copy the following code:

Computer code Code listing 3.5: Loop.java
  1. class Loop {
    
  2.     public static void main(String[] args) {
    
  3.         int i;
    
  4.         for (i = 1; i <= 10; i++) {
    
  5.             System.out.println(i + ' ');
    
  6.         }
    
  7.     }
    
  8. }
    
Standard input or output Output for code listing 3.5
1
2
3
4
5
6
7
8
9
10

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 10 different lines of code, line 5 executes ten times. 10 lines of code have been reduced to just 4. Furthermore, we may change the number 10 to any number we like. Try it yourself, replace the 10 with your own number.

While[edit]

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

while (boolean expression1) {
statement1
statement2
...
statementn

}

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 squareHigherThan200 the smallest integer whose square exceeds 200.

Example Code section 3.28: The smallest integer whose square exceeds 200.
  1. int squareHigherThan200 = 0;
    
  2.  
    
  3. while (squareHigherThan200 * squareHigherThan200 < 200) {
    
  4.   squareHigherThan200 = squareHigherThan200 + 1;
    
  5. }
    
Note If a loop's condition will never become false, such as if the true constant is used for the condition, said loop is known as an infinite loop. Such a loop will repeat indefinitely unless it is broken out of. Infinite loops can be used to perform tasks that need to be repeated over and over again without a definite stopping point, such as updating a graphics display.

Do... while[edit]

The do-while loop is functionally similar to the while 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.

do {
statement1
statement2
...
statementn

} while (boolean expression1);

For[edit]

The for loop is a specialized while loop whose syntax is designed for easy iteration through a sequence of numbers. It consists of the keyword for 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 while 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 for loop:

for (variable declarations; condition; iteration statement) {
statement1
statement2
...
statementn

}

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

Example Code section 3.29: A for loop.
  1. for (int i = 1; i <= 10; i++) {
    
  2.     System.out.println(i);
    
  3. }
    
Standard input or output Output for code listing 3.29
1
2
3
4
5
6
7
8
9
10

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

Example Code section 3.30: An iteration using a while loop.
  1. int i = 1;
    
  2. while (i <= 10) {
    
  3.   System.out.println(i);
    
  4.   i++;
    
  5. }
    

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

Example Code section 3.31: The for loop using multiple variables.
  1. for (int i = 1, j = 10; i <= 10; i++, j--) {
    
  2.   System.out.print(i + " ");
    
  3.   System.out.println(j);
    
  4. }
    
Example Code section 3.32: The for loop without parameter.
  1. for (;;) {
    
  2.   // Some code
    
  3. }
    

For-each[edit]

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

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

Example Code section 3.33: A for-each loop.
  1. String[] sentence = {"I", "am", "a", "Java", "program."};
    
  2. for (String word : sentence) {
    
  3.     System.out.print(word + " ");
    
  4. }
    
Standard input or output Output for code section 3.33
I am a Java program.

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

Here is the general contract of the for-each loop:

for (variable declaration : array or list) {
statement1
statement2
...
statementn

}

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, eg, not to increment each element in a collection.
  • Only single structure. It's not possible to traverse two structures at once, eg, to compare two arrays.
  • Only single element. Use only for single element access, eg, 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[edit]

The break 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 i equals 5. When the loop reaches its fifth iteration, it will be cut short by the break statement, at which point it will exit the loop.

Example Code section 3.34: An interrupted for loop.
  1. for (int i = 1; i <= 10; i++) {
    
  2.     System.out.println(i);
    
  3.     if (i == 5) {
    
  4.        System.out.println("STOP!");
    
  5.        break;
    
  6.     }
    
  7. }
    
Standard input or output Output for code section 3.34
1
2
3
4
5
STOP!

The continue 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 continue statement in action:

Example Code section 3.35: A for loop with a skipped iteration.
  1. for (int i = 1; i <= 10; i++) {
    
  2.     if (i == 5) {
    
  3.         System.out.println("Caught i == 5");
    
  4.         continue;
    
  5.     }
    
  6.     System.out.println(i);
    
  7. }
    
Standard input or output Output for code section 3.35
1
2
3
4
Caught i == 5
6
7
8
9
10

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

Test your knowledge

Question 3.2: Consider the following code:

Example Question 3.2: Loops and conditions.
  1. int numberOfItems = 5;
    
  2. int currentItems = 0;
    
  3. int currentCandidate = 1;
    
  4.  
    
  5. while (currentItems < numberOfItems) {
    
  6.   currentCandidate = currentCandidate + 1;
    
  7.   System.out.println("Test with integer: " + currentCandidate);
    
  8.  
    
  9.   boolean found = true;
    
  10.   for (int i = currentCandidate - 1; i > 1; i--) {
    
  11.  
    
  12.     // Test if i is a divisor of currentCandidate
    
  13.     if ((currentCandidate % i) == 0) {
    
  14.       System.out.println("Not matching...");
    
  15.       found = false;
    
  16.       break;
    
  17.     }
    
  18.  
    
  19.   }
    
  20.  
    
  21.   if (found) {
    
  22.     System.out.println("Matching!");
    
  23.     currentItems = currentItems + 1;
    
  24.   }
    
  25. }
    
  26.  
    
  27. System.out.println("Find the value: " + currentCandidate);
    

What will be printed in the standard output?

Answer
Standard input or output Output for Question 3.2
Test with integer: 2
Matching!
Test with integer: 3
Matching!
Test with integer: 4
Not matching...
Test with integer: 5
Matching!
Test with integer: 6
Not matching...
Test with integer: 7
Matching!
Test with integer: 8
Not matching...
Test with integer: 9
Not matching...
Test with integer: 10
Not matching...
Test with integer: 11
Matching!
Find the value: 11

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 while loop:

Example Answer 3.2.1: while loop.
  1. int numberOfItems = 5;
    
  2. int currentItems = 0;
    
  3. int currentCandidate = 1;
    
  4.  
    
  5. while (currentItems < numberOfItems) {
    
  6.   currentCandidate = currentCandidate + 1;
    
  7.   System.out.println("Test with integer: " + currentCandidate);
    
  8.  
    
  9.   boolean found = true;
    
  10.   for (int i = currentCandidate - 1; i > 1; i--) {
    
  11.  
    
  12.     // Test if i is a divisor of currentCandidate
    
  13.     if ((currentCandidate % i) == 0) {
    
  14.       System.out.println("Not matching...");
    
  15.       found = false;
    
  16.       break;
    
  17.     }
    
  18.  
    
  19.   }
    
  20.  
    
  21.   if (found) {
    
  22.     System.out.println("Matching!");
    
  23.     currentItems = currentItems + 1;
    
  24.   }
    
  25. }
    
  26.  
    
  27. System.out.println("Find the value: " + currentCandidate);
    

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.

Example Answer 3.2.2: A prime number.
  1. int numberOfItems = 5;
    
  2. int currentItems = 0;
    
  3. int currentCandidate = 1;
    
  4.  
    
  5. while (currentItems < numberOfItems) {
    
  6.   currentCandidate = currentCandidate + 1;
    
  7.   System.out.println("Test with integer: " + currentCandidate);
    
  8.  
    
  9.   boolean found = true;
    
  10.   for (int i = currentCandidate - 1; i > 1; i--) {
    
  11.  
    
  12.     // Test if i is a divisor of currentCandidate
    
  13.     if ((currentCandidate % i) == 0) {
    
  14.       System.out.println("Not matching...");
    
  15.       found = false;
    
  16.       break;
    
  17.     }
    
  18.  
    
  19.   }
    
  20.  
    
  21.   if (found) {
    
  22.     System.out.println("Matching!");
    
  23.     currentItems = currentItems + 1;
    
  24.   }
    
  25. }
    
  26.  
    
  27. System.out.println("Find the value: " + currentCandidate);
    
Example Answer 3.2.3: Not a prime number.
  1. int numberOfItems = 5;
    
  2. int currentItems = 0;
    
  3. int currentCandidate = 1;
    
  4.  
    
  5. while (currentItems < numberOfItems) {
    
  6.   currentCandidate = currentCandidate + 1;
    
  7.   System.out.println("Test with integer: " + currentCandidate);
    
  8.  
    
  9.   boolean found = true;
    
  10.   for (int i = currentCandidate - 1; i > 1; i--) {
    
  11.  
    
  12.     // Test if i is a divisor of currentCandidate
    
  13.     if ((currentCandidate % i) == 0) {
    
  14.       System.out.println("Not matching...");
    
  15.       found = false;
    
  16.       break;
    
  17.     }
    
  18.  
    
  19.   }
    
  20.  
    
  21.   if (found) {
    
  22.     System.out.println("Matching!");
    
  23.     currentItems = currentItems + 1;
    
  24.   }
    
  25. }
    
  26.  
    
  27. System.out.println("Find the value: " + currentCandidate);
    

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

Example Answer 3.2.4: End of the program.
  1. int numberOfItems = 5;
    
  2. int currentItems = 0;
    
  3. int currentCandidate = 1;
    
  4.  
    
  5. while (currentItems < numberOfItems) {
    
  6.   currentCandidate = currentCandidate + 1;
    
  7.   System.out.println("Test with integer: " + currentCandidate);
    
  8.  
    
  9.   boolean found = true;
    
  10.   for (int i = currentCandidate - 1; i > 1; i--) {
    
  11.  
    
  12.     // Test if i is a divisor of currentCandidate
    
  13.     if ((currentCandidate % i) == 0) {
    
  14.       System.out.println("Not matching...");
    
  15.       found = false;
    
  16.       break;
    
  17.     }
    
  18.  
    
  19.   }
    
  20.  
    
  21.   if (found) {
    
  22.     System.out.println("Matching!");
    
  23.     currentItems = currentItems + 1;
    
  24.   }
    
  25. }
    
  26.  
    
  27. System.out.println("Find the value: " + currentCandidate);
    

Labels[edit]

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:

label name:loop

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

For example:

Example Code section 3.36: A double for loop.
  1. int i, j;
    
  2. int[][] nums = {
    
  3.     {1, 2, 5},
    
  4.     {6, 9, 7},
    
  5.     {8, 3, 4}
    
  6. };
    
  7.  
    
  8. Outer:
    
  9. for (i = 0; i < nums.length; i++) {
    
  10.     for (j = 0; j < nums[i].length; j++) {
    
  11.         if (nums[i][j] == 9) {
    
  12.             System.out.println("Found number 9 at (" + i + ", " + j + ")");
    
  13.             break Outer;
    
  14.         }
    
  15.     }
    
  16. }
    
Standard input or output Output for code section 3.36
Found number 9 at (1, 1)

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[edit]

See also Throwing and Catching Exceptions.

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

Here's what try-catch blocks looks like:

try {
statement1.1
statement1.2
...
statement1.n

} catch (exception1) {

statement2.1
...
statement2.n

}

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.

Computer code Code listing 3.6: Attempt.java
  1. public class Attempt {
    
  2.   public static void main(String[] args) {
    
  3.     try {
    
  4.       System.out.println(args[0]);
    
  5.       System.out.println(args[1]);
    
  6.       System.out.println(args[2]);
    
  7.       System.out.println(args[3]);
    
  8.     } catch (ArrayIndexOutOfBoundsException e) {
    
  9.       System.out.println("No enough arguments");
    
  10.     }
    
  11.   }
    
  12. }
    

In addition to the try and catch blocks, a finally 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:

try {
statement1.1
statement1.2
...
statement1.n

} catch (exception1) {

statement2.1
...
statement2.n

} finally {

statement3.1
...
statement3.n

}

Examples[edit]

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

Computer code Code listing 3.7: GetBinary.java
  1. public class GetBinary {
    
  2.     public static void main(String[] args) {
    
  3.         if (args.length == 0) {
    
  4.             // Print usage
    
  5.             System.out.println("Usage: java GetBinary <decimal integer>");
    
  6.             System.exit(0);
    
  7.         } else {
    
  8.             // Print arguments
    
  9.             System.out.println("Received " + args.length + " arguments.");
    
  10.             System.out.println("The arguments are:");
    
  11.             for (String arg : args) {
    
  12.                 System.out.println("\t" + arg);
    
  13.             }
    
  14.         }
    
  15.  
    
  16.         int number = 0;
    
  17.         String binary = "";
    
  18.  
    
  19.         // Get the input number
    
  20.         try {
    
  21.             number = Integer.parseInt(args[0]);
    
  22.         } catch (NumberFormatException ex) {
    
  23.             System.out.println("Error: argument must be a base-10 integer.");
    
  24.             System.exit(0);
    
  25.         }
    
  26.  
    
  27.         // Convert to a binary string
    
  28.         do {
    
  29.             switch (number % 2) {
    
  30.                 case 0: binary = '0' + binary; break;
    
  31.                 case 1: binary = '1' + binary; break;
    
  32.             }
    
  33.             number >>= 1;
    
  34.         } while (number > 0);
    
  35.  
    
  36.         System.out.println("The binary representation of " + args[0] + " is " + binary);
    
  37.     }
    
  38. }
    

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.

Computer code Code listing 3.8: LuckySevens.java
  1. import java.util.*;
    
  2.  
    
  3. public class LuckySevens {
    
  4.     public static void main(String[] args) {
    
  5.         Scanner in = new Scanner(System.in);
    
  6.         Random random = new Random();
    
  7.         String input;
    
  8.         int startingCash, cash, maxCash, rolls, roll;
    
  9.  
    
  10.         // Loop until "quit" is input
    
  11.         while (true) {
    
  12.             System.out.print("Enter the amount of cash to start with (or \"quit\" to quit): ");
    
  13.  
    
  14.             input = in.nextLine();
    
  15.  
    
  16.             // Check if user wants to exit
    
  17.             if (input.toLowerCase().equals("quit")) {
    
  18.                 System.out.println("\tGoodbye.");
    
  19.                 System.exit(0);
    
  20.             }
    
  21.  
    
  22.             // Get number
    
  23.             try {
    
  24.                 startingCash = Integer.parseInt(input);
    
  25.             } catch (NumberFormatException ex) {
    
  26.                 System.out.println("\tPlease enter a positive integer greater than 0.");
    
  27.                 continue;
    
  28.             }
    
  29.  
    
  30.             // You have to start with some money!
    
  31.             if (startingCash <= 0) {
    
  32.                 System.out.println("\tPlease enter a positive integer greater than 0.");
    
  33.                 continue;
    
  34.             }
    
  35.  
    
  36.             cash = startingCash;
    
  37.             maxCash = cash;
    
  38.             rolls = 0;
    
  39.             roll = 0;
    
  40.  
    
  41.             // Here is the game loop
    
  42.             for (; cash > 0; rolls++) {
    
  43.                roll = random.nextInt(6) + 1;
    
  44.                roll += random.nextInt(6) + 1;
    
  45.  
    
  46.                 if (roll == 7)
    
  47.                     cash += 4;
    
  48.                 else
    
  49.                     cash -= 1;
    
  50.  
    
  51.                 if (cash > maxCash)
    
  52.                     maxCash = cash;
    
  53.             }
    
  54.  
    
  55.             System.out.println("\tYou start with $" + startingCash + ".\n"
    
  56.                     + "\tYou peak at $" + maxCash + ".\n"
    
  57.       + "\tAfter " + rolls + " rolls, you run out of cash.");
    
  58.         }
    
  59.     }
    
  60. }
    


Conditional blocks Java Programming
Loop blocks
Boolean expressions