Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Structures 2 Chapter 6. © Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition.

Similar presentations


Presentation on theme: "Control Structures 2 Chapter 6. © Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition."— Presentation transcript:

1 Control Structures 2 Chapter 6

2 © Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition structures  There are two common types of loops  while loop or do...while loop Used to continue repetition while a condition holds Used to continue repetition while a condition holds  for loop Used to repeat a particular number of times Used to repeat a particular number of times

3 © Janice Regan 2003 Definition of while Loop while ( condition ) while ( condition ) { /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; }

4 © Janice Regan 2003 Flowchart for a while loop T F condition Statement 1: Statement n:

5 © Janice Regan 2003 Definition of do…while Loop do do{ /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; } while ( condition );

6 © Janice Regan 2003 Flowchart for a do…while loop T F condition Statement 1: Statement n: T Loop condition

7 © Janice Regan 2003 Definition FOR Loop for ( initial statement ; loop condition ; update statement) for ( initial statement ; loop condition ; update statement){ /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; }

8 © Janice Regan 2003 Flowchart for a for loop T F Condition LoopIndex+=step LoopIndex=Start; Statement 1: Statement n: Initial statement Update statement Loop condition Statements in body of loop

9 © Janice Regan 2003 Control of while loops  Counter controlled  Sentinel controlled  Flag controlled  EOF controlled

10 © Janice Regan 2003 Sentinel Controlled while loops  Sentinel value is a special value of a variable in the loop condition that makes the loop condition false.  The sentinel value of a sentinel variable must be a value of the variable that is not encountered in normal operation of the loop  When the sentinel variable has the sentinel value at the end of the statements in the loop, execution will pass to the next statement following the loop  Often used for reading unknown amounts of input data

11 © Janice Regan 2003 Sentinel Controlled while loop while ( sentinel variable != sentinel value ) { //Series of actions to be taken //each time the loop is executed //one of these actions will change the value // of the sentinel variable action 1; action 2; }

12 © Janice Regan 2003 Flag Controlled while loops  Uses a boolean variable to control the loop  The boolean value is set before the loop begins to execute  At some point a condition within the loop is met and the value of the boolean variable is changed  The next time the loop condition is tested execution passes to the statement following the loop

13 © Janice Regan 2003 Flag Controlled while loop flagvalue = false; while ( !flagvalue ) { //Series of actions to be taken //each time the loop is executed action 1; if(expression) flagvalue = true; action n; }

14 © Janice Regan 2003 End of File (EOF) Controlled while loops  Uses an end of file condition to control the loop  The end of file condition occurs when the program attempts to read data after the end of the file is reached

15 © Janice Regan 2003 End of File Controlled while loop inputLine = inFile.readLine(); while ( inputLine != null ) { //Series of actions to be taken //each time the loop is executed tokenizer = new StringTokenizer(inputLine); action 1; action n; inputLine = inFile.readLine(); }

16 © Janice Regan 2003 End of File Controlled while loop nextValue = inFile.read(); while ( nextvalue != -1 ) { //Series of actions to be taken //each time the loop is executed ch = (char)nextValue; action 1; action n; inputLine = inFile.read(); }


Download ppt "Control Structures 2 Chapter 6. © Janice Regan 2003 Basic Loops  When one action is to be repeated a number of times a loop is used. Loops are repetition."

Similar presentations


Ads by Google