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; }
4 ответов
+ 2
Last iteration, i = 9
i<=9 == true so i receives an increment and is equal to 10
3 * 10 = 30
+ 1
Ok, thank you very much
+ 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.
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.