Vous êtes sur la page 1sur 2

Loops

Sunday, January 27, 2013 6:36 PM

Each loop has a different structure. Here, I will attempt to deconstruct each loop and tell you what it means. Loops in general Any value initiated inside a loop disappears when the loop terminates For loops for (int i = x; i <= y; i++) { ... } for initiates the loop int i = x initiates the integer i. It doesn't necessarily have to be i, but it is standard naming convention to use i. Also, x is not really x. It should be replaced with an integer value, like 10 . i <= y creates a condition for the loop. It tells the loop that it should run as long as i <= y. Again, y is not really y. It should be replaced with an integer value, like 20. Note, this can be several different things. i > 10, i = 45, etc. i++ is the loop iterator. It tells the loop to add 1 to i at the end of the loop. You can also have i-as a loop iterator, which tells the loop to take away 1 from i at the end of the loop. The things contained between { and } are the body of the loop. It tells the loop what to do every time it runs. A loop works as follows: first, the loop is initiated. It checks to see that it's condition evaluates to true. If it does, it carries out the instructions in the body of the loop. Then it adds 1 to the integer i. Then it checks the condition again. If at any time the condition evaluates to false, the loop does not run. It is possible for the condition to be false at the start, in which case the loop does not run at all. When answering questions about loops, any kind of loops, you are likely to be asked how many times the loop is iterated (how many times it runs), what is printed during the execution of the loop (if it prints something at all), and what the value of i is when the loop condition finally fails. Often, several questions will be assigned to a single loop. The easiest and most comprehensive way to answer these kinds of questions is to write a loop analysis. A loop analysis is a list of the outcomes of all passes (runs / executions of the body of the loop) until the loop finally fails. Ex: Create a loop analysis for the following loop: for (int i = 10; i <= 20; i++) { if (i % 3 < 2) { cout << i << " "; } else { cout << i++ << " "' } } Loop analysis: 1st pass: 10%3 = 1, which is <2, so the if condition is true. The loop prints 10, and the loop iterator increments i to 11. 11<=20, so the loop iterates (runs) again.

Computer Science Page 1

2nd pass: 11%3 = 2, which is not <2, so the if condition is false. Print 11, and increment i to 12. The loop iterator increments i again to 13. 13<=20, so the loop iterates again. 3rd pass: 13%3 = 1, which is <2, so the if condition is true. The loop prints 13, and increments i to 14. 14<=20 is true, so the loop iterates again. 4th pass: 14%3 = 2, which is not <2, so the if condition is false. The loop prints 14 and increments i to 15, and the loop iterator increments i to 16. 16<=20, so the loop iterates again. 5th pass: 16%3 = 1, which is <2, so the if condition is true. The loop prints 16 and increments i to 17. 17<=20, so the loop iterates again. 6th pass: 17%3 = 2, which is not <2, so the if condition is false. The loop prints 17 and increments i to 18. The loop iterator increments i again to 19. 18<=20, so the loop iterates again. 7th pass: 19%3 = 1, which is <2, so the if condition is true. The loop prints 19 and increments i to 20. 20<=20, so the loop iterates again. 8th pass: 20%3 = 2, which is not <2, so the if condition is false. The loop prints 20 and increments i to 21. The loop iterator increments i again to 22. 22 is not <=20, so the loop terminates. A shorter version of this analysis (and the one you should write for the test, if need be, is): 1: true, print 10, i=11 2: false, print 11, i=13 3: true, print 13, i=14 4: false, print 14, i=16 5: true, print 16, i=17 6: false, print 17, i=19 7: true, print 19, i=20 8: false, print 20, i=22 From this analysis, we can draw the following answers to the previous questions: the loop iterates eight times, the loop prints 10 11 13 14 16 17 19 20, and the value of i when the loop terminates is 22.

Computer Science Page 2

Vous aimerez peut-être aussi