Qbasicnews.com

Full Version: help with ELSE
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
I am having trouble using else in my program.
How can i use else in my program to stop invalid entries.
(RTFM stuff, blah blah, and such)

IF/THEN/ELSE/ENDIF is used this way:

Code:
IF <condition> THEN
   <instructions if YES>
ELSE
   <instructions if NO>
END IF
my program continues to tell me "Else without if". here's the program maybe you can fix it better than i would understand.[/code]
input "Do you want to play a math game"; reply$
if reply$ = yes then goto blahblahblah
if reply$ = no then print "O.K.": end
else end
Quote:my program continues to tell me "Else without if". here's the program maybe you can fix it better than i would understand.[/code]
input "Do you want to play a math game"; reply$
if reply$ = yes then goto blahblahblah
if reply$ = no then print "O.K.": end
else end

Code:
input "Do you want to play a math game"; reply$

if reply$ = yes then
  goto blahblahblah:
end if

if reply$ = no then
  print "O.K."
  end
else
  end
end if
Better yet,

Code:
input "Do you want to play a math game"; reply$

if reply$ = "yes" then
  goto blahblahblah:
elseif reply$ = "no" then
  print "O.K."
  end
else
  end
end if
Better still:

Code:
input "Do you want to play a math game"; reply$

select case ucase$(reply$)
  case "YES"
    goto blahblahblah:
  case "NO"
    print "O.K."
  case else
    end
end select

Try to avoid the evil Elseif. My first solution was showing it with a block-if, which was in answer to your question. This is the superior solution.
and just why exactly is "elseif" evil?
i'm not really sure. i use if. select -> end select is certainly a faster way, but it's so insignificant it would matter unless you were pulling this off thousand of times a second.
ELSEIF is just this:

Code:
IF a% = 3 THEN
   PRINT "Three"
ELSE
   IF a%=5 THEN
      PRINT "Five"
   ELSE
      PRINT "Not five nor three"
   END IF
END IF

translates to... (faster to write)

Code:
IF a%=3 THEN
   PRINT "Three"
ELSEIF a%=5 THEN
   PRINT "Five"
ELSE
   PRINT "Not five nor three"
END IF

I don't see why it is evil, but there is a better form, just like toonski said:

Code:
SELECT CASE a%
   CASE 3:
      PRINT "Three"
   CASE 5:
      PRINT "Five"
   CASE ELSE:
      PRINT "Not five nor three"
END SELECT

You'll only notice the speed increase if, as toonki said, you need to evaluate things in a long loop (for example, in a game loop).
Pages: 1 2 3