+ 1

Pls can someone tell me why the answer is 30 and not 27.

int i=6; for(i;i<=9;i++){} Cout << 3*i;

14th Jul 2019, 8:26 PM
Aivic
Aivic - avatar
3 Réponses
+ 5
It's too simple! The value of i is equal to 9 then i increment by 1 and its equal to 10 Finally 3*10=30
14th Jul 2019, 9:18 PM
Sabiry 🇦🇫
Sabiry 🇦🇫 - avatar
+ 2
because the condition is <= 9, to break this condition i need to be more than 9. since the increment is 1 i is 10 in the last itteration then check the condition again. the condition is false, the loop ended, and i still 10. i i <=9 i++ 6 -> 6<=9 (YES) -> 6+1 7 -> 7<=9 (YES) -> 7+1 8 -> 8<=9 (YES) -> 8+1 9 -> 9<=9 (YES) -> 9+1 10 -> 10<=9 (NO) ---> Loop end
14th Jul 2019, 8:33 PM
Taste
Taste - avatar
+ 1
The third part of the for loop is executed before checking the condition. Let's jump to when i is 8, since until then everything should be clear. The contents of the loop get executed (nothing), then i is incremented (i = 9); i <= 9 is still true, so the loop continues, then it's incremented again (i = 10), but then the condition is false so the loop stops executing. 3 * 10 is 30, so the output is 30.
14th Jul 2019, 8:29 PM
Airree
Airree - avatar