Presentation is loading. Please wait.

Presentation is loading. Please wait.

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.

Similar presentations


Presentation on theme: "LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the."— Presentation transcript:

1 LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the loop.

2 while Loops example: while { statement(s) } Semantics 1.A while statement that contains a boolean expression that must be true before the body of the loop will be executed. 2.The body of the loop (the part being repeated). We indent the body of the loop and use braces if it contains more than 1 statement. -Any boolean expression could be used for., including compound tests using && and ||. -The WHILE loop is considered a Pre-Test loop. That is, if the Boolean expression. is false the first time you execute the WHILE statement, then the body of the loop will not be executed at all.

3 while Loops (continued) The computer will execute the body of the WHILE loop as long as the b.e. is true. It is up to the programmer to make sure that the Boolean expression. eventually becomes false, or you will have created an infinite loop.

4 C++ for Engineers and Scientists, Second Edition 4 while Loops (continued)

5 C++ for Engineers and Scientists, Second Edition 5 while Loops (continued) break statement: forces an immediate break, or exit, from switch, while, for, and do-while statements break statement violates pure structured programming, but is useful for breaking out of loops when an unusual condition is detected

6 C++ for Engineers and Scientists, Second Edition 6 while Loops (continued) Example of a break statement:

7 C++ for Engineers and Scientists, Second Edition 7 while Loops (continued) continue statement: applies to while, do-while, and for statements; causes the next iteration of the loop to begin immediately continue statement is useful for skipping over data that should not be processed in this iteration, while staying within the loop

8 C++ for Engineers and Scientists, Second Edition 8 while Loops (continued) A continue statement where invalid grades are ignored, and only valid grades are added to the total:

9 for loop int var; for( var = start ; var < somenumber ; var++) { statement(s) } The for loop is made up of: 1.A for statement that determines the starting point, what condition will keep the loop going, and the increment of a counting variable. 2.The body of the loop (the part being repeated). We indent the body of the loop to improve readability. If there is more that one statement, beginning and end braces must be used.

10 for loop To use for loops, you must know – where to begin – how high (or low) to count – what to count by. If any of these 3 values is not known, then you shouldn't be using a for loop. The for loop is a pretest loop. If the middle condition is false, it will skip the loop entirely.

11 C++ for Engineers and Scientists, Second Edition 11 for Loops (continued)

12 do / while Loop example: do { statement(s); } while(Boolean expression); Is made up of three parts: 1.A do statement that marks the beginning of the loop 2.The body of the loop (the part being repeated). We indent the body of the loop to improve readability. If more than one statement is in body, beginning and ending braces are required. 3.A while statement that signals the end of the loop It will contain a condition for continuing. -The boolean expression could include compound tests using && and ||. -The do/while loop is considered a posttest loop. A posttest loop will always be executed at least once, whereas a pretest loop may not be executed at all.

13 C++ for Engineers and Scientists, Second Edition 13 Nested Loops Nested loop: a loop contained within another loop – All statements of the inner loop must be completely contained within the outer loop; no overlap allowed – Different variables must be used to control each loop – For each single iteration of the outer loop, the inner loop runs through all of its iterations

14 C++ for Engineers and Scientists, Second Edition 14 Nested Loops (continued)


Download ppt "LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the."

Similar presentations


Ads by Google