4.7 Nested Loops

Ace your homework & exams now with Quizwiz!

Nested loops uses

-generate all combinations of some items

What is output by the following code? int row; int col; for(row = 2; row <= 3; row = row + 1) { for(col = 0; col <= 1; col = col + 1) { System.out.print("" + row + col + " "); } }

20 21 30 31 -outer loop ranges from 2 to 3 and inner loop ranges from 0 to 1 First outer loop iteration outputs 20 and 21. Second outer loop iteration outputs 30 and 31

Given the following code, how many times will the inner loop body execute? char letter1; char letter2; letter1 = 'a'; while (letter1 <= 'f') { letter2 = 'c'; while (letter2 <= 'f') { // Inner loop body ++letter2; } ++letter1; }

24 -The outer loop iterates 6 times (letter1 is a, b, c, d, e, f) the inner loop iterates 4 times (letter2 is c, d, e, f) for each outer loop execution 6*4=24

Given the following code, how many times will the inner loop body execute? int row; int col; for(row = 0; row < 2; row = row + 1) { for(col = 0; col < 3; col = col + 1) { // Inner loop body } }

6 The outer loop executes 2 times (row is 0, then 1) the inner loop executes 3 times (col is 0,1,2) for every outer loop execution 2*3 is 6.

Nested Loops are referred as

Inner loop / outer loop

Nested Loop

Loop that appears in the body of another loop

What is output by the following code? char letter1; char letter2; letter1 = 'y'; while (letter1 <= 'z') { letter2 = 'a'; while (letter2 <= 'c') { System.out.print("" + letter1 + letter2 + " "); ++letter2; } ++letter1; }

Ya yb yc za zb zc -outer loop ranges from y to z and the inner loop ranges from a to c. First outer loop iteration prints ya yb yc. Second prints za zb zc

Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3

import java.util.Scanner; public class NestedLoop { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int userNum; int i; int j; userNum = scnr.nextInt(); /* Your solution goes here */ } }

4.7.2: Nested loops: Print seats. Given numRows and numColumns, print a list of all seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat, including after the last. Use separate print statements to print the row and column. Ex: numRows = 2 and numColumns = 3 prints: 1A 1B 1C 2A 2B 2C

import java.util.Scanner; public class NestedLoops { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int numRows; int numColumns; int currentRow; int currentColumn; char currentColumnLetter; numRows = scnr.nextInt(); numColumns = scnr.nextInt(); /* Your solution goes here */ System.out.println(""); } }

import java.util.Scanner; public class IntHistogram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int numAsterisk; // Number of asterisks to print int i; // Loop counter numAsterisk = 0; while (numAsterisk >= 0) { System.out.print("Enter an integer (negative to quit): "); numAsterisk = scnr.nextInt(); if (numAsterisk >= 0) { System.out.println("Depicted graphically:"); for (i = 1; i <= numAsterisk; ++i) { System.out.print("*"); } System.out.println("\n"); } } System.out.println("Goodbye."); } }

java.util.Scanner package to allow user input. The code declares a new class called IntHistogram. The main method is called when the program runs. The program uses a Scanner object to read input from the user. The code declares two variables of type int called numAsterisk and i. The program initializes numAsterisk to the value 0. The program enters a while loop that will run as long as numAsterisk is greater than or equal to 0. Inside the while loop, the program prompts the user to enter an integer, which is read and stored in the variable numAsterisk. If the value entered by the user is greater than or equal to 0, the program enters a for loop that will print a number of asterisks equal to the value of numAsterisk. The for loop runs numAsterisk times and prints an asterisk to the console each time it runs. Once the for loop is finished, the program prints a newline character to the console to separate the histogram from the next input prompt. If the value entered by the user is less than 0, the program skips the for loop and prints "Goodbye." to the console. The program repeats this process until the user enters a negative value for numAsterisk. In summary, the program prompts the user to enter a non-negative integer, and if they do, it prints a histogram of asterisks with a length equal to the entered value. The program continues prompting the user for input until they enter a negative value, at which point the program prints "Goodbye." to the console and terminates

The code imports the java.util.Scanner package to allow user input. The code declares a new class called DomainNamePrinter. The main method is called when the program runs. The program uses a Scanner object to read input from the user (however, the input is not used in this code). The code declares two variables of type char called letter1 and letter2. The program prints the string "Two-letter domain names:" to the console. The code initializes letter1 to the value 'a'. The program enters a while loop that will run as long as letter1 is less than or equal to 'z'. Inside the while loop for letter1, the program initializes letter2 to the value 'a'. The program enters another while loop that will run as long as letter2 is less than or equal to 'z'. Inside the while loop for letter2, the program prints a string to the console that consists of letter1, letter2, and the string ".com". The "" + syntax is used to concatenate the characters and the string into a single string. After printing the string to the console, the program increments letter2 by one. Once the while loop for letter2 is finished, the program increments letter1 by one. The program repeats this process until all two-letter combinations from 'aa' to 'zz' have been printed to the console.

n summary, the program generates and prints all possible two-letter domain names from "aa.com" to "zz.com" using two nested while loops that increment two characters, letter1 and letter2, and concatenate them into a string.


Related study sets

Chemistry Test #2 (Chapters 2.14, 3, 4, 5.1-5.4)

View Set

Project Risk Management Study Questions

View Set

Diet therapy for clinical nutrition, modules 1-6 ( Understanding normal and clinical nutrition, tenth edition)

View Set