| << 5.3.1- Branching Structures | Chapter5 | 5.3.3- Jumping Controls >> |
Looping Controls
Looping controls allow the same block of code to be run a number of times. Instead of skipping code – which is what the branching technique does – we repeat code. In the example of the mechanic, the idea of completing all the steps for the first truck, and then going back and repeating those same steps for the rest of the trucks demonstrates looping. In our clothing example we may want to generate a page with each item ordered listed on a line. The construction of those lines (print the description, print the quantity, print the price, put in a line break) would be looped to produce one line for each item ordered.
- For Next is used to repeat line(s) when, at the beginning of the repetition, we know exactly how many times we wish to repeat the line(s), or we can use a test (such as the sizeof function) to determine the amount of repetition we want. For example, if we know there are five trucks needing a wash, we could repeat five times the set of steps involved in successfully cleaning each truck.
- Do While is used to repeat line(s) when we don't know at design time how many times we wish to repeat our line(s). We build into the loop some type of test of condition, which is checked after each loop. The loop will repeat as long as the condition is true.
| << 5.3.1- Branching Structures | Chapter5 | 5.3.3- Jumping Controls >> |

RSS
