endless loop
Im having a problem of an endless loop happening if a number goes into the opposite direction of the stop number
how do i make it show an error instead of crashing in cases like this? im learning for an upcoming exam i have atm
15 Replies
You could check that Add is greater than 0 and show a messagebox if not
but i want it to be able to go into negatives aswell
But then you'll hit an infinite loop again?
well not if the adding and stop number are both in the negatives
Oh you mean your Stop number can be <0 too?
yes
i want it to show an error if the adding and stop numbers arent the same
well yknow
what i mean
i hope
So you'd probably need to check that either:
1) Start < Stop and Add > 0
or
2) Start > Stop and Add < 0
do i just do that with if? or with something else?
Yeah you can
yeah i just decided to not allow negatives..
although, is there a shorter way to make this? not using 2 ifs
You can check multiple conditions in one if statement:
if (condition1 || condition2)
if (condition1 && condition2)
Where || is OR, && is AND
Rather than having your while loop inside the else you can also just have a return inside the if that's checking for negatives so that it never hits the while loopohh thanks for the tip
So if you did want to allow negatives still you could do:
if ((Start < Stop && Add <= 0) || (Start > Stop && Add >= 0))
why handle negative? mod everything and if the stop is a negative inverse the output.