©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005...

21
©2004 Brooks/Cole Chapter 5 Repetition
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    0

Transcript of ©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005...

©2004 Brooks/Cole

Chapter 5

Repetition

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Repetition

• So far, our programs processed a single set of data.

• More useful is to be able to process many sets of data in the same way

• Repetition statements allow us to do this.• Java has three repetition statements

– while– do … while– for

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The while statement

• Syntaxwhile (<boolean expression>)

<while block>

• Semantics– test the condition– if true, execute the body and go back to test– if false, continue to next statement in program

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Anatomy of a while Loop

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Loop Example

int count = 1;

while (count <= 10)

{

System.out.println( count + " ");

count++;

}

• Body of loop needs to update the count variable– Otherwise, you will have an infinite loop

• Above is example of counting loop

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Loop Example

• Schematically, a while looks like the followingvar = initVar();

while (testVar())

{

doBody();

var = updateVar();

}

• Update can be any kind of change to var– increment, decrement, input, …

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Interactive Input

Reading a fixed number of values

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Accumulating Input Values

• Suppose we want to average the values we read using a loop.– Since we use the same variable for all the input

values, we need to add each value to the total as we enter it.

• Later we will learn to use arrays to save the values we read in.

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Accepting and Adding a Numberto a Total

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

AccumulationFlow of Control

AddNumbers.javaAverageNumbers.java

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Sentinel Loops

• Sometimes, we don't know how many values need to be entered ahead of time.

• In this case, we can read values until a specified sentinel value is entered.

• Example: Sentinels.java– A number greater than 100 will signal the end

of the input

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Interrupting loops

• Sometimes you need to exit a loop before it is finished– The break statement jumps to next statement

after the loop– loop and a half

• The continue statement causes a jump to the next iteration of the loop

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

For loops

• for syntax

for(init;

condition;

update)

statement;

• Equivalent while

init;while (condition) {

statement; update; }

Counting loops are common enough that Java provides a special syntax for them

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The for Statement’s Flow

of Control

1.init is done before the loop starts

2.condition is tested

3.statement is executed

4.update is executed

5. go back to 2

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Nested loops

• The body of a loop can include any valid Java statement

• If the body includes another loop, we say the loops are nested

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Nested loops

•The inner loop is executed completely for each iteration of the outer loop

•The body of the inner loop is executed 3*4=12 times

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

do - while loops

• Sometimes, it is more convenient to test the loop condition after the loop body has executed.do {

statement;

} while (condition);

• The body of the do-while loop always executes at least once

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

The do Statement’s

Flow of Control

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Input validation

• Loops are often used to validate inputdo

readData;

while (dataNotValid);

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Testing for valid data with Scanner

• The Scanner class has some methods you can use to check for the right kind of data.

boolean hasNext()

boolean hasNextInt()

boolean hasNextDouble()• You can use a loop to read for as long as

there is data of the proper type.

Figures ©2004 Brooks/ColeCS 119: Intro to Java Fall 2005

Reading to end of valid input

Scanner kbd

= new Scanner( system.in);

int value;

while ( Scanner.hasNextInt())

{

value = kbd.nextInt();

process data }