why does result is 5? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
8th Jul 2022, 2:04 PM
🕸carpe diem🕸
🕸carpe diem🕸 - avatar
4 Answers
+ 2
First loop makes i=4 I=I+1=>i=1, and next i++; so I=2 I=I+1=>i=3, and next i++; so I=4 4<3 false, loop stops.. if block i%2==0 cause i++; so i=5 now If provide your understandings then it helps you correct your answer, if any wrong.. Hope it helps..
8th Jul 2022, 2:09 PM
Jayakrishna 🇮🇳
+ 1
In the last iteration i= 3 , then i++ = 4 according to (i =0, i<3, i++) Then it moves to the conditions if i%2 ==0 , which is true then it increment by 1 Making it 5 I hope this helps
8th Jul 2022, 2:36 PM
Muhammad Awwal Yusuf
Muhammad Awwal Yusuf - avatar
+ 1
This is the order of execution : i=0; i<3 i=i+1 i++; i < 3 i=i+1 i++; i<3 .. .. there checks are 0<3 true initial 2<3 true, after 1st iteration 4<3 false, after 2nd iteration
8th Jul 2022, 3:39 PM
Jayakrishna 🇮🇳
- 1
But when i=3, this loop will stops. I = 3, it's <3, so i =2
8th Jul 2022, 2:15 PM
🕸carpe diem🕸
🕸carpe diem🕸 - avatar