- 1

How many times will the following loop execute?

namespace SoloLearn { class Program { static void Main(string[] args) { int x = 1; while (x++ < 5) { if (x % 2 == 0) x += 2; } } } }

28th Jan 2018, 5:41 PM
Utsav Sikander
Utsav Sikander - avatar
6 Respostas
+ 13
after 1st loop ) x will be 4 after 2nd loop) x will be 5 6 as final value of x [5++ is not < 5 so loop stops & x become 6]
28th Jan 2018, 5:59 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
Twice x will be 2 and 5 at the if statement and 6 after exiting the loop.
28th Jan 2018, 5:55 PM
John Wells
John Wells - avatar
+ 1
Just add code to show you how many times it loops if you're having a hard time understanding. https://code.sololearn.com/c1yx4mJ7eQ47/?ref=app
28th Jan 2018, 5:57 PM
Jesse Bayliss
Jesse Bayliss - avatar
+ 1
loop will run twice
28th Jan 2018, 6:58 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 1
the loop will run twice #so answer will be 2
11th Jan 2021, 12:00 PM
Fredrick Brighton
Fredrick Brighton - avatar
0
The correct answer is 2
23rd Jun 2022, 3:36 PM
ilham niya
ilham niya - avatar