1 / 17

Iterative Statements

Iterative Statements. Contents. Form of an iterative statement The while-loop The do-while loop The for-loop Comparison between while-loop and for-loop Accumulation. The while - loop. All iterative statements must have. An initialization statement.

licia
Télécharger la présentation

Iterative Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterative Statements Contents Form of an iterative statement The while-loop The do-while loop The for-loop Comparison between while-loop and for-loop Accumulation

  2. The while - loop All iterative statements must have An initialization statement. A stopping condition (A continuation condition is not true) A statement that moves the loop toward termination A while - loop is Initialized before the start of the loop Tests for continuation before the start of each iteration Contains a statement in the body of the loop that moves the loop toward termination.

  3. Initialize the variant index The continuation condition appears in parentheses after the keyword while A statement inside the loop causes the variant to increase (and the loop to proceed toward termination) The while - loop //declare and initialize variable max int index = 0; while (index < max) { //do something index++; } Example int index = 0, sum = 0; //initialize the variant and accumulator while (index < 10) { //test for continuation -- stopping condition is false sum += index; //do something index++; //increment the variant -- proceed toward termination }

  4. The while -- loop The continuation condition is tested BEFORE each iteration of the loop. The body of a while loop is iterated 0 or more times. (The continuation condition may be false the first time it is tested) If the variant is not changed inside the body of a while - loop, the loop will never terminate, and the program will hang during execution. The while - loop is the only iterative statement that is needed, but two additional iterative forms exist in java to facilitate programming the various situations in which iteration occurs.

  5. The do - while loop The do - while loop tests the continuation condition AFTER every iteration of the loop. The body of a do-while loop is executed at least once. The do-while loop has the following form: Initialization of the variant and other variables do { body of the loop -- perform some action change the variant -- to proceed toward termination } while (continuation condition) //condition is tested - continue if true

  6. The do-while loop A do-while loop should only be used in a situation in which you know there will be at least one iteration of the loop. Consider a menu object with methods show( ) {which displays a numbered list of choices} and getResponse( ) which returns the numerical value selected. finalint quit = 5; int ans; menu.show( ); ans = menu.getResponse( ); while (ans != quit) { menu.show( ); ans = menu.getResponse( ); } finalint quit = 5; int ans; do { menu.show( ); ans = menu.getResponse( ); } while (ans != quit) while-loop requires pre-test or an artifice such as setting ans < 5 to start do-while loop provides a more straightforward “cleaner” code

  7. The for -- loop The for-loop is generally used when a pre-determined number of iterations is specified, but in java, the for-loop can be used wherever the while-loop is used. Some java programmers will use a for-loop for every iterative statement. while-loop for-loop int sum = 0; for(intindex = 0; index < 10; index++) { sum += index; } //initialization intindex = 0, sum = 0; while(index < 10) {//test sum += index; index++; //inc. variant } Initialization part Test for continution Proceed toward termination

  8. The for-loop The for statement has three components for (int index = 0; index < 10; index++ ) An initialization clause A test for continuation A move toward termination In a while-loop The initialization clause preceeds the while statement int index = 0; while (index < 10) { The while statement tests for continuation …… index++; } The variant is changed inside the body of the while statement

  9. Accumulation Iteration is used to add (or multiply) a sequence of terms (factors) Example: add the numbers from 1 to 100 Using a while-loop Using a for-loop int sum = 0, term = 1; while (term <= 100) { sum += term; term++; } int sum = 0; for (int term = 1; term <= 100; term++) sum += term; During each iteration, a new term is added to the sum!

  10. <Statement> //initialization <condition> Entry false <Statement1> true <StatementN> Semantics of a while loop Steps 1. initialization 2. Evauate <condition> 3. if <condition> is true, execute statements 1 ..N inside the while- loop block 4. Go to step 2. 5. else( <condition> is false), go to step 6 6. Execute statements following the loop Exit

  11. <condition> Entry <Statement1> <StatementN> true Exit Semantics of a do-while loop Steps 1. Execute <statement1> ..<StatementN> 2. Evaluate <condition> 3. if <condition> is false, go to step 5. (Exit) 4. else, go to step 1. 5. Execute statements following the loop. false

  12. Semantics of a for-loop • Execute <Initialization Statement> • Evaluate <condition> • If <condition> is false, go to step 7. • Else (<condition> is true) execute <statement1>…<statementN> • Execute <Increment Statement> • Go to step 2. • Execute statements following the for-loop block

  13. <Statement> //initialization <condition> false <Statement1> true <StatementN> <Increment> Semantics of a for-loop Entry Exit

  14. Converting a while-loop into a do-while while-loop do-while loop if (<condition>) { do { <statements> } while(<condition>); //initialization while (<condition>) { <statements> }

  15. Converting a do-while loop into a while-loop do-while loop while-loop <statements> while (<condition>) { <statements> } do { <statements> } while(<condition>) The statement sequence inside the loop must reset the condition so that it eventually tests false. The statement sequence before the loop and inside the loop are the same. Both reset the condition to be tested.

  16. Terminating a loop Loop forever and terminate with a break for ( ; ; ) { //do something if (boolean-condition) break; //zero or more additional statements to be executed if condition is false } //after the break control moves to here – the next statement after the loop //no variant is initialized and incremented, no test for termination is done – program will loop forever without a break

  17. When no values remain to be read null returned If readLine( ) encounters an empty line in the file, a null string is returned Termination of a loop The continue statement Consider the following example String inStr = “”; double value; try { BufferedReader in = new BufferedReader(new FileReader(“input.txt”)); for( ; ; ) { inStr = in.readLine( ); if (inStr == null) break; if (inStr.equals(“”) ) continue; //do something more } After continue, return to the top of the loop and read new input When break is executed, control leaves the loop and jumps to here

More Related