|
|
Line 1: |
Line 1: |
− | (Vic, Roger, David)
| + | {{1010Chapter}} |
− | | + | |
− | ==An Introduction==
| + | |
− | Until now all your programs have been working from top to bottom. Imagine if you were to write a game to guess a secret number from 1 to 100. The entire program would consist of one hundred [[Control Structures#The If Statement|if statements]] to account for each turn to check and see if the number chosen is correct; a situation like this is where a loop will come in to play and reduce those one hundred statements into one.
| + | |
− | | + | |
− | A loop is a [[Control Structures|control structure]] that allows you to repeat the same sequence of code as long as a given [[test condition]] evaluates to true. Every passage through this sequence of code is called an ''iteration''. If you repeat the same sequence of code 20 times, then you have performed 20 iterations.
| + | |
− | | + | |
− | ==Types of Loops==
| + | |
− | *[[While Loops|while]] -Vic
| + | |
− | *[[for]] -Roger
| + | |
− | *[[nested loops]] -Roger
| + | |
− | | + | |
− | ==[[Test_condition|Test Condition]]==
| + | |
− | Loops use the test condition to decide whether or not to enter the body of the loop.
| + | |
− | | + | |
− | ==[[Scope]]==
| + | |
− | Everything within the body of the loop is considered to be in the scope of the loop. In the case of a for loop, any variable declared in the inializer is considered to be within the scope of the loop. Declaring a variable within the variable within the scope of a loop is highly discouraged. This variable would be difined again everything time the loop iterates, causing a significant waste of memory.
| + | |
− | | + | |
− | ==Infinite Loops==
| + | |
− | An infinite loop is any loop that continues to repeat forever. There are situations where this is useful, but if you didn't intentionally program it in, then it causes a '''fatal''' error for your program. These occur if the test condition in your loop will never be false. Here are a few examples of infinite loops:
| + | |
− | | + | |
− | <pre>
| + | |
− | while(true) //always true
| + | |
− | { | + | |
− | }
| + | |
− | ------------------------------------------------
| + | |
− | for(int i = 0; i < 20; i++) //counter reset to 0 every time
| + | |
− | { | + | |
− | i = 0;
| + | |
− | } | + | |
− | ------------------------------------------------
| + | |
− | for(int i = 0; i < 9999999999999999; i++) //integers can't be as big as 9999999999999999, so 'i' will overflow to negative
| + | |
− | {
| + | |
− | } | + | |
− | </pre>
| + | |
− | | + | |
− | ==[[Additional Information]]==
| + | |
− | *[[Additional Information#Multiple Control Statments|Multiple Control Statements]]
| + | |
− | *[[Additional Information#Do-While Loops|The Evil Do-While Loop]]
| + | |
− | *[[Additional Information#Running Totals and Sentinel Values|Running Totals and Sentinel Values]]
| + | |
− | | + | |
− | | + | |
− | | + | |
− | ==[[Loop Review Questions and Exercises|Review Questions and Exercises]]==
| + | |
− | | + | |
− | Here you will find some Review Questions and Exercises to help you understand and and learn to use loops.
| + | |