0
I want explain for this code?
#include <iostream> using namespace std; int main() { for(int i=1;i<=3;++i) for(int j=1;j<=4;++j) cout<<i*j<<"\n"; return 0;
6 Answers
+ 5
// You can understand the code better, when you print out each step of the iteration, for instance like this:
#include <iostream>
using namespace std;
int main() {
for(int i=1;i<=3;++i) {
for(int j=1;j<=4;++j) {
cout << i << "*" << j << "=" << i*j << "\n";
}
}
return 0;
}
+ 3
Nested loops.
First iteration i=1 and j=1. After it prints i*j, j will be incremented by 1. This will continue until j=4. At this point i will increment by 1 and j will be set to 1 again. This will continue until i=3. At this point the loop will end and main will return 0
+ 1
1 / i=1 j=1 i*j=1 /
2 / i=1 j=2 i*j=2 /
3 / i=1 j=3 i*j=3 /
4 / i=1 j=4 i*j=4 /now j= 4 loop j<=4 so we add 1 to i
2 / i=2 j=1 i*j=2 /
4 / i=2 j=2 i*j=4 /
6 / i=2 j=3 i*j=6 /
8 / i=2. j=4 i*j=8 /. j= 4 we add 1 to i
3 / i=3 j=1 i*j=3 /
6 / i=3 j=2 i*j=6 /
9 / i=3 j=3 i*j=9 /
12 / i=3 j=4 i*j=12 / j=4 but the loop for i is i<=3 so the execute will end
+ 1
Thanksđđ»đđ»
+ 1
Salam Tamam Kasm everythinge is perfect you only forget curly bracket ({}) in the for loop:-
Eg. for(int i=1;i<=3;++i){ â//this thinge({})
for(int j=1;<=4;++j){ ââ
cout<<i*j<<"\n";
}â
}â
return 0;
}
+ 1
Alhaaj Raza they aren't strictly necessary if the for statement is a single statement, though it is good style. This will work as written