Iteration Structures

Download Report

Transcript Iteration Structures

Iteration Structures



Iteration is the third control
structure we will explore.
Iteration simply means to do
something repeatedly.
All iteration control structures test a
condition each time through the
loop.
Types of Loops




There are three types of loops that
Java employs.
Pretest Loop- test a condition each
time before the loop is executed.
Posttest Loop- test a condition
after each loop execution.
Fixed repetition- cause a loop to
be executed a predetermined
number of times.
Three Iteration Control Structures





The three iteration control
structures are the while, do/while,
and for.
The difference between them is the
means by which they control the
exiting of the loop.
The while is a pretest loop.
The do/while is a posttest loop.
The for is a fixed repetition loop.
The WHILE loop



If the test expression is true the loop
statements are executed.
If the test expression is false the loop
statements are bypassed.
To get out of the loop something must
change the expression to false,
otherwise the result will be an infinite
loop.
Syntax for the While Loop
while (test expression)
{
statement1;
statement2;
statementn;
}
The DO/WHILE loop



The do/while loop is tested at the end
of the loop compared to the while
which is tested at the beginning.
The loop statements will always be
executed at least once.
To break the loop the test expression
must become false, otherwise, just
like the while loop, the result will be
an infinite loop.
Syntax for DO/WHILE
do
{
statement1;
statement2;
statementn;
}
while (test expression);
Using Counters and Accumulators

Numeric variables used within a
repetition structure to calculate
subtotals, totals, averages and run the
loop.

Counter
Used for counting at constant rate.
 Ex. counter += 1 or counter++


Accumulator
Used for accumulating (adding together)
changing values.
 Ex. accumulator += value

Sentinel Values




Values used to end loops
Should be easily distinguishable
from the valid data used by the
program
Also called trip values or trailer
values
Should either be declared as a
constant variable or regular variable
and ask the user to enter a value.
The FOR loop




The for loop runs a fixed number of
times
The first thing that is done is the
initialization of the counter.
If the results of the test expression is
true, then the statements are
executed.
Each time the loop is executed, the
loop counter must be incremented or
decremented.
Syntax for FOR loop
for (initialize counter; test counter; update counter)
{
statement1;
statement2;
statementn;
}
Nested Loops



Many times it is desirable to have
looping operations within loops.
This is called nested looping.
Each inner loop will run as many
times as desired X the number of
times the outer loop runs.
Break and Continue Option


If you want a loop to terminate
when a desired value is found, it is
possible to use the break
statement to force the loop to quit.
If you want one iteration of the
loop to be terminated, it is possible
to use a continue statement to
force the loop to quit one iteration
and continue on with another.
Syntax for BREAK
while (test expression)
{
statement1;
statement2;
if (test expression)
break;
statementn;
}
Syntax for CONTINUE
for (initialize counter; test counter; update counter)
{
statement1;
statement2;
if (test expression)
continue;
statementn;
}
Number Formatting

Import


Instantiation



import java.text.DecimalFormat;
DecimalFormat twoDecimal = new DecimalFormat("0.00");
The numbers inside of parenthesis are the way in which you
want your numbers to show up when you output.
Ouput

twoDecimal.format(variable);
Example Formatting Program









import TerminalIO.KeyboardReader;
import java.text.DecimalFormat;
public class FormatExample
{
public static void main(String [] args)
{
KeyboardReader reader = new KeyboardReader();
DecimalFormat twoDecimal = new DecimalFormat("0.00");
DecimalFormat oneDecimal = new DecimalFormat("0.0");

double number;


number = reader.readDouble("Please enter a number: ");
System.out.println("Two decimal format: " +
twoDecimal.format(number));
System.out.println("One decimal format: " +
oneDecimal.format(number));






}
}