Repetition Structures - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Repetition Structures

Description:

Minimum number of times executed is 0. Do While Loop Post-Test Loop ... do everything inside curly braces } // end of while loop. While Loop. int number = 3; ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 22
Provided by: debrac7
Category:

less

Transcript and Presenter's Notes

Title: Repetition Structures


1
Repetition Structures
2
3 Types of Control Structures
  • Sequence
  • Selection
  • If
  • IfElse
  • Repetition
  • Looping

3
Types of Loops
  • While Loop ? Pre-Test Loop
  • Test loop condition prior to loop code
  • Minimum number of times executed is 0
  • Do While Loop ? Post-Test Loop
  • Test loop condition after loop code
  • Minimum number of times executed is 1
  • For / Counted Loop

4
While Loop
  • General Syntax
  • while (condition) //while condition is true
  • // do everything inside curly braces
  • // end of while loop

5
While Loop
  • int number 3
  • while (number lt 5)
  • JOptionPane.showMessageDialog (Null, Hello)
  • number
  • // end while

6
3 Keys to Successful Looping
  • Initialize the Loop Control Variable (LCV)
  • (Give the LCV a beginning value)
  • Test the LCV for Exit Condition
  • Update the LCV
  • (Most overlooked step)

7
While Loop
  • initialize LCV //Key 1
  • While (condition) // Key 2
  • loop business
  • update LCV // Key 3
  • // close loop

8
Example of While Loop
  • Input / Data Validation
  • Ask the user for input, make sure the user inputs
    a proper data value (example 0 100)

9
While Loop
input JOptionPane.showInputDialog (Enter
Score ) number Integer.parseInt
(input) while (number lt 0 number gt 100)
JOptionPane.showMessageDialog (null, Bad
Input) input JOptionPane.showInputDialog
(Enter a new Score ) number
Integer.parseInt (input) // end while
10
Do While Loop
  • Post Test ? Move the test condition to after the
    looping code
  • General Syntax
  • do
  • init LCV // initialize update
  • Loop business
  • while (condition) //test exit condition

11
Do While Loop
do input JOptionPane.showInputDialog
(Enter Score ) number Integer.parseInt
(input) if (number lt 0 number gt 100)
JOptionPane.showMessageDialog (null, Bad
Input) input JOptionPane.showInputDialog
(Enter a new Score ) number
Integer.parseInt (input) // end if while
(number lt 0 number gt 100)
12
Sentinel Value Loop
  • User enters a Sentinel Value (a point as which to
    stop execution)
  • Example
  • Read in numbers from the user until a value of
    -1 (sentinel value) is input. Then output the
    count, sum and average of those numbers.

13
Pseudocode
  • Get first data value
  • While data value ! sentinel value
  • Process current value
  • Add to sum
  • Increment count
  • Get next data value / Update LCV
  • Determine Average
  • If count gt 0 then average sum/count
  • Else average 0
  • Output
  • Sum
  • Count
  • Ave

14
Data Dictionary
15
import javax.swing.JOptionPane class
Nums public static void main (String args)
String input int count 0
int sum 0 int number float
average final int END_MARKER 999
input JOptionPane.showInputDialog (Enter
Number ) number Integer.parseInt
(input) while (number !
END_MARKER) count // count count 1
(increment counter) // can also use
count(count count 1) sum number //sum
sum number input JOptionPane.showInputDialog
(Enter Number ) number Integer.parseInt
(input) // end while if (count
0) JOptionPane.showMessageDialog (null, No
Numbers Entered) else average
(float)sum / (float)count JOptionPane.showMessag
eDialog (null, Count is count) JOptionPane
.showMessageDialog (null, Sum is
sum) JOptionPane.showMessageDialog (null,
Average is average) // end else
// end main // end Nums
16
Control of Loops with Flags
  • Using boolean variables for control purposes
  • Set a flag when a condition is met
  • Change value of boolean to True

17
Boolean Flag
finished false // boolean flag while
(!finished) 1st part of loop body if
(criteria check) finished true else 2nd
part of loop // end while In this case the
2nd part of the loop body is only executed if
the exit criteria is not met.
18
Counted / For Loops
  • A Pre-test loop in which a counter is
    initialized, updated and checked to see if it has
    reached a terminal value
  • Runs a given number of times

19
General Syntax
  • for (initialization loop criteria step)
  • // looping code
  • // end for loop
  • Initialization clause initialize LCV
  • Loop clause Check LCV
  • Step clause Update LCV

20
Example
  • for (int I0 I lt 20 I)
  • JOptionPane.showMessageDialog (null, Counting
    Loop)
  • // end loop

21
Syntax to Remember
  • A semicolon at the end of a loop header causes a
    NULL Loop Statement
  • No Error Message
  • Loop Code never executes
  • For (int count 1 count lt 10 count)
  • System.out.print (Null Loop)
  • // end loop
  • While (counter lt -1)
  • // loop code
Write a Comment
User Comments (0)
About PowerShow.com