Jan 20, 2018 at 8:45pm
What is the value of points before the loop? (Hint: watch out for undefined behavior)
Jan 20, 2018 at 8:51pm
Edit: There was, at one point, a post before this one saying something like:
I set points but it doesn't work.
1 2 3 4 5
|
int points = 50;
while(points < 0 || points > 100)
{
cin >> points;
}
|
|
It does work.
Line 1 : You create points, and you set its value to 50
Line 2 : if points is less than zero, OR if points is more than a hundred, do what's inside the braces.
Is points less than zero? No.
Is points more than one hundred? No.
So should we do what's inside the braces? Is points less than zero, OR more than a hundred? No it isn't.
Last edited on Jan 20, 2018 at 9:13pm
Jan 20, 2018 at 10:35pm
this is why do-whiles exist. They do the loop body once no matter what.
eg:
do
{
cin >> points;
}
while (points < 0)
Last edited on Jan 20, 2018 at 10:35pm