Explain how the for loop condition work for this code!? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Explain how the for loop condition work for this code!?

https://code.sololearn.com/c9XJ968KwUUU/?ref=app For loop

14th Dec 2019, 8:10 AM
Deena Dhayalan.S
Deena Dhayalan.S - avatar
4 Answers
+ 2
Your x starts with the value 0. 1st case: 0 <= 10 is true so 0. Then x is incremented by 3. 2nd case: 3 <= 10 is true so 3. Then x is incremented by 3. 3rd case: 6 <= 10 is true so 6. Then x is incremented by 3. 4th case: 9 <= 10 is true so 9. Then x is incremented by 3. 5th case: 12 <= 10 is false so the loop breaks and the control is transferred to the end. So output is 0 3 6 9.
14th Dec 2019, 8:16 AM
Avinesh
Avinesh - avatar
+ 2
Flow of for loop - 1) Initialization 2) Condition checking 3) Code inside for is executed 4) Increment/ Decrement
14th Dec 2019, 8:21 AM
Avinesh
Avinesh - avatar
+ 2
Yes, initialization is just a one time thing and the value is just updated after each iteration.
14th Dec 2019, 8:23 AM
Avinesh
Avinesh - avatar
0
So we should take only the output values for the first starting value and then we should use in that condition which is given in the loop!?
14th Dec 2019, 8:22 AM
Deena Dhayalan.S
Deena Dhayalan.S - avatar