Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.

Similar presentations


Presentation on theme: "COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014."— Presentation transcript:

1 COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

2 Announcements Program 2 due Friday, 2pm ◦ Remember to also print out your code and hand it in I will be out of town on Friday ◦ Recitation will still be held by another COMP110 instructor at the usual time ◦ No office hours Friday morning  Ask me questions before 6pm Thursday 2

3 Some notes about printing your code Print from jGRASP if you can Print with a fixed-width font (such as Consolas or Courier New ) Print double-sided if you can 3

4 Questions? 4

5 Today in COMP 110 Nested loops Some comments on Lab 3 5

6 Nested loops Just like we have nested if/else statements, we can have nested loops 6

7 Nested loops example: friendly greetings 7

8 Student 1 shakes Student 4’s hand Student 1 shakes Student 5’s hand Student 1 shakes Student 6’s hand Student 2 shakes Student 4’s hand Student 2 shakes Student 5’s hand Student 2 shakes Student 6’s hand Student 3 shakes Student 4’s hand Student 3 shakes Student 5’s hand Student 3 shakes Student 6’s hand 8

9 Nested loops example: friendly greetings Student 1 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s hand Student 2 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s hand Student 3 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s hand 9

10 Nested loops example: friendly greetings for (every student in line A) { Student in line A shakes every Student’s hand in line B } 10

11 Nested loops example: friendly greetings for (every student in line A) { for (every student in line B) { (Student in line A) shakes (Student in line B)’s hand } 11 Inner loop Outer loop

12 Nested loops example: friendly greetings for (int stdLineA = 1; stdLineA <= 3; stdLineA++) { for (int stdLineB = 4; stdLineB <= 6; stdLineB++) { System.out.println(“Student ” + stdLineA + “ shakes Student ” + stdLineB + “’s hand.”); } 12 stdLineA 1 2 3 stdLineB 4 5 6

13 Nested loops example: friendly greetings for (int stdLineA = 1; stdLineA <= 3; stdLineA++) { for (int stdLineB = 4; stdLineB <= 6; stdLineB++) { System.out.println(“Student ” + stdLineA + “ shakes Student ” + stdLineB + “’s hand.”); } 13 Inner loop Outer loop

14 Nested loops You can nest different kinds of loops inside other loops, or put if/else statements inside loops, or put loops inside if/else statements, or… ExamAverager example in jGRASP 14

15 Loop bugs Infinite loops – already talked about these Off-by-one errors 15

16 Off-by-one errors Loop repeats one too many or one too few times for (count = 1; count < 10; count++) // loop 9 times: 1, 2, 3,... 9 for (count = 1; count <= 10; count++) // loop 10 times: 1, 2, 3,... 10 for (count = 0; count < 10; count++) // loop 10 times: 0, 1, 2,... 9 16

17 Finding errors Trace your variables ◦ Put output statements in your code to see what values are stored in your variables  System.out.println(variable);  Check whether the values are correct and what you expect ◦ Remove these extra output statements after the program runs correctly ◦ Read example in the book, p. 188 (4 th edition), p. 218 (5 th edition) Use a debugger 17

18 What does this code do? int a = 0; int b = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { b = b + i;} else { a = a + i; } 18

19 What does this code do? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { evenSum = evenSum + i;} else { oddSum = oddSum + i; } 19

20 What does this code do? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { evenSum = evenSum + i; } else { oddSum = oddSum + i; } 20

21 What is wrong with this code? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) evenSum = evenSum + i;} else { oddSum = oddSum + i; } 21

22 What is wrong with this code? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) evenSum = evenSum + i; } else { oddSum = oddSum + i; } 22

23 Indentation Indentation ◦ Makes code easier to read ◦ Helps with finding syntax and logic errors ◦ Indent code that goes between { and } Be consistent! 23

24 Scope Variables declared in outer scopes are visible to code inside inner scopes public static void main(String[] args) { int total = 15; int n = 5; if (n <= 10) { total = total + n; } System.out.println(total); } outer inner 24

25 Scope Variables declared in inner scopes are NOT visible to outer code public static void main(String[] args) { int n = 5; if (n <= 10) { int total = 15 + n; } System.out.println(total); // ERROR!!! } outer inner 25

26 else if (inputString.equals(“BLUE”)) eyeColor = Color.BLUE; else if (inputString.equals(“GREEN”)) eyeColor = Color.GREEN; else if (inputString.equals(“RED”)) eyeColor = Color.RED; else if (!inputString.equals(“BLUE”) && !inputString.equals(“GREEN”) && !inputString.equals(“RED”)) eyeColor = Color.WHITE; 26

27 else if (inputString.equals(“BLUE”)) eyeColor = Color.BLUE; else if (inputString.equals(“GREEN”)) eyeColor = Color.GREEN; else if (inputString.equals(“RED”)) eyeColor = Color.RED; else eyeColor = Color.WHITE; 27

28 else not needed when empty if (inputString.equals(“MOUTH”)) { mouthStartAngle = 0; } else { } 28

29 Multi-line comments /* This is a multi-line comment. What do you think of it? */ // You can also have multi-line comments // this way. 29

30 Too many comments Don’t need to comment every line of code, especially when it is obvious what your code is doing // set count to 12 count = 12; Only comment to enhance understanding and to explain why your code does what it does 30

31 Friday Program 2 due, 2pm Lab 4 help 31


Download ppt "COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014."

Similar presentations


Ads by Google