+ 2

Why does the code below exceeds the loop's condition?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { int num = 1; while(num < 6) { num+=2; Console.Write(num); } } } } output: 3 5 7

25th Jul 2020, 1:01 PM
Divine Darkey
Divine Darkey - avatar
1 Odpowiedź
+ 5
Because you are writing after increment inside the loop body. So when num = 5 while num < 6 true num += 2 now num becomes 7 Console.Write(num); // prints 7 while num < 6 false, loop ends.
25th Jul 2020, 1:08 PM
Gordon
Gordon - avatar