+ 1
Why is the result of this loop with continue function 16?
At the moment im at the break and continue lesson, but I can't understand why this executes as 16 as a result? https://code.sololearn.com/WMFYTR4YB62x as far as I understand. The loop in general runs as long as the value of i is under 8. The variable i starts with the value 4. And the 6th run gets skipped because of the continue function. After every run the variable I gets added up by 1 and after every run the loop calculates: sum (0) + i (4,5,7,8). So for me it looks like: Run 1: 0 + 4 Run 2: 0 + 5 Run 3: 0+ 7 Run 4: 0 + 8 I don't seem to find the 16? help!
1 Antwort
+ 3
You already said it yourself, the loop runs while the value of 'i' is less than 8. What is important is that the condition is checked before the body is entered, so when 'i' becomes 8 at the end of the fourth (third, if you don't count the continued one) iteration, the condition becomes false and the loop terminates before it would be added to the sum again during the fifth iteration. Therefore, what is calculated is 4 + 5 + 7 = 16, not 4 + 5 + 7 + 8 = 24.