Please why is the output 30. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please why is the output 30.

# <iostream> using namespace std; int main() { int i=6; for (i;i<=9;++i) {} cout << 3*i << endl; return 0; }

30th Mar 2019, 7:25 PM
Prince Salasi Agblevor
Prince Salasi Agblevor - avatar
4 Answers
+ 2
Last iteration, i = 9 i<=9 == true so i receives an increment and is equal to 10 3 * 10 = 30
30th Mar 2019, 7:33 PM
Bruno Costa
Bruno Costa - avatar
+ 1
Ok, thank you very much
30th Mar 2019, 7:34 PM
Prince Salasi Agblevor
Prince Salasi Agblevor - avatar
+ 1
Comparing the examples for (i;i<=9;++i) {} <--- You see the empty brackets? cout << 3*i << endl; Thats because it's checking the conditional and incrementing the iterator without executing additional code each iteration. After it finishes looping it only prints i value once. for (i;i<=9;++i) { cout << 3*i << endl; } This one it prints 3 * i every iteration.
30th Mar 2019, 7:44 PM
Bruno Costa
Bruno Costa - avatar
0
# <iostream> using namespace std; int main() { int i=6; for (i;i<=9;++i) { cout << 3*i << endl; } return 0; } What does this code give me an answer of 18,21,24,27, what is the main difference between these two codes.
30th Mar 2019, 7:36 PM
Prince Salasi Agblevor
Prince Salasi Agblevor - avatar