Repetition in Java
by Ben Davidson
Objectives
- Know when and why to use repetition
- Understand how to implement
while loops
- Understand how to implement
do while loops
- Understand how to implement
for loops
- Understand how to implement
for each loops
- Understand how to use nested loops
Purpose of Repetition
Sometimes it is necessary to repeat a block of code many times.
It may seem like a simple solution to copy a block of code and paste it
as many times as you need, but there is an alternative to this
code-bloating idea. In all programming languages there is some
type of mechanism for repeating a block of code. This is known as
repetition or looping. Some examples of when you want to repeat a
block of code are:
- You want to print a list of student's names from an array
- You want to find the factorial of a number
while Loops
- A while loop has three parts: the keyword
while, a
boolean expression, and the code block to be repeated.
- Syntax:
while (boolean expression) {
statements to be repeated;
}
- while loops are useful to repeat a block of code until a
condition is met.
- In the following example, student names will be printed until
the student "Kyle" is found.
String[] studentNames = {"Bill", "Susan", "Fred", "Kyle", "Sarah"};
int i = 0;
while (!studentNames[i].equals("Kyle")) {
System.out.println(studentNames[i]);
i++;
}
do while Loops
- In a do while loop the block of code to be repeated comes before
the boolean expression. This guarantees that the code in the
body of the loop will be executed at least once.
- Syntax:
do {
statements to be repeated;
} while (boolean expression);
- Again, a do while loop is different from a while loop because
the statements in the code block will be executed at least once.
- Example:
String[] studentNames = {"Bill", "Susan", "Fred", "Kyle", "Sarah"};
int i = 0;
do {
System.out.println(studentNames[i]);
i++;
} while (!studentNames[i].equals("Kyle"));
for Loops
- A for loop has some of the same parts as a while loop, but with
a different order.
- for loops are useful for counting and going through an entire
array.
- A for loop has five parts: the keyword
for, an
initial action, a boolean expression, an action to be done after
each loop, and a block of code to be repeated:
- Syntax:
for (initial-action; boolean expression; action after each iteration) {
statements to be repeated;
}
- for loops are often implemented with arrays.
- The following example will print all of the names in the
studentNames array:
String[] studentNames = {"Bill", "Susan", "Fred", "Kyle", "Sarah"};
for (int i = 0; i < studentNames.length; i++) {
System.out.println(studentNames[i]);
}
- The following is an ordered list of how the loop is executed
- The array is created and filled with student names
- 0 is assigned to the variable i (only done once)
- The boolean expression is evaluted (it is true)
- Because the boolean expression was true, the code in the
body of the loop is executed
- i is incremented by one
- Steps 4 and 5 are repeated until the boolean expression is
false
for each Loops
- for each loops were designed to be used with arrays and
collections.
- A for each loop is similar to a for loop, except it only has
three parts: the keyword
for, a variable declaration in
which a new variable is declared and assigned to each element in the
array, and the block of code to be repeated.
- Syntax:
for (variableType variableName: arrayName) {
statements to be repeated;
}
- In the following example, all of the names in the
studentNames array are printed to the screen.
String[] studentNames = {"Bill", "Susan", "Fred", "Kyle", "Sarah"};
for (String name : studentNames) {
System.out.println(name);
}
Nested Loops
- It is often useful to put a loop inside of a loop.
- Nested loops are used to print two dimmensional arrays.
- An example of a nested loop follows:
int[][] board = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int i = 0; i < board.length; i++) {
for(int j = 0; j < board[i].length; j++){
System.out.print(board[i][j]);
}
System.out.println();
}
Copyright © 2010, Maia L.L.C. All rights reserved.