+ 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
1 Réponse
+ 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.