The QBASIC section has been archived, and is no longer updated.

Block If

Control multiple statements with one IF statement

A common example of this is the password-protection program. You probably have something like this:

 INPUT "Enter Password: " , PASSWD$
 IF PASSWD$ = "Secret" THEN PRINT "Access Granted"
 IF PASSWD$ <> "Secret" THEN PRINT "Login failed"

Instead of multiple IF statements, you can write it like this:

 INPUT "Enter Password: " , PASSWD$
 IF PASSWD$ = "Secret" THEN
   PRINT "Access Granted"
 ELSE
   PRINT "Login failed"
 END IF

(Download)

If PASSWD$ is "Secret", then everything between the IF and the ELSE is run, and if it does not, then everything between the ELSE and the END IF is run. This means that you can have a program that looks like this:

 INPUT "Enter Password: " , PASSWD$
 IF PASSWD$ = "Secret" THEN
   PRINT "Access Granted"
   PRINT "++++ WELCOME TO THE PAL COMPUTER ++++"
   INPUT "How many things should I do? " , THINGS
   FOR I = 1 TO THINGS
     INPUT "Do what? ", DO$
     PRINT "I'm afraid I can't do that, Dave."
   NEXT I
 ELSE
   PRINT "Login failed"
   BEEP
 END IF

(Download)

Here is a sample run: (User input is underlined)

 Enter Password: Secret
 Access Granted
 ++++ WELCOME TO THE PAL COMPUTER ++++
 How many things should I do? 2
 Do what? Play chess
 I'm afraid I can't do that, Dave.
 Do what? Don't play chess
 I'm afraid I can't do that, Dave.

And another:

 Enter Password: Password
 Login failed