Page 1 of 1
how do you add a simple IF/THEN command?
Posted: Thu Mar 17, 2005 5:15 pm
by Guest
I want to know how you add a if/then command. I want it so if you type in 'read me' on the input it would jump somewhere (like a GOTO), and if you type 'play' it would jump somewhere else, and if you type 'about' it will jump another place, and so on. I can't quite find out how to do this.
I am using a program called Just BASIC if that helps.
THANK YOU SO MUCH!

Posted: Thu Mar 17, 2005 6:18 pm
by {Nathan}
Just BASIC? Sorry...
Welcome to the forums! Register anad hang around.
Ugh... try
www.freebasic.net
Its free, fast, open-source, and ever-thriving!
Posted: Thu Mar 17, 2005 7:18 pm
by Levi
Code: Select all
10
Input "Enter a key-word: ", KeyWord$
If lcase$(KeyWord$) = "read me" then
Goto ReadMe:
Elseif lcase$(KeyWord$) = "play" then
Goto PlayMe:
Elseif lcase$(KeyWord$) = "about" then
Goto About:
Else
Goto 10
End if
But For something like this I suggest using a Case Select statement
Code: Select all
10
Input "Enter a key-word: ", KeyWord$
Select Case lcase$(KeyWord$)
Case "read me"
Goto ReadMe:
Case "play"
Goto PlayMe:
Case "about"
Goto About:
Case Else
Goto 10
End Select
Posted: Fri Mar 18, 2005 1:23 pm
by vohaul
I'd just be lame and call a dedicated sub
But then again, I haven't touched Basic in years

Posted: Fri Mar 18, 2005 2:59 pm
by Levi
True most prefer and suggest the use of subs. I just use Goto in examples. I don't know why. That and it was asked for. Oh well.
Posted: Fri Mar 18, 2005 4:29 pm
by vohaul
It seemed to me that the GOTO was offered more by example, as in "sort of like what GOTO does".
I think that in this situation, and if it won't cause a spaghetti situation, it'll be easiest, cleanest and most useful to simply use subs

Posted: Fri Mar 18, 2005 5:47 pm
by lurah-
Somehow funny. Others thinks that GOTO is for bad...me, i dont think that way. I use gGOTO if it?s usable...dont wana waste my time for thinking other ways...
Posted: Fri Mar 18, 2005 6:23 pm
by Levi
Dont' get me wrong, Goto is a great thing. But not all the time, subs are great, but not all the time.
In the case of if they enter something other than one of the main options it should GOTO the input question again. However the selected option should have it's own space.
I suggest using both not one or the other.
Posted: Fri Mar 18, 2005 7:49 pm
by vohaul
Even in that case I wouldn't use GOTO. Any time I've used an input expecting certain options, I use a loop, where the options typed that are expected open a sub, returning control to the loop when the subs end.
GOTO isn't necessarily a bad thing, I agree, and can be very useful in many situations, but I just kind of shied away from it after awhile.
Posted: Sat Mar 19, 2005 4:01 am
by lurah-
vohaul wrote:Even in that case I wouldn't use GOTO. Any time I've used an input expecting certain options, I use a loop, where the options typed that are expected open a sub, returning control to the loop when the subs end.
GOTO isn't necessarily a bad thing, I agree, and can be very useful in many situations, but I just kind of shied away from it after awhile.
Code: Select all
DO
Input "Enter a key-word: ", KeyWord$
If lcase$(KeyWord$) = "read me" then
Goto ReadMe:
Elseif lcase$(KeyWord$) = "play" then
Goto PlayMe:
Elseif lcase$(KeyWord$) = "about" then
Goto About:
Else
LOOP Until KeyWord$ = "Q" or KeyWord$ = "q"
End if
Here i would use DO...LOOP instead of GOTO.
Anyway, it?s a matter of opinion

Posted: Sun Mar 20, 2005 9:39 am
by Mitth'raw'nuruodo
You can't have a LOOP inside a If block inside that same loop:
Not valid:
Do
If
Loop
End IF
I agree 100% with vohaul, GOTOs should be avoided as much as possible because it does make the "spagetti" effect.
Me? I NEVER use them. I always use loops and SUBs. Once you set out avoiding GOTOs in your brain you automatically think in terms of not jumping around in a structure. I don't even consider GOTOs in a program anymore.
Its a good prorgamming habit.

Posted: Sun Mar 20, 2005 1:01 pm
by lurah-
Code: Select all
End if
LOOP Until KeyWord$ = "Q" or KeyWord$ = "q"
Ok, i messed up with lines
**it happens

Posted: Sun Mar 20, 2005 9:21 pm
by Levi
No point arguing if GOTO is good or not. It is all opinion. Professionals use a form of Branching or GOTO statements as well, so it's not exactly like there's a "Good" way of doing things when it comes to this. Just a preferred way.
Besides, in this case If really shouldn't be considered. Case is far more efficient when it comes to menu or command options.
Posted: Sun Mar 20, 2005 9:43 pm
by vohaul
A loop, subs and case. I can dig it

Posted: Mon Mar 21, 2005 9:02 am
by Guest
Code: Select all
End if
LOOP Until KeyWord$ = "Q" or KeyWord$ = "q"
Sorry don't mean to be a nit-pick but wouldn't it be easier to simply go.
Code: Select all
End if
LOOP Until lcase$(KeyWord$) = "q"
?
Posted: Mon Mar 21, 2005 11:23 am
by vohaul
That struck me as odd too, as lcase was used in the other checks

Posted: Mon Mar 21, 2005 11:29 am
by lurah-
Anonymous wrote:Code: Select all
End if
LOOP Until KeyWord$ = "Q" or KeyWord$ = "q"
Sorry don't mean to be a nit-pick but wouldn't it be easier to simply go.
Code: Select all
End if
LOOP Until lcase$(KeyWord$) = "q"
?
Yes it would. But question was about using GOTO, not what is shortest way to do that exactly code
Someones could now ask, is it ok to use "q" instead of CHR$(something) and next would ask something...
Posted: Mon Mar 21, 2005 12:16 pm
by Levi
the original question was how to add an If statement to the program. We all sort of got off track after what, the third or fourth post I think. When subs instead of GOTO came into what was posted. And so we're here. But the code has been offered, they can take which ever they want. They all work.
So, this is done.
Posted: Mon Mar 21, 2005 3:45 pm
by Mitth'raw'nuruodo
lurah wrote:Anonymous wrote:Code: Select all
End if
LOOP Until KeyWord$ = "Q" or KeyWord$ = "q"
Sorry don't mean to be a nit-pick but wouldn't it be easier to simply go.
Code: Select all
End if
LOOP Until lcase$(KeyWord$) = "q"
?
Yes it would. But question was about using GOTO, not what is shortest way to do that exactly code
Someones could now ask, is it ok to use "q" instead of CHR$(something) and next would ask something...
What about UCASE$(KeyWord$) = "Q" ? lol
Ya, Levi, fine I prefer NOT to use GOTOs, (college professers, and Programming co-workers too

)
Posted: Wed Mar 23, 2005 4:04 pm
by lurah-
Levi wrote:So, this is done.
I gues so too
Alltough it could be done like...