Title: COMP 110 Augustus Gloop, Augustus Gloop
1COMP 110Augustus Gloop, Augustus Gloop
- Luv Kohli
- September 24, 2008
- MWF 2-250 pm
- Sitterson 014
2Announcements
- 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
3Some 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
4Questions?
5Today in COMP 110
- Nested loops
- Some comments on Lab 3
6Nested loops
- Just like we have nested if/else statements, we
can have nested loops
7Nested loops example friendly greetings
8Nested loops example friendly greetings
- Student 1 shakes Student 4s hand
- Student 1 shakes Student 5s hand
- Student 1 shakes Student 6s hand
- Student 2 shakes Student 4s hand
- Student 2 shakes Student 5s hand
- Student 2 shakes Student 6s hand
- Student 3 shakes Student 4s hand
- Student 3 shakes Student 5s hand
- Student 3 shakes Student 6s hand
9Nested loops example friendly greetings
- Student 1 shakes Student 4s hand
- shakes Student 5s hand
- shakes Student 6s hand
- Student 2 shakes Student 4s hand
- shakes Student 5s hand
- shakes Student 6s hand
- Student 3 shakes Student 4s hand
- shakes Student 5s hand
- shakes Student 6s hand
10Nested loops example friendly greetings
- for (every student in line A)
-
- Student in line A shakes every Students hand
- in line B
11Nested 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
-
Outer loop
Inner loop
12Nested loops example friendly greetings
- for (int stdLineA 1 stdLineA lt 3 stdLineA)
-
- for (int stdLineB 4 stdLineB lt 6
stdLineB) -
- System.out.println(Student stdLineA
- shakes Student stdLineB
- s hand.)
-
stdLineA 1 2 3
stdLineB 4 5 6
13Nested loops example friendly greetings
- for (int stdLineA 1 stdLineA lt 3 stdLineA)
-
- for (int stdLineB 4 stdLineB lt 6
stdLineB) -
- System.out.println(Student stdLineA
- shakes Student stdLineB
- s hand.)
-
Inner loop
Outer loop
14Nested 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
15Loop bugs
- Infinite loops already talked about these
- Off-by-one errors
16Off-by-one errors
- Loop repeats one too many or one too few times
- for (count 1 count lt 10 count)
- // loop 9 times 1, 2, 3, ... 9
- for (count 1 count lt 10 count)
- // loop 10 times 1, 2, 3, ... 10
- for (count 0 count lt 10 count)
- // loop 10 times 0, 1, 2, ... 9
17Finding 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 (4th edition),
p. 218 (5th edition) - Use a debugger
18What does this code do?
- int a 0
- int b 0
- for (int i 1 i lt 6 i)
-
- if (i 2 0)
- b b i
- else
- a a i
-
19What does this code do?
- int oddSum 0
- int evenSum 0
- for (int i 1 i lt 6 i)
-
- if (i 2 0)
- evenSum evenSum i
- else
- oddSum oddSum i
-
20What does this code do?
- int oddSum 0
- int evenSum 0
- for (int i 1 i lt 6 i)
-
- if (i 2 0)
-
- evenSum evenSum i
-
- else
-
- oddSum oddSum i
-
21What is wrong with this code?
- int oddSum 0
- int evenSum 0
- for (int i 1 i lt 6 i)
-
- if (i 2 0)
- evenSum evenSum i
- else
- oddSum oddSum i
-
22What is wrong with this code?
- int oddSum 0
- int evenSum 0
- for (int i 1 i lt 6 i)
-
- if (i 2 0)
- evenSum evenSum i
-
- else
-
- oddSum oddSum i
-
23Indentation
- Indentation
- Makes code easier to read
- Helps with finding syntax and logic errors
- Indent code that goes between and
- Be consistent!
24Scope
- 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 lt 10)
-
- total total n
-
- System.out.println(total)
-
inner
outer
25Scope
- Variables declared in inner scopes are NOT
visible to outer code - public static void main(String args)
-
- int n 5
- if (n lt 10)
-
- int total 15 n
-
- System.out.println(total) // ERROR!!!
-
outer
inner
26else
- 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
27else
- 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
28else not needed when empty
- if (inputString.equals(MOUTH))
-
- mouthStartAngle 0
-
- else
-
29Multi-line comments
- / This is a multi-line comment.
- What do you think of it? /
- // You can also have multi-line comments
- // this way.
30Too many comments
- Dont 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
31Friday
- Program 2 due, 2pm
- Lab 4 help
31