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;

22nd May 2023, 8:12 PM
Salam Tamam Kasm
Salam Tamam Kasm - avatar
5 odpowiedzi
+ 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; }
22nd May 2023, 9:35 PM
Lisa
Lisa - avatar
+ 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
22nd May 2023, 9:35 PM
Bob
+ 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
22nd May 2023, 11:53 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
+ 1
Thanks👏🏻👍🏻
23rd May 2023, 12:00 AM
Salam Tamam Kasm
Salam Tamam Kasm - avatar
+ 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
27th May 2023, 9:31 PM
Bob