Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nesting Loops (That’s loops inside of loops. Why not?)

Similar presentations


Presentation on theme: "Nesting Loops (That’s loops inside of loops. Why not?)"— Presentation transcript:

1 Nesting Loops (That’s loops inside of loops. Why not?)

2 Consider a line of asterisks: *********************... How can I do this in a program? row column

3 Consider a line of asterisks: *************… final int numberCols = 20; for (int col=1; col<=numberCols; col++) { System.out.print( "*" ); }System.out.println();

4 Say I want rows and rows of this line. **************…**************…**************…... final int numberRows = 5; final int numberCols = 20;

5 //output all of the lines for (int row=1; row<=numberRows; row++) { //output one line //output one line for (int col=1; col<=numberCols; col++) { for (int col=1; col<=numberCols; col++) { System.out.print( "*" ); System.out.print( "*" ); } System.out.println(); System.out.println();}

6 Say instead we want: * * * * …... Note space between asterisks.

7 for (int row=1; row<=numberRows; row++) { for (int col=1; col<=numberCols; col++) { for (int col=1; col<=numberCols; col++) { System.out.print( "* " ); System.out.print( "* " ); } System.out.println(); System.out.println();} space

8 Say we want a checkerboard: for (int row=1; row<=numberRows; row++) { for (int col=1; col<=numberCols; col++) { for (int col=1; col<=numberCols; col++) { System.out.print( "* " ); System.out.print( "* " ); } System.out.println(); System.out.println();}

9 for (int row=1; row<=numberRows; row++) { for (int col=1; col<=numberCols; col++) { for (int col=1; col<=numberCols; col++) { System.out.print( "* " ); System.out.print( "* " ); } System.out.println(); System.out.println();} Can mod (%) help us somehow? Say we want a checkerboard:

10 final int width = 8; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row //output all of the columns within the row for (int c=1; c<=width/2; c++) { for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 ) { System.out.print( " *" ); System.out.print( " *" ); } else { System.out.print( "* " ); System.out.print( "* " );} } System.out.println(); System.out.println();}

11 final int width = 8; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row //output all of the columns within the row for (int c=1; c<=width/2; c++) { for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 )System.out.print( " *" ); elseSystem.out.print( "* " ); } System.out.println(); System.out.println();}

12 final int width = 8; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row //output all of the columns within the row for (int c=1; c<=width/2; c++) { for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 ) System.out.print( " *" ); System.out.print( " *" );else } System.out.println(); System.out.println();} What are the possible values of r % 2? How many values are possible with r % 2? What data type has exactly this many values?

13 final int width = 8; boolean bumpOver = false; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row //output all of the columns within the row for (int c=1; c<=width/2; c++) { for (int c=1; c<=width/2; c++) { if (bumpOver) System.out.print( " *" ); System.out.print( " *" );else } System.out.println(); System.out.println(); bumpOver = !bumpOver; bumpOver = !bumpOver;} Use a boolean instead of mod.

14 final int width = 8; boolean bumpOver = false; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row //output all of the columns within the row for (int c=1; c<=width/2; c++) { for (int c=1; c<=width/2; c++) { if (bumpOver) System.out.print( " *" ); System.out.print( " *" );else } System.out.println(); System.out.println(); bumpOver = !bumpOver; bumpOver = !bumpOver;} Why width/2?

15 /* * file : CheckerBoard.java * file : CheckerBoard.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to output a checkerboard (problem 4.1, p. 74. * desc. : program to output a checkerboard (problem 4.1, p. 74. */ */ public class CheckerBoard { public static void main ( String[] s ) { public static void main ( String[] s ) { //declare variables & initialize i/o //ouput startup message System.out.println( "here's the checkerboard:\n" ); final int width = 8; boolean bumpOver = false; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row //output all of the columns within the row for (int c=1; c<=width/2; c++) { for (int c=1; c<=width/2; c++) { if (bumpOver) System.out.print( " *" ); System.out.print( " *" );else } System.out.println(); System.out.println(); bumpOver = !bumpOver; bumpOver = !bumpOver;} //finish up System.out.println( "\n\nadios" ); }} The complete program.

16 Let’s “draw” a rectangle/box.

17 Generate a rectangle. ********** * * * *<- rows * * ********** ^ cols

18 Generate a rectangle. ********** * * * *<- rows * * ********** ^ cols Note that the first and last lines are the same; also note that all of the middle lines are the same.

19 Generate a rectangle. ********** * * * *<-rows * * ********** ^ cols So our algorithm is: 1. Output the first line. 2. Output the intervening lines. 3. Output the last line (just like the first).

20 Generate a rectangle. ********** * * * *<-rows * * ********** ^ cols //Output the first line. for (int c=0; c<cols; c++) { System.out.print( "*" ); }System.out.println();

21 Generate a rectangle. ********** * * * *<-rows * * ********** ^ cols //Output the intervening lines. for (int r=0; r<rows-2; r++) { System.out.print( "*" ); for (int c=0; c<cols-2; c++) { System.out.print( " " ); } System.out.println( "*" ); }

22 Generate a rectangle. ********** * * * *<-rows * * ********** ^ cols //Output the last line (just like the first). for (int c=0; c<cols; c++) { System.out.print( "*" ); }System.out.println();

23 final int rows = 10;//define number of rows final int cols = 20;//define number of columns //Output the first line (just like the last). for (int c=0; c<cols; c++) { System.out.print( "*" ); }System.out.println(); //Output the intervening lines. for (int r=0; r<rows-2; r++) { System.out.print( "*" ); for (int c=0; c<cols-2; c++) { System.out.print( " " ); } System.out.println( "*" ); } //Output the last line (just like the first). for (int c=0; c<cols; c++) { System.out.print( "*" ); }System.out.println();

24

25 Repeatedly prompt until user enters something valid. final intminimum = 0; intinputValue = minimum-1; while (inputValue < minimum) { inputValue = in.nextInt(); }

26 Repeatedly prompt until user enters something valid. final intminimum = 0; intinputValue = minimum-1; booleaninvalid = true; while (invalid) { inputValue = in.nextInt(); if (inputValue >= minimum) { invalid = false; }}

27 Repeatedly prompt until user enters something valid. final intminimum = 0; intinputValue = minimum-1; booleanvalid = false; while (!valid) { inputValue = in.nextInt(); if (inputValue >= minimum) { valid = true; }}

28

29 Shakespearean insults p. 75 of Per Brinch Hansen’s Java book (if time permits). p. 75 of Per Brinch Hansen’s Java book (if time permits). Thou goatish, hasty-witted ratsbane! Thou goatish, hasty-witted ratsbane! Thou loggerheaded, doghearted dewberry! Thou loggerheaded, doghearted dewberry! Thou churlish, tickle-brained pigeon-egg! Thou churlish, tickle-brained pigeon-egg! Our task is to prompt the user for the number of these to output, and then output that number of insults. Our task is to prompt the user for the number of these to output, and then output that number of insults.

30 Shakespearean insults Thougoatish,hasty-wittedratsbane! Thougoatish,hasty-wittedratsbane! Thouloggerheaded,doghearteddewberry! Thouloggerheaded,doghearteddewberry! Thouchurlish,tickle-brainedpigeon-egg! Thouchurlish,tickle-brainedpigeon-egg! always pick one

31 Shakespearean insults Column 1 Column 2 Column 3 artlessbeetle-headedbarnacle bootlessclay-brainedcoxcomb churlishdoghearteddewberry dissemblingfly-bittenfoot-licker erranthasty-wittedgudgeon fobbingidle-headedjolthead goatishrough-hewnlout impertinentswag-belliedminnow jarringtickle-brainedpigeon-egg loggerheadedweather-bittenratsbane

32 Shakespearean insults algorithm 1. Prompt user for number of insults. 2. Read in the number of insults. 3. Output that number of insults. 1. Output “Thou ”. 2. Pick and output a word from column 1. 3. Output “,”. 4. Pick and output a word from column 2. 5. Output “ ”. 6. Pick and output a word from column 3. 7. Output “!” w/ newline.

33 Shakespearean insults algorithm //Prompt user for number of insults. //Read in the number of insults. //Output that number of insults. //Output “Thou ”. //Pick and output a word from column 1. //Output “,”. //Pick and output a word from column 2. //Output “ ”. //Pick and output a word from column 3. //Output “!” w/ newline.

34 Shakespearean insults algorithm Scanner in = new Scanner( System.in ); //Prompt user for number of insults. System.out.print( "Enter the number of insults: " ); //Read in the number of insults. final int count = in.nextInt();

35 Shakespearean insults algorithm //Output that number of insults. for (int i=0; i<count; i++) { //Output “Thou ”. //Pick and output a word from column 1. //Output “, ”. //Pick and output a word from column 2. //Output “ ”. //Pick and output a word from column 3. //Output “!” w/ newline. }

36 //Output that number of insults. for (int i=0; i<count; i++) { //Output “Thou ”. System.out.print( “Thou ” ); //Pick and output a word from column 1. ? //Output “, ”. System.out.print( “, ” ); //Pick and output a word from column 2. ? //Output “ ”. //Pick and output a word from column 3. ? //Output “!” w/ newline. System.out.println( “!” ); }

37 //Output that number of insults. Random rnd = new Random(); for (int i=0; i<count; i++) { System.out.print( “Thou ” ); //Pick and output a word from column 1. ? System.out.print( “, ” ); //Pick and output a word from column 2. ? System.out.print( “ ” ); //Pick and output a word from column 3. ? System.out.print ln ( “!” ); }

38 //Output that number of insults. Random rnd = new Random(); for (int i=0; i<count; i++) { System.out.print( “Thou ” ); //Pick and output a word from column 1. switch (rnd.nextInt(10)+1) { case 1:System.out.print( “artless”) break; case 2:System.out.print( “bootless”); break;… default:System.out.print( “loggerheaded”); break; } //end switch

39 //Output that number of insults. Random rnd = new Random(); for (int i=0; i<count; i++) { System.out.print( “Thou ” ); //Pick and output a word from column 1. switch (rnd.nextInt(10)+1) { case 1:System.out.print( “artless”)break; case 2:System.out.print( “bootless”);break; … default:System.out.print( “loggerheaded”);break; } //end switch System.out.print( “, ” ); //Pick and output a word from column 2. ? System.out.print( “ ” ); //Pick and output a word from column 3. ? System.out.println( “!” ); }

40 //Pick and output a word from column 1. switch (rnd.nextInt(10)+1) { case 1:System.out.print( “artless”)break; case 2:System.out.print( “bootless”);break; … default:System.out.print( “loggerheaded”);break; } //end switch System.out.print( “, ” ); //Pick and output a word from column 2. switch (rnd.nextInt(10)+1) { case 1:System.out.print( “beetle-headed”)break; case 2:System.out.print( “clay-brained”);break; … default:System.out.print( “weather-bitten”);break; } //end switch System.out.print( “ ” ); //Pick and output a word from column 3. …

41 //Output that number of insults. Random rnd = new Random(); for (int i=0; i<count; i++) { System.out.print( “Thou ” ); //Pick and output a word from column 1. switch (rnd.nextInt(10)+1) { case 1:System.out.print( “artless”)break; case 2:System.out.print( “bootless”);break; … default:System.out.print( “loggerheaded”);break; } //end switch System.out.print( “, ” ); //Pick and output a word from column 2. switch (rnd.nextInt(10)+1) { case 1:System.out.print( “beetle-headed”)break; case 2:System.out.print( “clay-brained”);break; … default:System.out.print( “weather-bitten”);break; } //end switch System.out.print( “ ” ); //Pick and output a word from column 3. ? System.out.println( “!” ); } And similarly here.


Download ppt "Nesting Loops (That’s loops inside of loops. Why not?)"

Similar presentations


Ads by Google