+ 4

break and continue

Why does this code not show "5" at the end? The "if (num == 5)" statement should make it loop till it's even with 5. I'm not getting this. Can someone assist w/this example please? Thanks. int num = 0; while (num < 20) { if (num == 5) break; Console.WriteLine(num); num++; } /* Outputs: 0 1 2 3 4 */

2nd Feb 2018, 7:16 PM
Damon
4 Answers
+ 6
If you put WriteLine before the if clause, the number 5 will be shown. ;)
2nd Feb 2018, 8:23 PM
Pedro Maimere
Pedro Maimere - avatar
+ 3
when it found the num ==5 then it find break and it will stop good luck
2nd Feb 2018, 7:18 PM
Abdurrahman Abulgasim
Abdurrahman Abulgasim - avatar
+ 2
Thank you Jamie and Abdurrahman. I believe I have it now.I was in the mindset of "if (num > 5)" then stop (break).
2nd Feb 2018, 8:23 PM
Damon
0
you break the statement num before output and it should be shows below then 20
3rd Feb 2018, 6:55 PM
Manju G
Manju G - avatar