Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / If/then statement with multiple values

Author
Message
BeastMaster
10
Years of Service
User Offline
Joined: 10th Apr 2015
Location:
Posted: 10th Apr 2015 23:07

That code does not work and I just want to know how i can make it work . Pretty much my question is how can I code something like
if x = 1 or 2 then print "hello"
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 10th Apr 2015 23:19 Edited at: 10th Apr 2015 23:20


You have to write the comparison in full each time.

Also, you need AND, not OR. It will always be not 1 or 2 or 3 (because it can only ever be one of them, it can't be all of them). This is a common error, even for old professionals!
The rule is that a NOT ( <> ) switches the ORs to ANDs.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
BeastMaster
10
Years of Service
User Offline
Joined: 10th Apr 2015
Location:
Posted: 11th Apr 2015 04:22
Thanks man, helped a lot
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 11th Apr 2015 08:42
In Addition: When dealing with user input, if the user enters some text like a name, then it's generally best that our programs force the case of the input to either lower or upper before making the required comparisons.


ie.
if InputName$="Bill"
print "Hello Bill"
endif

This code would only function as we expect, when the user enters Bill with a capital B, it'd fail in every other combination.

where as forcing it to lower and comparing the text with the lower case literal of "bill" this time, would catch all the combinations of BILL that are possible.

if lower$(InputName$)="bill"
print "Hello Bill"
endif

if you need to do a bunch of string compares then it's generally worth create a temp string and comparing everything to that.

LowerCase_InputName$ =lower$(InputName$)

if LowerCase_InputName$="bill"
print "Hello Bill"
endif

if LowerCase_InputName$="dude"
print "Hello Dude"
endif


etc etc

Login to post a reply

Server time is: 2025-05-11 10:55:28
Your offset time is: 2025-05-11 10:55:28