1 / 41

Nested Loops: Explore Different Patterns and Shapes in Programming

Learn how to create various shapes and patterns using nested loops in a program. From basic lines of asterisks to checkerboards and rectangles, explore different design possibilities.

ccook
Download Presentation

Nested Loops: Explore Different Patterns and Shapes in Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Consider a line of asterisks:*************… final intnumberCols = 20; for (intcol=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 for (intcol=1; col<=numberCols; col++) { System.out.print( "*" ); } System.out.println(); }

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

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

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

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

  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 for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 ) { System.out.print( " *" ); } else { System.out.print( "* " ); } } 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 for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 ) System.out.print( " *" ); else System.out.print( "* " ); } 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 for (int c=1; c<=width/2; c++) { if ( (r%2) == 0 ) System.out.print( " *" ); else System.out.print( "* " ); } 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; booleanbumpOver = false; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row for (int c=1; c<=width/2; c++) { if (bumpOver) System.out.print( " *" ); else System.out.print( "* " ); } System.out.println(); bumpOver = !bumpOver; } Use a boolean instead of mod.

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

  15. /* * file : CheckerBoard.java * author: george j. grevera, ph.d. * desc. : program to output a checkerboard (problem 4.1, p. 74. */ public class CheckerBoard { 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; booleanbumpOver = false; //output all of the rows for (int r=1; r<=width; r++) { //output all of the columns within the row for (int c=1; c<=width/2; c++) { if (bumpOver) System.out.print( " *" ); else System.out.print( "* " ); } System.out.println(); 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: • Output the first line. • Output the intervening lines. • 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. Repeatedly prompt until user enters something valid. final int minimum = 0; intinputValue = minimum-1; while (inputValue < minimum) { inputValue = in.nextInt(); }

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

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

  27. Shakespearean insults • p. 75 of Per Brinch Hansen’s Java book (if time permits). • Thou goatish, hasty-witted ratsbane! • Thou loggerheaded, doghearted dewberry! • 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.

  28. Shakespearean insults • Thou goatish, hasty-witted ratsbane! • Thou loggerheaded, doghearted dewberry! • Thou churlish, tickle-brained pigeon-egg! always always pick one pick one pick one

  29. Shakespearean insults

  30. 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.

  31. 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.

  32. 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();

  33. 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. }

  34. //Output that number of insults. for (inti=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( “!” ); }

  35. //Output that number of insults. Random rnd = new Random(); for (inti=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.println( “!” ); }

  36. //Output that number of insults. Random rnd = new Random(); for (inti=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

  37. //Output that number of insults. Random rnd = new Random(); for (inti=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( “!” ); }

  38. //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. …

  39. //Output that number of insults. Random rnd = new Random(); for (inti=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.

More Related