0
A task from a challenge
Hi everyone. Can anybody explain me how does the following code work, because i don't understand the result of it. #include <iostream> using namespace std; int main(){ for(int i=0;i<3;i++) for(int j=0;j<=i;j++) cout<<i<<endl; return 0; } // Output is 011222, shouldn`t it be 012, because we ouput only i and how it becomes 011222?
1 Answer
+ 10
i = 0, j <= 0 means 0 will be printed once (for j = 0).
i = 1, j <= 1 means 1 will be printed twice (for j = 0, 1).
i = 2, j <= 2 means 2 will be printed thrice (for j = 0, 1, 2).