1
2 Revision To understand the term Nesting –To be able to write programs that contain nested loops. –To be able to dry run programs containing nested loops. To introduce the concepts of Iteration –To understand how to use the while loop construct –To understand how to use the for loop construct –To understand how to use the do … while loop construct To understand a different kind of selection statement: switch
Nested if … else int a = 15; int b = 10; if(10 <= a) { if(b <= 10) { a = a * b; } else { a = a + b; } } else { a = a - b; } 3 Very poor layout. You should indent every time you have : - then clause else clause if inside another if (which is really then/else clause) Put } at the start of the line. Put { at the end of a line
Nested if … else Value of a ? int a = 15; int b = 10; if(10 <= a) { if(b <= 10) { a = a * b; } else { a = a + b; } // end inner if } else { a = a - b; } // end outer if Better layout? You should indent every time you have : - then clause else clause if inside another if (which is really then/else clause) Put } at the start of the line. Put { at the end of a line
What is the value of a ? int a = 15; int b = 10; if(25 > a) { if(10 > b) { a = a * b; } else { a = a + b; } // end inner if } else { a = a - b; } // end if 5 25
Selection - revision Have a look at this youtube video on selection In your own time watch this one 6
Additional Information A BIG problem you may encounter when using nesting of if … else code is the matching of “else” with the appropriate “if”.
String calculateGrade(int marks) { String result = "Error"; if(marks >= 70) result = "A"; else if(marks >= 60) result = "B"; else if(marks >= 50) result = "C"; else if(marks >= 40) result = "D"; result = "Fail"; else return result; } If you want to spend hours debugging your code don’t follow indenting and bracketing standards. Why won't this program compile? The return statement may not necessarily be reached. The else on line 11 should be on line 10. You can see that there is no “if” just before line 11 statement. Conform to the Coding Conventions
String calculateGrade(int marks) { String result = "Error"; if(marks >= 70){ result = "A"; } else { if(marks >= 60){ result = "B"; } else { if(marks >= 50){ result = "C"; } else { if(marks >= 40){ result = "D"; } else { result = "Fail"; } return result; } Brackets can help reduce errors. Brackets and indents can help debugging.
When will message 2 be printed? if(x>18 && x<30){ if (q > 200) { System.out.println("message 1"); } else { System.out.println("message 2"); } if(x>18 && x<30){ if (q > 200) { System.out.println("message 1"); } } else { System.out.println("message 2"); } if(x>18 && x<30) if (q > 200) System.out.println("message 1"); else System.out.println("message 2"); else is matched with most recently defined if. Here we have the same behaviour as the bracket free version Different behaviour can be achieved with different positioning of braces Indents should make reading easier The “Dangling else” Problem Answer: when x>18 and x 200
Do loop. What is the value of s ? int t = 12; int s = 2; do { t = t - 2; s = s + 3; } while(t > 8); // end do loop 11 8 t s t> True 8 8 False
While Loop. What is the value of x ? int y = 10; int x = 9; while(y > 7) { y = y - 1; x = x + 3; } // end while 12 y x y> True 9 12 True 8 15 True 7 18 False 18
For loop. What is the value of s ? int s = 15; int r; for(r=4; r>=0; r=r-2) { s = s + 5; } // end for s r r>= True 20 2 True 25 0 True False
For loop. What is the value of h ? int h = 14; int i, j, k; for(i=1; i<3; i=i+1) { for(j=2; j<3; j=j+1) { for (k=2; k<=3; k=k+1) { h = h + 1; } // end inner for } // end middle for } // end outer for 14
int h = 14; int i, j, k; for(i=1; i<3; i=i+1) { for(j=2; j<3; j=j+1) { for (k=2; k<=3; k=k+1) { h = h + 1; } // end inner for } // end middle for } // end outer for 15 h i j k i<3 j<3 k<= T 2 T 15 3 T 16 4 F 3 F 2 T 17 3 T 18 4 F 3 F 18 Value of h ?
Switch. What is the value of a ? int a = 12; int b = 2; switch(b) { case 1: a = a - b; case 2: a = b * b; case 3: a = a + b; default: a = a - 8; } a b
Switch. What is the value of b ? int a = 3; int b = 17; switch(a) { case 3: b = a + b; break; case 6: b = a * b; break; case 9: b = a * b * 2; break; default: b = a * b * 3; break; } 17 20
Switch with data type char char a = 'B'; int b = 7; Int c = 10; switch(a) { case 'A': b = c + b; break; case 'B': b = c * b; break; case 'C': b = c * b * 2; break; default: b = c * b * 3; break; } 18 70
Can we deal with upper or lower case? char a = 'a'; int b = 7; Int c = 10; switch(a) { case 'A': case 'a': b = c + b; break; case 'B': case 'b': b = c * b; break; case 'c': case 'C': b = c * b * 2; break; default: b = c * b * 3; break; } 19 17
Any Requests for Thursday? 20