QBasic/Flow Control

From Wikibooks, open books for an open world
Jump to: navigation, search

Contents

Flow control [edit]

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.

True or False [edit]

Boolean logic is the logic of things being true or false. In programming 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.

IF [edit]

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.

12IF.BAS [edit]

 CLS
 num = INT(RND * 100) + 1
 DO 
      INPUT "Pick a number between 1 and 100: ", answer
 
      IF num = answer THEN PRINT "You Got It!"
      IF num > answer THEN PRINT "Too Small"
      IF num < answer THEN PRINT "Too big"
LOOP UNTIL num = answer
PRINT "Game Over."

Basic IF...THEN [edit]

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.

13IFELSE.BAS [edit]

 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"

IF...THEN...ELSE [edit]

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.

LOGIC [edit]

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

14FOR.BAS [edit]

 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"

FOR...NEXT [edit]

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.

15WHILE.BAS [edit]

PRINT "Press any key to continue"
WHILE INKEY$=""
WEND

WHILE...WEND [edit]

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.)

DO...LOOP [edit]

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

SELECT CASE and RANDOM SELECTION [edit]

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.

CLS
PRINT "WELCOME"
PRINT "I HAVE ANSWER FOR ANY OF YOUR QUESTION"
INPUT "WRITE YOUR QUESTION AND I'L GIVE YOU ANSWER ", question$
10 RANDOMIZE TIMER
PRINT
answer=INT(RND*10+1)
SELECT CASE answer
 
 PRINT "pls rephrase your question ."
  CASE 2
    PRINT "Your question is meaningless ."
  CASE 3
    PRINT "do you think i cant answer this."
  CASE 4 
    PRINT "the question looks funny."
END SELECT
PRINT "ENTER ANOTHER QUESTION", K$
GOTO 10

If a parameter would be covered by more than one case statement, the first option will take priority.