COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
-
Upload
evelyn-owen -
Category
Documents
-
view
215 -
download
0
description
Transcript of COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110COMP 110Augustus GAugustus Glooploop, Augustus G, Augustus Glooploop……
Luv KohliSeptember 24, 2008
MWF 2-2:50 pmSitterson 014
AnnouncementsAnnouncementsProgram 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
Some notes about printing your codeSome 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
Questions?Questions?
4
Today in COMP 110Today in COMP 110Nested loops
Some comments on Lab 3
5
Nested loopsNested loopsJust like we have nested if/else
statements, we can have nested loops
6
Nested loops example: friendly greetingsNested loops example: friendly greetings
7
Nested loops example: friendly greetingsNested loops example: friendly greetings
Student 1 shakes Student 4’s handStudent 1 shakes Student 5’s handStudent 1 shakes Student 6’s handStudent 2 shakes Student 4’s handStudent 2 shakes Student 5’s handStudent 2 shakes Student 6’s handStudent 3 shakes Student 4’s handStudent 3 shakes Student 5’s handStudent 3 shakes Student 6’s hand
8
Nested loops example: friendly greetingsNested loops example: friendly greetings
Student 1 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s handStudent 2 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s handStudent 3 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s hand
9
Nested loops example: friendly greetingsNested loops example: friendly greetings
for (every student in line A){ Student in line A shakes every Student’s hand in line B}
10
Nested loops example: friendly greetingsNested 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 loopOuter loop
Nested loops example: friendly greetingsNested 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
stdLineA123
stdLineB456
Nested loops example: friendly greetingsNested 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
Nested loopsNested loopsYou 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
Loop bugsLoop bugsInfinite loops – already talked about
these
Off-by-one errors
15
Off-by-one errorsOff-by-one errorsLoop 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
Finding errorsFinding errorsTrace 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 (4th edition), p. 218 (5th edition)
Use a debugger
17
What does this code do?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
What does this code do?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
What does this code do?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
What is wrong with this code?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
What is wrong with this code?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
IndentationIndentationIndentation◦Makes code easier to read◦Helps with finding syntax and logic errors◦ Indent code that goes between { and }
Be consistent!
23
ScopeScopeVariables 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
ScopeScopeVariables 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!!! }
outerinner
25
elseelseif (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
elseelseif (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
elseelse not needed when empty not needed when emptyif (inputString.equals(“MOUTH”)){ mouthStartAngle = 0;}else{}
28
Multi-line commentsMulti-line comments/* This is a multi-line comment. What do you think of it? */
// You can also have multi-line comments// this way.
29
Too many commentsToo many commentsDon’t need to comment every line of code,
especially when it is obvious what your code is doing
// set count to 12count = 12;
Only comment to enhance understanding and to explain why your code does what it does
30
FridayFridayProgram 2 due, 2pm
Lab 4 help
31 31