COMP 110 More loops - PowerPoint PPT Presentation

About This Presentation
Title:

COMP 110 More loops

Description:

Similar to while, but often more convenient syntax. 13. do-while loop. Similar to while loop ... while, but often more convenient syntax. 20. for loop ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 32
Provided by: csU49
Learn more at: http://www.cs.unc.edu
Category:
Tags: comp | convenient | loops | more

less

Transcript and Presenter's Notes

Title: COMP 110 More loops


1
COMP 110More loops
  • Luv Kohli
  • September 15, 2008
  • MWF 2-250 pm
  • Sitterson 014

1
2
Announcements
  • Facebook Tech Talk tonight, 6pm, SN011
  • Google info session, 6pm, SN014, tomorrow night

2
3
Questions?
3
4
Today in COMP 110
  • do-while loops
  • for loops

5
Review loops
  • Often you need to repeat an action in a program

6
Review loops
  • Loop part of a program that repeats
  • Body statements being repeated
  • Iteration each repetition of body
  • Stopping condition

7
Review Types of Loops
  • while
  • Safest choice
  • Not always most elegant
  • do-while
  • Loop iterates AT LEAST once
  • for
  • Similar to while, but often more convenient syntax

7
8
Review while loop
  • start
  • while (not enough sandwiches)
  • make a sandwich
  • distribute sandwiches

9
Review while loop syntax
  • while (boolean expression)
  • statements

10
Review Using a while loop
  • int n 1
  • while (n lt 10)
  • System.out.println(n)
  • n n 1

11
while loop
Evaluate Boolean expression
true
false
Execute Body
End loop
12
while loop
  • First, evaluate Boolean expression
  • If true, go into the body
  • If false, do not go into the body

13
Types of Loops
  • while
  • Safest choice
  • Not always most elegant
  • do-while
  • Loop iterates AT LEAST once
  • for
  • Similar to while, but often more convenient syntax

13
14
do-while loop
  • Similar to while loop
  • At least one iteration of the loop
  • First execute the loop body
  • Then evaluate the Boolean expression
  • If true, execute the body again
  • If false, quit the body

15
do-while loop
Equivalent
15
16
do-while loop syntax
  • do
  • statements // loop body
  • while (boolean expression)
  • If Boolean expression is true, repeat loop body
  • Otherwise, exit loop

17
Using a do-while loop
  • int n 1
  • do
  • System.out.println(n)
  • n n 1
  • while (n lt 10)

Dont forget the semicolon!
18
Using a do-while loop
  • What if n 20?
  • int n 20
  • do
  • System.out.println(n)
  • n n 1
  • while (n lt 10)

19
Infinite loop
  • int n 1
  • do
  • System.out.println(n)
  • // n n 1
  • while (n lt 10)

20
Types of Loops
  • while
  • Safest choice
  • Not always most elegant
  • do-while
  • Loop iterates AT LEAST once
  • for
  • Similar to while, but often more convenient syntax

20
21
for loop
  • Components of a for loop
  • Initializing actions
  • Boolean expression
  • Loop body
  • Update actions

22
for loop
22
23
for loop syntax
  • for (initializing actions Boolean expression
    update actions)
  • statements // loop body

24
Using a for loop
  • int n
  • for (n 1 n lt 10 n)
  • System.out.println(n)

25
while loops and for loops
  • int n
  • for (n 1 n lt 10 n)
  • System.out.println(n)

int n 1 while (n lt 10)
System.out.println(n) n n 1
26
for loop initializing actions
  • Only done ONCE at the beginning of the loop,
    before the Boolean expression is tested

int n for (n 1 n lt 10 n)
System.out.println(n)
27
for loop update actions
  • Executed at the end of every iteration
  • After the loop body, but before the next Boolean
    expression evaluation

int n for (n 1 n lt 10 n)
System.out.println(n)
28
Tracing a for loop
int n for (n 1 n lt 3 n)
System.out.println(n) 1 2 3
n ? n 1 n 2 n 3 n 4
Execute initializing actions Evaluate Boolean
expression Execute body Execute update actions
29
Infinite loop
  • int n
  • for (n 1 n lt 10 n 0)
  • System.out.println(n)

30
Types of Loops
  • while
  • Safest choice
  • Not always most elegant
  • do-while
  • Loop iterates AT LEAST once
  • for
  • Similar to while, but often more convenient syntax

30
31
Wednesday
  • Spend some more time with loops

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