how do you add a simple IF/THEN command?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Guest

how do you add a simple IF/THEN command?

Post 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! :?:
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post 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!
Image
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post 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
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
User avatar
vohaul
Coder
Posts: 15
Joined: Thu Mar 17, 2005 10:38 am
Location: Netherlands
Contact:

Post by vohaul »

I'd just be lame and call a dedicated sub ;)

But then again, I haven't touched Basic in years :P
I am what you'd call a nice bastard.
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post 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.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
User avatar
vohaul
Coder
Posts: 15
Joined: Thu Mar 17, 2005 10:38 am
Location: Netherlands
Contact:

Post 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 :)
I am what you'd call a nice bastard.
User avatar
lurah-
Veteran
Posts: 206
Joined: Mon Nov 01, 2004 10:47 am
Location: Finland
Contact:

Post 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...
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post 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.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
User avatar
vohaul
Coder
Posts: 15
Joined: Thu Mar 17, 2005 10:38 am
Location: Netherlands
Contact:

Post 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.
I am what you'd call a nice bastard.
User avatar
lurah-
Veteran
Posts: 206
Joined: Mon Nov 01, 2004 10:47 am
Location: Finland
Contact:

Post 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 8)
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post 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.
:D
"But...It was so beutifully done"
User avatar
lurah-
Veteran
Posts: 206
Joined: Mon Nov 01, 2004 10:47 am
Location: Finland
Contact:

Post by lurah- »

Code: Select all

End if 
LOOP Until KeyWord$ = "Q" or KeyWord$ = "q"
Ok, i messed up with lines :oops:
**it happens :lol:
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post 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.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
User avatar
vohaul
Coder
Posts: 15
Joined: Thu Mar 17, 2005 10:38 am
Location: Netherlands
Contact:

Post by vohaul »

A loop, subs and case. I can dig it :)
I am what you'd call a nice bastard.
Guest

Post 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" 
?
User avatar
vohaul
Coder
Posts: 15
Joined: Thu Mar 17, 2005 10:38 am
Location: Netherlands
Contact:

Post by vohaul »

That struck me as odd too, as lcase was used in the other checks :P
I am what you'd call a nice bastard.
User avatar
lurah-
Veteran
Posts: 206
Joined: Mon Nov 01, 2004 10:47 am
Location: Finland
Contact:

Post 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 :wink:

Someones could now ask, is it ok to use "q" instead of CHR$(something) and next would ask something...
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

Post 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.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
User avatar
Mitth'raw'nuruodo
Veteran
Posts: 839
Joined: Sat Jan 22, 2005 11:04 am
Location: Eastern Coast of US
Contact:

Post 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 :wink:

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 :lol:

Ya, Levi, fine I prefer NOT to use GOTOs, (college professers, and Programming co-workers too :lol: )
"But...It was so beutifully done"
User avatar
lurah-
Veteran
Posts: 206
Joined: Mon Nov 01, 2004 10:47 am
Location: Finland
Contact:

Post by lurah- »

Levi wrote:So, this is done.
I gues so too :lol: 8) :lol:

Alltough it could be done like...
Post Reply