Repetition - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Repetition

Description:

Increment and decrement can appear as a prefix or a postfix. Prefix increment/decrement ... postfix. Counter-Controlled Loops. Sometimes we want to execute a ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 27
Provided by: scie263
Category:

less

Transcript and Presenter's Notes

Title: Repetition


1
Chapter 4
  • Repetition

2
The Iteration Structure
  • Iteration structure (or loop) repeats a process
  • Uses a condition to control iteration
  • Loop continues as long as condition is TRUE
  • Pretest tests conditions at the start of the loop
  • Posttest tests conditions at the end of the loop
  • Endless loops never stop

3
Types of Loop Testing
4
Pretest Loops in Java
  • One pretest loop in Java is the while statement
  • A condition is used to control the loop
  • The condition is put between parentheses

while (condition) statement
while (condition) statement1
statement2 . . .
5
Example of while loop
/ print the numbers from 1 to 10 using the
pretest while loop / public class PrintNum
public static void main(String args) int
number 1 while ( number lt 10)
System.out.print(number ) number
number 1 System.out.println("\nThank
you.")
Output 1 2 3 4 5 6 7 8 9 10 Thank you.
6
Posttest Loops in Java
  • Posttest done with the do-while statement
  • Also uses a condition placed in parentheses

do statement while (condition)
do statement1 statement2 . .
. while (condition)
7
Example of do while
Output 1 2 3 4 5 6 7 8 9 10 Thank you.
/ print the numbers from 1 to 10 using the
pretest while loop / public class PrintNum
public static void main(String args) int
number 1 do System.out.print(numbe
r ) number number 1
while( number lt 10) System.out.println("\nT
hank you.")
8
Assignment
  • Write a program to calculate the factorial of 10.

9
Increment/Decrement Operators
  • Counting by 1 is a common operation
  • Increment uses
  • count or count
  • equivalent to
  • count count 1 or count 1
  • Decrement uses --
  • count-- or -- count
  • equivalent to
  • count count - 1 or count - 1

10
Increment/Decrement Operators (Cont ..)
  • Increment and decrement can appear as a prefix or
    a postfix
  • Prefix increment/decrement
  • Changes before the expression
  • count or --count
  • Postfix increment/decrement
  • Changes after the expression
  • count or count--

11
Increment/Decrement Operators (Cont ..)
double total, price 3.5 int quantity 2
12
Counter-Controlled Loops
  • Sometimes we want to execute a loop a specific
    number of times
  • Elements necessary for CCLs
  • Initialization give counter a starting value
  • Test usually a pretest
  • Body statement(s) to repeat
  • Counter value changed in each iteration
  • End sends program back to beginning of loop

13
Example of a CCL
Output 1 2 3 The value of count is 4
public class MyLoop public static void
main(String args) int count count
1 / Initialization /
while (count lt 3) / Test /
System.out.println(count) / Body /
count / Counter
/ / End
/ System.out.println("The value of
count is count)
14
The for Statement
  • Specially designed for counting loops
  • for (initialization test counter)
  • statement
  • for (initialization test counter)
  • statement1
  • statement2
  • . . .

15
The for Statement (Cont ..)
  • Actions of the for spread out around the loop
  • Initialization occurs only ONCE at the start
  • Testing is the first repeated action of the loop
    (it gets done before the body each iteration)
  • Counter occurs at the end of the loop body

16
Example
public class MyLoop public static void
main(String args) int count for
(count 1 count lt 3 count )
System.out.println(count)
System.out.println("The value of count is
count)
17
Example (Cont ..)
Output 1 2 3 The value of count is 4!
18
Assignment
  • Write a program to ask the user to enter 10
    integers and then find the maximum value.

19
Nested Loops
  • A loop within another is a nested loop
  • Nest as deep as you want, but
  • An inner loop must be entirely contained within
    an outer one
  • If the loops are counter controlled, each loop
    must have a different loop counter variable
  • Nested loops used often for rows and columns
  • Outer loop controls the rows
  • Inner loop controls the columns

20
Example of Nested Loops
Output 1 2 3 4 5 2 4 6 8 10 3 6 9
12 15 4 8 12 16 20
public class MoreLoops public static void
main(String args) int column, row
for (row 1 row lt 4 row) for
(column 1 column lt 5 column)
System.out.print(row column)
System.out.print("\n")
21
Assignment
  • Write a program to display the following output
  • 1
  • 1 2
  • 1 2 3
  • 1 2 3 4
  • 1 2 3 4 5

22
Validation Loops
  • Sometimes you may need to check for validity of
    input data
  • The user can enter negative value where it is
    supposed to be positive (year)
  • The user can enter a value that is outside the
    range of possible values (entering 110 for a test
    score)
  • This can be done with validation loops
  • Continue prompting the user for valid value as
    long as invalid value is entered

23
Sentinel Values
  • A special value of input that signals the program
    to behave in some special manner (like stopping a
    loop)
  • Must be a value that would not occur in the
    normal course of operation
  • i.e., a price and quantity of zero.

24
The break statement
  • The break statement can be used with switch or
    any of the 3 looping structures.
  • It causes an immediate exit from the switch,
    while, do while, or for structure in which it
    appears.
  • If the break is inside nested structures, control
    exits only the innermost structure containing it.

25
The continue statement
  • The continue statement is valid only within
    loops.
  • It terminates the current loop iteration, but not
    the entire loop.
  • In a for or while loop, continue causes the rest
    of the body statement to be skipped - in a for
    statement, the update is done.
  • In a do while loop, the exit condition is tested,
    and if true, the next loop iteration starts.

26
Loop Testing and Debugging
  • Beware of infinite loops - program doesnt stop
  • Check loop termination condition, and watch for
    off-by-1 problem
  • Trace execution of loop by hand with code
    walk-through
  • Use debug output statements
Write a Comment
User Comments (0)
About PowerShow.com