Lecture 6: for Loops - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Lecture 6: for Loops

Description:

initialization is performed once before the loop starts. It initial-izes the counter. ... HW3 out today, due Wednesday. Black Jack. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 11
Provided by: stephenr9
Category:
Tags: lecture | loops

less

Transcript and Presenter's Notes

Title: Lecture 6: for Loops


1
Lecture 6 for Loops
  • Yoni Fridman
  • 7/6/01

2
Outline
  • The for statement
  • The for statements format
  • The for statements flow
  • Comparing while, do, and for
  • Loop examples
  • Debugging control structures

3
The for Statement
  • Remember this while loop?
  • int i 1
  • while (i lt 5)
  • System.out.println(i)
  • i
  • It can be described as follows
  • initialization
  • while (test)
  • statement
  • update
  • The for loop condenses this
  • for (initialization test update)
  • statement

4
The for Statements Format
for (initialization test update) statement
5
The for Statements Flow
for (initialization test update)
statement
6
The for Statement (Side Notes)
  • The initialization, test, and update need not all
    operate on the counter variable.
  • The initialization and update need not have one
    statement.
  • They can be empty (have no statements), in which
    case they must be performed elsewhere. The
    semicolons must still be included.
  • They can have multiple statements, separated by
    commas.
  • The test can be empty it defaults to true.
  • In this case, the loop must have a break, or it
    will iterate forever.
  • The initialization need not declare the counter.
  • IMPORTANT Variables CAN be redeclared within
    for statements.

7
Comparing while, do, and for
  • Why use for instead of while? Same old reasons
  • Its shorter.
  • It makes the program more readable all the
    vital information (initialization, test, and
    update) can easily be found in one place.
  • So when do you use which loop?
  • for is used in loops with counters.
  • while is used in loops without counters.
  • do is used in loops without counters, when you
    must guarantee at least one iteration.

8
Loop Examples
  • Powers of 2 (while loop)
  • ltCode Examplegt
  • Factorials (for loop)
  • ltCode Examplegt
  • Fibonacci numbers (for loop)
  • ltCode Examplegt

9
Debugging Control Structures
  • Debugging becomes more complicated once control
    structures have been introduces.
  • When testing your program, test the program on
    various input data.
  • Make sure each statement of your code is executed
    at least once.
  • Put println() statements within control
    structures.
  • Common errors to watch out for
  • Off-by-one errors, for example, using i lt n
    instead of i lt n.
  • Infinite loops.
  • Never-executed loops or if statements.
  • Using the debugger An example.

10
Homework
  • Read 5.1, 5.2 (bottom of p. 193 to bottom of p.
    194), 5.3, 5.4.
  • HW3 out today, due Wednesday.
  • Black Jack.
Write a Comment
User Comments (0)
About PowerShow.com