Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repeating Actions While and For Loops

Similar presentations


Presentation on theme: "Repeating Actions While and For Loops"— Presentation transcript:

1 Repeating Actions While and For Loops

2 Forms of the Repetition Structure
For Next loops Count controlled Do While loops Condition controlled

3 For Next Loops Tells the computer to repeat one or more statements a specified number of times. Called a pretest loop because the loop is evaluated before the instructions are processed. For loops are used to repeat actions a predetermined number of times.

4 For Next Loop Tasks The loop initializes the counter (often called the index) variable to the startvalue (done only once, at the beginning of the loop). If the stepvalue is positive, the loop tests if the value in counter is greater than the endvalue (when negative the test is less than). If it is, the loop stops; otherwise the instructions within the loop are processed. The loop increments the counter by the stepvalue. It then tests the condition again.

5 For Loop in VL The for loop element displays the loop variable, initial value, final value, and step by value if it is something other than 1

6 Nested Loops For loops allow code inside the body of the loop to be repeated many times. A nested loop refers to a loop contained inside the body of another loop.

7 Nested For Loops For each time through the outer loop, the inner loop executes from initial to final by step times.

8 Nesting At The Same Level

9 Diamond Problem

10 Do While Loops Do While loops are used when an action is to be repeated an unknown number of times. A pre-test loop tests the looping condition before executing the body of the loop. The body of the loop may not be executed. A post-test loop guarantees at least one execution of the loop body regardless of the condition. The test is after the body of the loop.

11 Pre-test loop Note: This loop may or may not be executed at all True
Do It? False Note: This loop may or may not be executed at all

12 Post-test Loop Note: This loop will be executed at least once True
Again? False Note: This loop will be executed at least once

13 Sentinel Value A sentinel value (or signaling value) is used to indicate the end of input. The solution flowchart in Figure 3-13 shows -1 as the sentinel value which is checked twice (if and while).

14 Grocery Checkout

15 Counters and Accumulators
Used within a repetition structure to calculate subtotals, totals, and averages Initialized (usually to 0 or 1) outside the loop and updated within the loop A counter is a numeric variable used for counting something and is updated by 1 An accumulator is a numeric variable used for accumulating (adding together) and is updated by an amount that varies

16 Counters and Accumulators
Counters are variables that keep track of how many times a statement has executed. Counter = Counter + 1 Accumulators are variables that maintain a running total. Accumulator = Accumulator + NewValue

17 Nested While Loops

18 Endless Loops When writing loops you must be sure that the condition will terminate the loop or you have what is called an endless or infinite loop. When you write a loop you must be careful to increment the variable or input a new value inside the loop so it will terminate.


Download ppt "Repeating Actions While and For Loops"

Similar presentations


Ads by Google