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

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

View Full Document Right Arrow Icon
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.
Background image of page 1

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

View Full DocumentRight Arrow Icon
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");
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

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 Right Arrow Icon
Ask a homework question - tutors are online