0

Loop, break and continue second question

I can't understand why the result Is 16, for me It would be 13, not 16.

11th Feb 2019, 5:53 PM
CauĂŁ Paz
CauĂŁ Paz - avatar
1 Answer
+ 9
Continue acts like a skip , so when you tell to continue something, it skips. Lets break the question down.. for(i=4 ; i<8 ; i++) : this for loop will generate 4 values i.e 4,5,6,7 respectively. Now if i == 6 it says to continue (lets call it skip) Otherwise , it adds the i to the variable called 'sum' which is initialized to 0. So now when: i = 4 sum = 0+4 => 4 i=5 sum = 4+5 => 9 i=6 continue i.e skip i=7 sum = 9+7 => 16 Finally print sum which is now 16
11th Feb 2019, 6:12 PM
Frost
Frost - avatar