Repetition in Java
Explains how to write while, for, and do while loops in a Java program.
Prerequisites
- Understand Java primitive types and variables
- Understand Java arithmetic operators
Objectives
- Know when and why to use repetition
- Understand how to implement
while
loops - Understand how to implement
for
loops - Understand how to implement
do while
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 most programming languages there is some type of mechanism for repeating a block of code. This is known as repetition or a loop. Some examples of when you want the computer to repeat a block of code are:
- You want to create a string that has a repeating pattern
- You want to sum a list of numbers
- You want to find the factorial of a number
- You want to print a list of student’s names from an array
while Loops
- A
while
loop has three parts: the keywordwhile
, 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", "Kyle", "Sarah"}; int i = 0; while (studentNames[i].equals("Kyle") == false) { System.out.println(studentNames[i]); i++; }
for Loops
- A for loop has 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", "Kyle", "Sarah"}; for (int i = 0; i < studentNames.length; i++) { System.out.println(studentNames[i]); }
- The following is an ordered list of how the previous code example is executed.
- The computer creates an array named
studentNames
and fills it with five names. - The computer stores 0 in the variable
i
. The computer does this once only. - The computer evaluates the boolean expression (it is true).
- Because the boolean expression was true, the computer executes the code in the body of the loop.
- The computer adds 1 to
i
. - Steps 3, 4, and 5 are repeated until the boolean expression evaluates to false.
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", "Kyle", "Sarah"}; int i = 0; do { System.out.println(studentNames[i]); i++; } while (studentNames[i].equals("Kyle") == 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; }
- Example:
studentNames
array are printed to the screen. String[] studentNames = {"Bill", "Susan", "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.
- Here is an example of a nested loop:
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(); }