Am I right? for (i=5; i>=1; i--) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Am I right? for (i=5; i>=1; i--)

#include <iostream> using namespace std; int main(int argc, char *argv[]) { int i, j=1; for (i=5; i>=1; i--) j *= i; cout << j; return 0; } 1st interaction j*=i == j= j*5 so j=5 2nd j = 5*4 so j=20 3rd j = 20*3 so j=60 4th j = 60*2 so j=120 5th j = 120*1 so j=120 Output is 120

5th Mar 2020, 1:52 AM
Sh.A.kH.
Sh.A.kH. - avatar
5 Answers
+ 1
Yes, you're getting it right Sh.A.kH. 👍 If you want to see what happens in each iteration then you can add `cout << j << endl;` inside the loop body. That way each change to <j> will be printed. Don't forget to wrap the loop body block with curly brackets {...}
5th Mar 2020, 5:00 AM
Ipang
+ 2
//There is editor in codes Tab where you can run and check.
5th Mar 2020, 1:53 AM
Sudarshan Rai
Sudarshan Rai - avatar
+ 1
I know about the editor, but do I understand interactions right?
5th Mar 2020, 1:56 AM
Sh.A.kH.
Sh.A.kH. - avatar
+ 1
Ipang Wow, thank you! This is the best answer cause sometimes I don't understand outputs but your way can really help me. Thanks !!!!
5th Mar 2020, 6:01 AM
Sh.A.kH.
Sh.A.kH. - avatar
0
You are welcome Sh.A.kH. 👌
5th Mar 2020, 6:28 AM
Ipang