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
There is no shortcut key of input in qbasic
Yes
END '...END of program/halt program code execution. *NOTE*: There should be only 'one' END statement written inside of a QBASIC program. I have seen example code where they use multiple END statements; this is wrong!
The different types of operators in QBASIC are:Arithmetic OperatorsRelational OperatorsLogical Operators
QBASIC CODE/EXAMPLE 1: WITHOUT 'RANDOMIZE' statement:- ==== RANDOMIZE TIMER CLS FOR eachDiceThrow% = 1 TO 10 diceNo% = INT(RND * 6) + 1 PRINT diceNo%; " "; NEXT END ===== ...output... 5 3 6 3 1 3 6 1 1 6 ...re-RUN/output... 6 2 6 5 1 4 1 5 6 5 As you can see from the above 2 code examples. When we don't use the RANDOMIZE statement; then, each time we run the program...the sequence of random numbers that gets produced...is always in the same 'fixed' order as went before. But, when we do use the RANDOMIZE command statement; then, each time the sequence of random numbers being produced is entirely 'different'. NOTE: The statement, RANDOMIZE TIMER, is used to 'seed' the QBASIC random number generator; by selecting a random number to start the sequence of numbers off with which is based on your current computer clock time.
A 'conditional' statement is a logical test while unconditional statement will cause the computer to branch.
Statement numbers were a feature of BASIC, and while QBASIC supports them, they are by no means necessary.
There is no shortcut key of input in qbasic
Yes
To stop a program from running in QBASIC, you can press "Ctrl" + "Break" on your keyboard. This interrupts the program execution and returns you to the QBASIC command prompt. Alternatively, you can close the QBASIC window to terminate the program. If you want to exit gracefully, you can also use the END statement in your code to stop execution at a specific point.
END '...END of program/halt program code execution. *NOTE*: There should be only 'one' END statement written inside of a QBASIC program. I have seen example code where they use multiple END statements; this is wrong!
H. Tornsdorf has written: 'Windows 3.1 - Trucos y Consejos' 'QBasic for beginners' -- subject(s): QBasic (Computer program language)
Most computer languages use the asterisk, "*", for multiplication.
In QBasic, the READ statement is used to read data from a data list that has been previously defined using the DATA statement. This allows programmers to input a series of values that can be retrieved later in the program. The READ statement retrieves values sequentially, so each call to READ fetches the next value in the data list. This is useful for initializing variables with predefined data without hardcoding them directly in the program.
the extensions of qbasic are that, there are only 80 pixels to write in the qbasic
(1) Immediate mode In this mode, QBASIC performs the command/instruction that we type in immediately after we press the enter key. (2) Program mode In this mode, the instructions/commands we type in are not performed as we type them in, but are first stored in the computer memory as a program. This program can be executed later, at a high speed.
qbasic is important because its technology