C
C#7mo ago
Aubrey

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
No description
15 Replies
SinFluxx
SinFluxx7mo ago
You could check that Add is greater than 0 and show a messagebox if not
Aubrey
Aubrey7mo ago
but i want it to be able to go into negatives aswell
SinFluxx
SinFluxx7mo ago
But then you'll hit an infinite loop again?
Aubrey
Aubrey7mo ago
well not if the adding and stop number are both in the negatives
SinFluxx
SinFluxx7mo ago
Oh you mean your Stop number can be <0 too?
Aubrey
Aubrey7mo ago
yes i want it to show an error if the adding and stop numbers arent the same well yknow what i mean i hope
SinFluxx
SinFluxx7mo ago
So you'd probably need to check that either: 1) Start < Stop and Add > 0 or 2) Start > Stop and Add < 0
Aubrey
Aubrey7mo ago
do i just do that with if? or with something else?
SinFluxx
SinFluxx7mo ago
Yeah you can
Aubrey
Aubrey7mo ago
yeah i just decided to not allow negatives..
No description
Aubrey
Aubrey7mo ago
although, is there a shorter way to make this? not using 2 ifs
SinFluxx
SinFluxx7mo ago
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 loop
Aubrey
Aubrey7mo ago
ohh thanks for the tip
SinFluxx
SinFluxx7mo ago
So if you did want to allow negatives still you could do: if ((Start < Stop && Add <= 0) || (Start > Stop && Add >= 0))
Dharmang
Dharmang7mo ago
why handle negative? mod everything and if the stop is a negative inverse the output.