Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1

2 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

3 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

4 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 4 150 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

5 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

6 Selection - revision Have a look at this youtube video on selection http://www.youtube.com/watch?v=42Azc4Yjj68 In your own time watch this one https://www.youtube.com/watch?v=CZT00BnNscY 6

7 Additional Information A BIG problem you may encounter when using nesting of if … else code is the matching of “else” with the appropriate “if”.

8 1 2 3 4 5 6 7 8 9 10 11 12 13 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

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

10 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

11 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>8 12 2 10 5 True 8 8 False

12 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>7 10 9 True 9 12 True 8 15 True 7 18 False 18

13 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 13 30 s r r>=0 15 4 True 20 2 True 25 0 True 30 -2 False

14 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

15 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<=3 14 1 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 ? 134565654345656543134565654345656543 123456789123456789

16 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; } 16 -2 a b 12 2 4 6 -2

17 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

18 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

19 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

20 Any Requests for Thursday? 20


Download ppt "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."

Similar presentations


Ads by Google