Title: Repetition Structures
1Repetition Structures
23 Types of Control Structures
- Sequence
- Selection
- If
- IfElse
- Repetition
- Looping
3Types 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
4While Loop
- General Syntax
- while (condition) //while condition is true
- // do everything inside curly braces
- // end of while loop
5While Loop
- int number 3
- while (number lt 5)
-
- JOptionPane.showMessageDialog (Null, Hello)
- number
- // end while
63 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)
7While Loop
- initialize LCV //Key 1
- While (condition) // Key 2
- loop business
- update LCV // Key 3
- // close loop
8Example of While Loop
- Input / Data Validation
- Ask the user for input, make sure the user inputs
a proper data value (example 0 100)
9While 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
10Do 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
11Do 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)
12Sentinel 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.
13Pseudocode
- 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
14Data Dictionary
15import 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
16Control of Loops with Flags
- Using boolean variables for control purposes
- Set a flag when a condition is met
- Change value of boolean to True
17Boolean 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.
18Counted / 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
19General Syntax
- for (initialization loop criteria step)
- // looping code
- // end for loop
- Initialization clause initialize LCV
- Loop clause Check LCV
- Step clause Update LCV
20Example
- for (int I0 I lt 20 I)
-
- JOptionPane.showMessageDialog (null, Counting
Loop) - // end loop
21Syntax 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
-