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