{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# L11 - Computer Science 1 1 SS C Lecture 11 Contents 2...

This preview shows pages 1–3. Sign up to view the full content.

CS101 Lecture 12 1 Computer Science 1 1 SS C Lecture 11 Contents nested if statements Indentation of if statements. if . .. else if . .. statements equality of Strings (equals(), ==) return statements in methods Coursebook: §11 2 Nested if statements Any java statement, including another if statement, can occur inside the then or else parts of if statements. if ( condition1 ) { } else { } if ( condition2 ) { } else { } statement D1; . .. statement A1; ... statement B1; ... if ( condition3 ) { } statement E1; ... statement C1; 3 Ex01 - What is the output? The code below makes two calls to the lotsIfs() method on the next slide. Give the output. public class L11Program { public void start() { System.out.println("1. lotsIfs(6, 7)"); lotsIfs(6 , 7); System.out.println("2. lotsIfs(16, 7)"); lotsIfs(16 , 7); System.out.println(); } > java L11 1. lotsIfs(6, 7) line 3 line 7 line 8 2. lotsIfs(16, 7) line 6 line 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 private void lotsIfs(int x, int y) { if (x < 10) { if (y > 9 || y == 5) { System.out.println("line 1"); x = 11; } else { if (y % 2 == 0 || y == 3) { System.out.println("line 2"); } else { System.out.println("line 3"); } } } else { if (y > x && y >= 5) { System.out.println("line 4"); x = 12; } else { System.out.println("line 6"); } } if (x % 3 == 0) { System.out.println("line 7"); } System.out.println("line 8"); 4 } } 5 Ex02 - Complete the code Complete the two calls to the lotsIfs() method on the previous slide so that the output is: System.out.println("3. lotsIfs()"); lotsIfs( , ); //complete this call System.out.println(); System.out.println("4. lotsIfs()"); lotsIfs( , ); //complete this call System.out.println(); 3. lotsIfs() line 1 line 8 4. lotsIfs() line 6 line 8 6 Ex03 - Indentation is important I n d e n t a t i o n i s important. All the statements inside the if are indented. All the statements inside the else are indented. public class L11Program { public void start() { int x = 4; int y = 6; int z = 8; if (x == 5 || y > 5) { if (x > 4 && z == 8) { System.out.print("A "); } else { if (y == 6 && z >= x) { System.out.print("B "); } else { System.out.print("C "); }}} else { System.out.print("D "); } } On the next slide rewrite the code with the correct indentation.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS101 Lecture 12 2 7 Ex03 - Rewrite the code public class L11Program { public void start() { if (x == 5 || y > 5) { if (x > 4 && z == 8) { System.out.print("A "); //output int x = 4; int y = 6; int z = 8; 8 if . .. else . .. if . .. statements Frequently we would like to execute different statements depending on the value in one variable. The code on the next slide, if indented in the way shown so far, leads diagonally across the page like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public class L11Program { public void start() { displayAgeInfo (3); } private void displayAgeInfo (int age) { if (age > 70) { System.out.println("In your 70s, 80s or 90s");
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

L11 - Computer Science 1 1 SS C Lecture 11 Contents 2...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online