What is the sequence of execution of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the sequence of execution of this code?

... namespace SoloLearn { class Program { static void Main(string[] args) { var num = 1; while(num++ < 5) { if(num % 2 == 0){ num += 2; } Console.WriteLine(num); } } } } I tried to figure out how many times the loop will run which I got right to be 2. but the value of 'num' on iteration was 4 and 5. that got me confused.

18th Jan 2017, 11:50 PM
Raheem Kareem
Raheem Kareem - avatar
1 Answer
+ 4
1 + 1(++num)= 2 (2%2=0) + 2 = 4 ( 4 < 5 ) + 1(++num) It should loop 2 times. Break on value being 5.
19th Jan 2017, 12:36 AM
Alex
Alex - avatar