CS256 Computer Science I Kevin Sahr, PhD Lecture 19: Nested Conditionals and Loops 1 Nesting remember that conditional statements and loops contain single statements (or single block statements) this statement can itself be a conditional or loop a conditional or loop that occurs inside another conditional or loop is called a nested conditional or loop 2 Abstraction & Nesting abstraction is the key to writing complicated nested constructs view each entire nested conditional or loop as a single abstracted unit relative to what it is nested in 3 EX: pseudocode that outputs which of two numbers is smaller: x is less than y output x is smaller output y is smaller problem: what x and y are equal? will need to use a nested conditional 4
Abstract Pseudocode recall that we can write pseudocode at dferent levels of abstraction what matters is that you can get from the pseudocode to Java source code once we understand the - on the previous slide, we can abstract that pseudocode as: output the smaller of x and y 5 Pseudocode we can then write pseudocode to handle the case when x and y are equal: x is equal to y output equal output the smaller of x and y 6 Building the Code start with the code for the equality case: (x == y) Output.showMessage( equal ); output the smaller of x and y 7 Code now substitute in the code for the -clause unit: (x == y) Output.showMessage( equal ); (x < y) Output.showMessage( x is smaller ); Output.showMessage( y is smaller ); note no s could use but not necessary, since the nested - is a single statement relative to the outer - 8
EX: given a numeric grade (0.0-10.0), output the corresponding letter grade 9 (grade >= 8.0) (grade >= 7.0) (grade >= 6.0) this code is hard to read recall that Java doesn t care about whitespace, so we can reformat it to make our meaning clearer 10 (grade >= 8.0) (grade >= 7.0) (grade >= 6.0) 11 (grade >= 8.0) (grade >= 7.0) (grade >= 6.0) 12
(grade >= 8.0) (grade >= 7.0) (grade >= 6.0) 13 (grade >= 8.0) (grade >= 7.0) (grade >= 6.0) see Grade.java for a complete program 14 ( 2) EX: our code from earlier (x == y) Output.showMessage( equal ); (x < y) Output.showMessage( x is smaller ); Output.showMessage( y is smaller ); 15 ( 2) should be formatted: (x == y) Output.showMessage( equal ); (x < y) Output.showMessage( x is smaller ); Output.showMessage( y is smaller ); 16
input word problem: Write a program that calculates and outputs the areas of circles with user-input radii. For each circle, give the user as many chances as needed to enter a positive radius. Classy each area as big (greater than 100), medium (less than or equal to 100 and greater than 50), or small (less than or equal to 50). let s begin by breaking this down into smaller chunks that must be accomplished 17 word problem: Write a program that calculates and outputs the areas of circles with user-input radii. For each circle, give the user as many chances as needed to enter a positive radius. Classy each area as big (greater than 100), medium (less than or equal to 100 and greater than 50), or small (less than or equal to 50). we note that the highlighted phrase is an instance of the algorithm: positive radius we have detailed pseudocode for this algorithm (see last lecture) 18 word problem: Write a program that calculates and outputs the areas of circles with user-input radii. For each circle, give the user as many chances as needed to enter a positive radius. Classy each area as big (greater than 100), medium (less than or equal to 100 and greater than 50), or small (less than or equal to 50). let s write an algorithm named calculate area for the highlighted portion: area = 3.14 * radius * radius output area 19 word problem: Write a program that calculates and outputs the areas of circles with user-input radii. For each circle, give the user as many chances as needed to enter a positive radius. Classy each area as big (greater than 100), medium (less than or equal to 100 and greater than 50), or small (less than or equal to 50). note that classying the area will be a nested construct so let s abstract-out the details of that part by writing an algorithm for just that part 20
Pseudocode for Algorithm classy area area is greater than 100 output BIG area is greater than 50 output MEDIUM output SMALL 21 we now have three abstract algorithms: input positive radius calculate area classy area now we can write the entire program pseudocode, ignoring the details of these abstract sub-algorithms note that our word problem indicates that the program must handle multiple circles; we can use either: a counter-controlled loop, or a sentinel loop we choose a counter-controlled loop 22 Word Problem Program Pseudocode input positive #-of-circles do #-of-circles times input positive radius calculate area classy area now we just need to substitute in the code for each sub-unit of pseudocode see Circles2.java for the complete source code 23 Nested Loops note that this program contains an input validation loop nested inside of a counter-controlled loop when working with nested loops it is especially important to think of the inner loop as a single unit, independent of the outer loop the inner loop will execute completely every time the outer loop executes once 24
Nested Loops EX: How many times will "A" be output? count1 = 0; while (count1 < 10) count2 = 0; while (count2 < 20) count2 = count2 + 1; count1 = count1 + 1; We can use abstraction to understand this code 25 25 Nested Loops Note that the inner loop is a countercontrolled loop count1 = 0; while (count1 < 10) count2 = 0; while (count2 < 20) count2 = count2 + 1; count1 = count1 + 1; 26 26 Nested Loops let s replace the inner loop with our abstract counter-controlled loop pseudocode count1 = 0; while (count1 < 10) output A 20 times count1 = count1 + 1; 27 27 Nested Loops the outer loop is also a counter-controlled loop, so let s replace that as well repeat 10 times output A 20 times How many times will "A" be output? 10 * 20 = 200 28 28
Lecture 19 Vocabulary nested conditional nested loop 29