0

Can some explain this while loop to me? It's not exicuting.

int num = Convert.ToInt32(Console.ReadLine()); int res = 1; //your code goes here while(num < 9) { Console.WriteLine(num);num+=2

3rd Jan 2021, 4:35 PM
Swiff _Vastolorde
Swiff _Vastolorde - avatar
3 Respostas
0
Youre are missing a semicolon and a curly brace at the end: while(num < 9) { Console.WriteLine(num);num+=2; } then, with every loop iteration the value of num is increased by 2 num+=2; is equal to num = num+2; when the value of num gets bigger than 8, the loop is left. So basically, if the user enteres a value of 8 or bigger - the loop will never be entered
3rd Jan 2021, 4:53 PM
Martin Ed
Martin Ed - avatar
+ 1
Martin Ed it's still not executing. Look below. int num = Convert.ToInt32(Console.ReadLine()); int res = 1; //your code goes here while(num < 9) { Console.WriteLine(num);num+=2; }
3rd Jan 2021, 7:19 PM
Swiff _Vastolorde
Swiff _Vastolorde - avatar
0
Hi, the above code didn't work for me. This was my solution: int num = Convert.ToInt32(Console.ReadLine()); int res = 1; res++; while(res <= num) { Console.WriteLine(res); res+=2; } It seems to be that you can't increment N because N (num) is the number that res (what I took to be the range) needs to be smaller or equal to, in order for the sequence up to N to be shown Not sure why ++res was needed but that's the only way I could get res to = 2 Could have missed something though!
16th Jan 2022, 6:04 PM
Ania