Iteration Structures

17
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.

description

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. - PowerPoint PPT Presentation

Transcript of Iteration Structures

Page 1: 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.

Page 2: Iteration Structures

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.

Page 3: Iteration Structures

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.

Page 4: Iteration Structures

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.

Page 5: Iteration Structures

Syntax for the While Loop

while (test expression)

{

statement1;

statement2;

statementn;

}

Page 6: Iteration Structures

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.

Page 7: Iteration Structures

Syntax for DO/WHILE

do

{

statement1;

statement2;

statementn;

}

while (test expression);

Page 8: Iteration Structures

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

Page 9: Iteration Structures

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.

Page 10: Iteration Structures

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.

Page 11: Iteration Structures

Syntax for FOR loop

for (initialize counter; test counter; update counter)

{

statement1;

statement2;

statementn;

}

Page 12: Iteration Structures

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.

Page 13: Iteration Structures

Break and Continue Option

If you want a loop to terminate when a desired value is found, it is possible to use the breakbreak statement to force the loop to quit.

If you want one iteration of the loop to be terminated, it is possible to use a continuecontinue statement to force the loop to quit one iteration and continue on with another.

Page 14: Iteration Structures

Syntax for BREAK

while (test expression)

{

statement1;statement2;if (test expression)

break;statementn;

}

Page 15: Iteration Structures

Syntax for CONTINUE

for (initialize counter; test counter; update counter)

{

statement1;

statement2;

if (test expression)

continue;

statementn;

}

Page 16: Iteration Structures

Number Formatting

Import import java.text.DecimalFormat;

Instantiation 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);

Page 17: Iteration Structures

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));

} }