Control Structures 2 Chapter 6 Basic Loops When
Control Structures 2 Chapter 6
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 n or do. . . while loop Used to continue repetition while a condition holds ] for loop n Used to repeat a particular number of times
Definition of while Loop while ( condition ) { /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; }
Flowchart for a while loop condition T Statement 1: Statement n: F
Definition of do…while Loop do { /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; } while ( condition );
Flowchart for a do…while loop Statement 1: T Statement n: T condition F Loop condition
Definition FOR Loop for (initial statement ; loop condition ; update statement) { /*Series of actions to be taken each time the loop is executed*/ action 1; action 2; }
Flowchart for a for loop Initial statement Loop. Index=Start; Loop condition F Condition Update statement Loop. Index+=step T Statements in body of loop Statement 1: Statement n:
Control of while loops Ø Counter controlled Ø Sentinel controlled Ø Flag controlled Ø EOF controlled
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 Ø
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; }
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
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; }
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
End of File Controlled while loop input. Line = in. File. read. Line(); while ( input. Line != null ) { //Series of actions to be taken //each time the loop is executed tokenizer = new String. Tokenizer(input. Line); action 1; action n; input. Line = in. File. read. Line(); }
End of File Controlled while loop next. Value = in. File. read(); while ( nextvalue != -1 ) { //Series of actions to be taken //each time the loop is executed ch = (char)next. Value; action 1; action n; input. Line = in. File. read(); }
- Slides: 16