QBasic/Flow Control
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Flow control
Flow control is the ability to redirect the execution point of a program based on conditions. In most cases, control relies on a specific condition being either true or false, but can also operate based on a loop counter or through some other means.
In QBasic, flow control may be written in one or two ways:
- The flow control is contained within one line - usually the case with IF statements affecting one command
- The flow control affects a block of code - which is the case if major portions of the program are affected by a loop or condition.
[edit] True or False
Boolean logic is the logic of things being true or false. In programing this logic is used to control what a program does. In order for the computer to decided if something is true or not, it must be stated in math terms. You cannot say 'Is it six o'clock', you have ask if 'time = 6.00'. The comparisons used in qbasic are equal (=), less than (<), greater than (>), less than or equal to (=<), greater than or equal to (>=) and not equal (<>). Also, to link together more than one comparison, there are logical operators. These are 'AND', 'OR' and 'NOT'. We will cover exactly what these mean later on, but you probably understand the first two already.
[edit] IF
One of the most useful statements in QBasic is the IF statement. It allows you to choose what your program will do depending on the conditions you give it. The next few programs will be taking a look at ways to use the IF statement.
[edit] 12IF.BAS
CLS num = INT(RND * 100) + 1 INPUT "Pick a number between 1 and 100: ", answer IF num = answer THEN PRINT "You win" IF num > answer THEN PRINT "Too Small" IF num < answer THEN PRINT "Too big"
[edit] Basic IF...THEN
12IF.BAS shows the most basic use of the IF...THEN statement. The format is
IF [conditional] THEN [do this]
In this program the conditionals check what the guess is relative to answer. One side note, this program does not have RANDOMIZE TIMER intentionally, this will allow you to play the game and keep getting closer to the number, the drawback is that once you find the number, the game has no replay value. We will make a better version a little later though.
[edit] 13IFELSE.BAS
CLS num = INT(RND * 20) + 1 INPUT "Pick a number between 1 and 20: ", answer IF answer = num THEN PRINT "You Win" ELSE PRINT "You Lose"
[edit] IF...THEN...ELSE
This version of the IF statement adds the the ELSE operator. The ELSE operator allows you to specify what happens if the conditional fails.
IF [conditional] THEN [do this 1] ELSE [do this 2]
So, if the conditional is true 'do this 1' will happen if the conditional is not true 'do this 2' will happen.
If needed, you can add ELSEIF to the IF...THEN statement. This allows a construct known as an IF...ELSE ladder, where the QBasic interpreter can select one code path based on a series of condition checks.
[edit] LOGIC
When forcing the computer to compare two variables with the IF statement, you must keep this in mind. "a" < "d" for A begins in the alphabet earlier than D. So, if you had, for example, a variable name D$="a" and a variable name A$="d" then the logical expression would end up coming out as... D$<A$ TRUE
[edit] 14FOR.BAS
CLS num = INT(RND * 20) + 1 FOR count = 1 TO 5 INPUT "Pick a number between 1 and 20: ", answer IF answer = num THEN PRINT "You win after";count;"guesses!": END NEXT PRINT "You lose"
[edit] FOR...NEXT
The for loop allows you to create a counter - the variable may either be used within the loop, or in this case, provide a limit to the number of loops within a block of code. In the example above, the user is given five chances to guess the number, and if successful, is told how many attempts he made.
The syntax for the for statement is:
FOR <variable> = <start> TO <end> [STEP <increment>]
The incrementer may be positive or negative - if positive, it counts up, and if negative, it counts downward. The for loop stops when its value passes end after being incremented.
You can exit a for loop early with the EXIT FOR command.
[edit] 15WHILE.BAS
PRINT "Press any key to continue" WHILE INKEY$="" WEND
[edit] WHILE...WEND
The WHILE loop continues executing a block of code until the condition becomes false. In the example above, you see a press any key prompt that waits until the user presses a key. (The INKEY$ feature will be described under Advanced Input.)
[edit] DO...LOOP
The DO...LOOP construct is a more advanced of the WHILE loop - as with other flow control blocks, it is marked by DO and LOOP to denote the boundaries.
It relies on a conditional statement placed after either DO or LOOP:
DO a$ = INKEY$ LOOP WHILE a$=""
As an alternative, you can instead replace WHILE with UNTIL have the loop continue until a specific condition is met:
DO x=x+1 LOOP UNTIL x >= 10
[edit] SELECT CASE
The select statement is a substitute for repeated use of IF statements. It evaluates the test expression, and jumps to the appropriate control block. It supports strings and numbers, as well as multiple options within one case block.
INPUT "Enter your guess (1-10): ", guess SELECT CASE guess CASE IS > 10 PRINT "Your guess is out of range." CASE 1,2 PRINT "Your guess is too low." CASE 3 PRINT "Your guess is correct." CASE 4 TO 10 PRINT "Your guess is too high." END SELECT
If a parameter would be covered by more than one case statement, the first option will take priority.