More loops - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

More loops

Description:

When to use a do-loop. Use this form when you want the loop body to ... The 3 elements of the for-loop header are sparated by semi-colons, not commas! Do this: ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 24
Provided by: cspee
Category:

less

Transcript and Presenter's Notes

Title: More loops


1
More loops
  • Linag, Chpt 3, pp 74-90

2
The do-loop
DO-LOOP
WHILE-LOOP
3
When to use a do-loop
Use this form when you want the loop body to be
executed at least once
int data int sum 0 do data
MyInput.readInt() sum data while (data
! 0) System.out.println(The sum is sum)
4
Things to know about do-loops
  • The loop body will always be executed one
  • The first execution is done before the guard is
    checked
  • The do-loop is nothing more than a small variant
    of the while-loop
  • It is often useful for checking user input

5
Use of the do-loop
System.out.print(Enter a number ) int input
MyInput.readInt() while(input ! 0) ....
System.out.print(Enter a number ) input
MyInput.readInt()
6
A better way
int input do System.out.print(Enter a
number input MyInput.readInt() ...
while(input ! 0)
7
The for-loop
  • The for-loop is another variant of the while
    loop
  • Every for-loop can be written as a while loop
  • Not every while-loop can be written as a
    for-loop
  • Use a for-loop when you know exactly how many
    times the loop body should be executed

8
From while to for
int i startValue while (i lt endValue)
..... i
for (int istartValue iltendValue i) ...
9
Three things in one line
for ( ltinitializationgt ltguardgt ltadjustmentgt)
...
  • Initialization (declare and) set a counter, to
    0. This is done before everything else.
  • Guard loop ends when this is false. This is
    tested before each pass through the loop.
  • Adjustment e.g. increment a counter. This is
    done at the end of each pass through the loop.

10
Examples
for (int i0 ilt10 i) System.out.print(i
)
int i for (i10 igt0 i--)
System.out.print(Ouch...)
11
Flow of control in a for-loop
initialization
false
true
12
Rules and guidelines
  • Counters can be declared and initialized in one
    go
  • Never (never) change the value of the counter
    inside the loop body
  • I mean it. Never do that!
  • If the loop body is one statement only, you can
    omit the bracesbut please dont!
  • Indent the code within the loop body..

13
Examples
// TestSum.java Compute sum 0.01 0.02
1 ... float sum 0 // Keep adding
0.01 to sum for (float i0.01f i lt 1.0f i
i0.01f) sum i
System.out.println("The summation is " sum)
14
Common error 7 490
  • The 3 elements of the for-loop header are
    sparated by semi-colons, not commas!
  • Do this
  • for (int i0 ilt10 i)
  • Not this
  • for (int i0, ilt10, i)
  • Keep your loops as simple as possible

15
Nesting for-loops
  • Inside the loop body of a for-loop, we can put
    another for-loop
  • Each time through the 1st for-loop, we execute
    the 2nd loop until its guard is false
  • Handy for printing tables like this
  • 1 1 1 1 1 1
  • 2 2 2 2 2 2
  • 3 3 3 3 3 3
  • 4 4 4 4 4 4

16
Simple example
for (int i0 ilt5 i) for (int j0 jlt3
j) System.out.print( )
System.out.println()
17
Sample problem, p. 78
Multiplication table -------------------
-------------- 1 2 3 4 5 6 7 8 9 1
1 2 3 4 5 6 7 8 9 2 2 4 6 8 10
12 14 16 18 3 3 6 9 12 15 18 21 24 27 4 4
8 12 16 20 24 28 32 36 5 5 10 15 20 25 30 35
40 45
18
Nested for-loop example p. 78
// Print table body for (int i1 ilt9 i)
System.out.print(i" ") for
(int j1 jlt9 j) // Display
the product and align properly if (ij lt
10) System.out.print(" " ij)
else System.out.print(" " ij)
System.out.println()
19
break-ing out of a loop
  • Inside a loop body, the statement break causes
    execution to leave the loop immediately.
  • This is usually unnecessary, and shows that not
    enough though has gone into the development of a
    guard.

20
Example of break
int sum 0 int item 0 while
(item lt 5) item sum
item if (sum gt 6) break
System.out.println("The sum is " sum)
21
continue
  • This keyword also stops interation of the loop
    body
  • But...the program then starts the next iteration
    of the loop (testing the loop guard first)
  • This is also usually unnecessary, and can be
    replaced by an if-statement.

22
Example of continue
int sum 0 int item 0 while (item lt 5)
item if (item 2) continue sum
item System.out.println("The sum is " sum)
23
Case studies for study
  • Study cases 3.7, 3.8 and 3.9,
  • Liang, pp 8489
  • Compile and run the programs
  • Study the logic of flow control
  • Then go outside and do something else
Write a Comment
User Comments (0)
About PowerShow.com