QBasic/Flow Control

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

Conditional execution[edit | edit source]

To choose between two or more sections of the program to execute, the IF statement can be used. It is also possible to use the WHILE, DO UNTIL and CASE statements. All of these control conditional execution by using a Boolean logic 'test', the result of which is either TRUE or FALSE. To repeat a section of code for a set number of times, the FOR statement is used.

The IF test can be executed in a single line, however it can also be used like the others to control a block of code.

True or False[edit | edit source]

Boolean logic is a test that yields one of only two possible results, true or false. The tests are always mathematical in nature .. when two characters (or strings) are 'compared' it is their ASCII codes that are used (thus a < b and b < A).

The comparison operators used in qbasic are: = true if two variables are equal < true if the first is less than the second =< true if the first is less than or equal to the second > true if the first is greater than the second >= true if the first is greater than or equal to the second <> true if the two are unequal

Multiple tests can be linked together in the comparison, using the 'AND', 'OR' and 'NOT' operators. We will cover exactly what these mean later on, but you probably understand the first two already.

IF[edit | edit source]

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.

IF [conditional] THEN [do this]

The single line IF is the simplest example. To execute a block of code, the END IF is used

 IF [conditional] THEN
   [do this]
   [and do this]
    ...
   [and also do this]
 END IF

IF...THEN...ELSE[edit | edit source]

IF [conditional] THEN [do this] ELSE [do that]

To choose between two different code blocks, the ELSE statement is used.

IF [conditional] THEN
  [do this]
  ..
  [and do this]
ELSE
  [do that]
  ..
  [and also that]
END IF

13 ELSEIF[edit | edit source]

As an alternative to starting an entirely new IF THEN ELSE statement sequence. You can follow the THEN statement(s) with ELSEIF [conditional] THEN. This does not create a new level of nesting.

IF [conditional] THEN

 [do this]
 ..
 [and do this]

ELSEIF [conditional] THEN

 [do that]
 ..
 [and also that]

ELSEIF [conditional] THEN

 [do the other]
 ..
 [and also ...]

ELSE

 [do this final thing]

END IF

FOR...NEXT[edit | edit source]

FOR <variable name> = <start value> TO <end value> [STEP <increment>]
 [do this]
 ...
 [and do this]
NEXT

<increment> may be + or - and is optional. If omitted the default is +1. The code contained within the FOR loop will always be executed at least once because it is only at the 'NEXT' statement that the value of the variable is checked against the end value.

When the NEXT statement executes, the variable is modified by STEP value and compared against the end value. If the variable has not yet exceeded the end value, control is returned to the line following the FOR.

You can exit a FOR loop early with the EXIT FOR command.

14FOR.BAS[edit | edit source]

 CLS
 RANDOMIZE TIMER

 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"

WHILE...WEND[edit | edit source]

 WHILE <condition is true>
   [do this]
   ..
   [and this]
 WEND

If the condition is true, the code following the WHILE is executed. When the WEND command is executed, it returns control to the WHILE statement (where the condition is tested again). When the condition evaluates to FALSE, control is passed to the statement following the WEND.

15WHILE.BAS[edit | edit source]

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

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 | edit source]

 DO
   [this]
   ..
   [and this]
 LOOP WHILE <condition is true> / LOOP UNTIL <condition is true>

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

In some versions of BASIC the UNTIL or WHILE condition can follow the DO statement rather than the LOOP statement (pre-test) as apposed to the above shown (post-test).

12IF.BAS[edit | edit source]

 CLS
 RANDOMIZE TIMER

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

SELECT CASE[edit | edit source]

 SELECT CASE <variable expression>
   CASE <value>
     [do this]
   CASE <value 2>
     [do instead]
   ...
   CASE ELSE
   ...
 END SELECT

The select statement is a substitute for repeated use of IF statements. The <variable expression> is evaluated and compared against each CASE <value> in turn. When a CASE <value> is found to match, the [do this] code following is executed. If an EXIT CASE is executed, control passes to the line following the END SELECT, otherwise the next CASE <value> is checked. If no matches are found, the CASE ELSE is executed. Note that <value> may be a number, character or string or logical expression (eg '>0', '<>1'). Note also that multiple CASE matches may be found and executed (so, for example, if two CASE <values> are 'CASE >1' and 'CASE >10', a <variable expression> that evaluates to 11 (or more) will result in both CASE >1 and CASE >10 being executed)

CLS
PRINT "WELCOME"
PRINT "I HAVE AN ANSWER FOR ANY OF YOUR QUESTIONS"
10 INPUT "WRITE YOUR QUESTION AND I'LL GIVE YOU AN ANSWER ", question$
RANDOMIZE TIMER
PRINT
answer = INT(RND * 4 + 1)
SELECT CASE answer
    CASE 1
        PRINT "PLEASE REPHRASE YOUR QUESTION."
    CASE 2
        PRINT "YOUR QUESTION IS MEANINGLESS."
    CASE 3
        PRINT "DO YOU THINK I CAN ANSWER THIS?"
    CASE 4
        PRINT "THIS QUESTION LOOKS FUNNY."
END SELECT
PRINT
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.