Chapter 3.4.3 – Nested for Loops | Introduction to Programming Using Java

Chapter 3.4.3 – Nested for Loops | Introduction to Programming Using Java

 

3.4.3 Nested for Loops

 

Control structures in Java are statements that contain statements. In particular, control structures can contain control structures. You’ve already seen several examples of if statements inside loops, and one example of a while loop inside another while, but any combination of one control structure inside another is possible. We say that one structure is nested inside another. You can even have multiple levels of nesting, such as a while loop inside an if statement inside another while loop.

The syntax of Java does not set a limit on the number of levels of nesting. As a practical matter, though, it’s difficult to understand a program that has more than a few levels of nesting.

Nested for loops arise naturally in many algorithms, and it is important to understand how they work. Let’s look at a couple of examples. First, consider the problem of printing out a multiplication table like this one:

 

Chapter 3.4.3 - Nested for Loops | Introduction to Programming Using Java

 

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

 

Chapter 3.4.3 - Nested for Loops | Introduction to Programming Using Java

 

The first step in the for loop can itself be expressed as a for loop. We can expand “Print the first twelve multiples of rowNumber on one line” as:

 

Chapter 3.4.3 - Nested for Loops | Introduction to Programming Using Java

 

so a refined algorithm for printing the table has one for loop nested inside another:

 

Chapter 3.4.3 - Nested for Loops | Introduction to Programming Using Java

 

We want to print the output in neat columns, with each output number taking up four spaces. This can be done using formatted output with format specifier %4d. Assuming that rowNumber and N have been declared to be variables of type int, the algorithm can be expressed in Java as

 

Chapter 3.4.3 - Nested for Loops | Introduction to Programming Using Java

 

This section has been weighed down with lots of examples of numerical processing. For our next example, let’s do some text processing. Consider the problem of finding which of the 26 letters of the alphabet occur in a given string.

For example, the letters that occur in “Hello World” are D, E, H, L, O, R, and W. More specifically, we will write a program that will list all the letters contained in a string and will also count the number of different letters. The string will be input by the user. Let’s start with a pseudocode algorithm for the program.

 

Chapter 3.4.3 - Nested for Loops | Introduction to Programming Using Java

 

Since we want to process the entire line of text that is entered by the user, we’ll use TextIO.getln() to read it. The line of the algorithm that reads “for each letter of the alphabet” can be expressed as “for (letter=’A’; letter<=’Z’; letter++)”. But the body of this for loop needs more thought. How do we check whether the given letter, letter, occurs in str? One idea is to look at each character in the string in turn, and check whether that character is equal to letter.

We can get the i-th character of str with the function call str.charAt(i), where i ranges from 0 to str.length() – 1. One more difficulty: A letter such as ’A’ can occur in str in either upper or lower case, ’A’ or ’a’. We have to check for both of these. But we can avoid this difficulty by converting str to upper case before processing it. Then, we only have to check for the upper case letter. We can now flesh out the algorithm fully.

Note the use of break in the nested for loop. It is required to avoid printing or counting a given letter more than once (in the case where it occurs more than once in the string). The break statement breaks out of the inner for loop, but not the outer for loop. Upon executing the break, the computer continues the outer loop with the next value of letter.

 

Chapter 3.4.3 - Nested for Loops | Introduction to Programming Using Java

nested for loops

 

In fact, there is actually an easier way to determine whether a given letter occurs in a string, str. The built-in function str.indexOf(letter) will return -1 if letter does not occur in the string. It returns a number greater than or equal to zero if it does occur.

So, we could check whether letter occurs in str simply by checking “if (str.indexOf(letter) >= 0)”. If we used this technique in the above program, we wouldn’t need a nested for loop. This gives you a preview of how subroutines can be used to deal with complexity.

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java

Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java

Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java

Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java

1 thought on “Chapter 3.4.3 – Nested for Loops | Introduction to Programming Using Java”

Leave a Comment