0

I have doubt in the below code, I have attached my questions in the code as comments.

https://code.sololearn.com/cSM29EMHDd5e/?ref=app

16th Apr 2022, 4:40 PM
King
King - avatar
4 Réponses
+ 1
for(initialization ; condition_check; update) { //loop body.. } first Initialization happens, then condition is checked, if true loop body executed otherwise don't, in next step it updates values, next it goes to check again condition.. This gets repeated until condition false. I=I*i = 3*3=> 9 Next step is upadate i before checking condition so i += 3 => 9+3=12 Hope it clears...
16th Apr 2022, 4:48 PM
Jayakrishna 🇮🇳
+ 1
It's a for loop, now 3 * 3 = 9 but in the for loop as specified it has to run 5 times so it's just constantly multiplying
17th Apr 2022, 3:04 AM
Ben Archard
0
Jayakrishna🇮🇳 then why doesn't it happen the same way in for(int i=0;i<5;i+=3)
18th Apr 2022, 5:27 AM
King
King - avatar
0
Do you mean 0 output? Because int i=0 ; creates a local variable in for loop block, it shadows outside global i. And local i variable does not exist outside loop. So out side, loop i unmodified by loop. So value 0 remains.. Outputs 0; Hope it clears..
18th Apr 2022, 9:09 AM
Jayakrishna 🇮🇳