Posted by Goku_75 on 22:05:00 10-17-2003
Help me with this code:
CLS
INPUT "What's your name" , name$
If name$ = "George" Then
PRINT "Hi George"
END IF
IF name$ = "Fred" THEN
PRINT "Yo Fred"
END IF
IF name$ = "Earl" THEN
PRINT "Hey Earl
ELSE
PRINT "I don't know you"
END IF

Do you get what i want to do with the else.
When i put in Fred or George then it says I don't know you

I want it to say that when i dont put in George or Fred Or Earl
It's a little confusing aint it
[addsig]
Posted by Neu[Mann] on 22:46:00 10-17-2003
Okay! Imagine the flow of your program is like this one (I don't want to give you the answer straight away)

Input x
If x = a Then DoThingA()
If x = b Then DoThingB()
If x = c Then DoThingC() Else DoThingElse()

Now, if you input b, let's see what happen:

The first if, If X = a Then DoThingA(), misses since X = b.
The second if, If X = b Then DoThingB(), suceeds and that's what we want it to do.
But, the program doesn't stop there. The next instruction is the 3rd if, If x = c Then DoThingC(), which fails, but you have an Else close. So, in the end, your program will DoThingB() and DoThingElse().

This is your error. Now there are many ways to fix it. The "Select Case" instruction in Basic is useful for that. You could also use a combination of If and Goto, but the first solution is prefered.

If you don't understand something, please write back.

[ This Message was edited by: Neu[Mann] on 2003-10-17 23:52 ]
Posted by dxprog on 00:29:00 10-18-2003
You could also use ElseIf. Example:

If sThing = "a" Then
  DoThingA
ElseIf sThing = "b" Then
  DoThinB
ElseIf sThing = "c" Then
  DoThingC
Else
  DoThingElse
End If

Say sThing = "b". Once the ElseIf matches it up it would skip over all the other if statements in that block. [addsig]