COMP 110 Augustus Gloop, Augustus Gloop - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 110 Augustus Gloop, Augustus Gloop

Description:

Nested loops example: friendly greetings. Student 1 shakes Student 4's hand ... Nested loops example: friendly greetings. for (int stdLineA = 1; stdLineA = 3; ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 32
Provided by: csU65
Learn more at: http://www.cs.unc.edu
Category:

less

Transcript and Presenter's Notes

Title: COMP 110 Augustus Gloop, Augustus Gloop


1
COMP 110Augustus Gloop, Augustus Gloop
  • Luv Kohli
  • September 24, 2008
  • MWF 2-250 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

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

4
Questions?
5
Today in COMP 110
  • Nested loops
  • Some comments on Lab 3

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

7
Nested loops example friendly greetings
8
Nested 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

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

10
Nested loops example friendly greetings
  • for (every student in line A)
  • Student in line A shakes every Students hand
  • in line B

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

Outer loop
Inner loop
12
Nested 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
13
Nested 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
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

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

16
Off-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

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 (4th edition),
    p. 218 (5th edition)
  • Use a debugger

18
What 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

19
What 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

20
What 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

21
What 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

22
What 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

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

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 lt 10)
  • total total n
  • System.out.println(total)

inner
outer
25
Scope
  • 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
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

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

28
else not needed when empty
  • if (inputString.equals(MOUTH))
  • mouthStartAngle 0
  • else

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.

30
Too 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

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

31
Write a Comment
User Comments (0)
About PowerShow.com