+ 1
Output inside outer loop , innerloop and in main I need a good explanation of the outputs and why??
#include <iostream> using namespace std; int main() { int i; for(i = 0; i < 4; ++i){ for(int j = 0; j < 4; ++j){ i = i + j; cout<<i; //0136 } //cout<<i; //6 } //cout<<i; //7 return 0; }
1 Réponse
0
Inside for (int j = 0; j < 4; j++)
First iteration:
i = 0 + 0 -> output 0
Second
i = 0 + 1 -> output 1
Third
i = 1 + 2 -> output 3
Fourth
i = 3 + 3 -> output 6
The loop exit and then execute cout << i inside the other loop, which will print the same value of i, 6. After the execution, i is incramented and then the loop exit because the value of i is now 7, 7 < 4 (false). After that, cout << i in the main scope is executed