Here is a simple example of a program using the IF...THEN...ELSE... statement in QBASIC:
1 CLS
2 INPUT "What is your name?" NAME$
3 IF NAME$ = "Fred" THEN GOTO 4 ELSE GOTO 6
4 PRINT "Fred is a cool name!"
5 END
6 PRINT "Your name isn't Fred, you're Lame!"
7 END
NB: My name isn't Fred!!!
How the statement works is simple...
IF [VARIABLE NAME] [EQUALS/DOES NOT EQUAL/GREATER THAN/LESS THAN] [PERAMETER] THEN [ACTION] [PERAMETER] ELSE [ACTION] [PERAMETER]
A similar function exists in most programming languages, and uses can range from a simple calculation to passWord control. Also, you can use other commands within this (eg: OR) or use several commands together to give more options:
1 CLS
2 INPUT "What is your name?" NAME$
3 IF NAME$ = "Fred" THEN GOTO 4 ELSE GOTO 6
4 PRINT "Fred is a cool name!"
5 END
6 IF NAME$ = "Bob" THEN GOTO 7 ELSE GOTO 9
7 PRINT "Fred is a cool name, but Bob is OK too!"
8 END
9 PRINT "Fred is OK, Bob is Acceptable but "; NAME$; " is just lame!"
10 END
NB: Bob is not my name either, guess I'm lame!
Hope this answers your query!
Dan
PS: Other statements used in examples:
CLS = Clear Last Screen
INPUT = Allows you to enter data after text is displayed, and attach to the variable
PRINT = Print the text.
END = Finish the program run.
It may also be useful to note, if using QBASIC on Windows Vista the LPRINT function does not work correctly. If you need to output to a printer, it is better to save as a text file and then print within the Windows Vista environment. Alternatively, run an older version of Windows just for programming in. I tend to use Windows 2000 professional - good flexability and pretty stable!
=====
No disrespect meant to the above programmer; who, overall, I think, has done a really fine job of explaining things. ;-)
Nevertheless, for me, the above code contains certain problems which still need to be addressed and fixed...
A) Line number 2...
2 INPUT "What is your name?" NAME$
...is incorrect syntax...instead, it should have said...
2 INPUT "What is your name?";NAME$
...including a semi-colon symbol(;) which is to be placed in between both quoted text("")/and, variable name(NAME$).
B) The first example uses the END statement, twice; and, the second example uses the END statement three times, no less...; but, strictly speaking, there should only be no more than just 'one' single END statement, alone, in any program.
C) The use of LINE NUMBERS together with GOTO statements is completely outdated...and, quite often, can lead to difficult to read, error prone programs; these are ancient relics belonging to the old time 'original' BASIC programming language.
When using the modern day QBASIC programming language we don't need to rely on using such old fashioned statements anymore...much preferring instead to use carefully structured block statements...which makes our program logic read a lot more clear.
I would re-write program 1, by using an IF-THEN-ELSE-END IF statement block, as...
CLS
INPUT "What is your name?";name$
IF name$ = "Fred" THEN
PRINT "Fred is a cool name!"
ELSE
PRINT "Your name isn't Fred, you're Lame!"
END IF
END
I would re-write program 2, by using an IF-THEN-ELSEIF-ELSE-END IF statement block, as...
CLS
INPUT "What is your name?"; name$
IF name$ = "Fred" THEN
PRINT "Fred is a cool name!"
ELSEIF name$ = "Bob" THEN
PRINT "Fred is a cool name, but Bob is OK too!"
ELSE
PRINT "Fred is OK, Bob is Acceptable but "; name$; " is just lame!"
END IF
END
NOTE: In the above amended code; there are no line numbers/and, no goto's.
NOTE: Each of the above programs has 'one'...and, only 'one' single END statement.
NOTE: My own variable names begin by using all lower case letters; and, I will capitalise each 1st letter of any successive Word(s) appearing in the same variable name...nameOne$/nameTwo$/nameThirtyThree$...as it makes the code read a lot more clear...when it comes to very quickly and easily distinguishing between what is a QBASIC language keyWord such as PRINT/and, what is a variable name which was invented by the actual program writer themselves.
Everybody tends to write code differently; so, if 100 different programmers were asked to write a program which displays exactly the same 'surface output'; then, one would most likely find that the underlying 'source code' beneath has been written entirely differently by every single programmer!
Programming 'style'...is really all down to a matter of personal taste/or, choice. You will learn this as you come across reading more and more programs. For example, going back to the question of what type of variable name to use? Some people like using the minimal, n$/others use all capitals NAME$/and, yet, another writes it as being all lower case, name$/whereas, a next programmer might write it using long form, 3 letter prefix, strName$/-etc. Furthermore, some will add code comments/and, other's don't! Over time, and, with experience we all tend to develop our own much preferred style of writing programs; going according to whichever method we find works best, and, does most tend to suit our own personal needs.
NOTE: The IF statement comes in quite a few different forms...
(single line)
A) IF/THEN
B) IF/THEN/ELSE
(multiple lines; also, called a statement block)
C) IF/THEN/ELSE/END IF
D) IF/THEN/ELSEIF/ELSE/END IF
...which I will quickly illustrate as...
A)
x=1
IF x=1 THEN PRINT "one"
B)
x=3
IF x=1 THEN PRINT "one" ELSE PRINT "NOT one"
C)
x=2
IF x=1 THEN
PRINT "one"
ELSE
PRINT "NOT one"
END IF
D)
x=1 : y=2
IF x=1 THEN
PRINT "one"
ELSEIF x=2 THEN
PRINT "two"
ELSE
PRINT "NOT one/NOT two"
END IF
Copyright © 2026 eLLeNow.com All Rights Reserved.