QBasic/Flow Control

Conditional execution
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
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
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
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
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
FOR = TO [STEP ] [do this] ... [and do this] NEXT

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.

WHILE...WEND
WHILE [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
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
DO   [this] ..   [and this] LOOP WHILE / LOOP UNTIL

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:

As an alternative, you can instead replace WHILE with UNTIL have the loop continue until a specific condition is met:

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

SELECT CASE
SELECT CASE CASE [do this] CASE [do instead] ...   CASE ELSE ... END SELECT

The select statement is a substitute for repeated use of IF statements. The is evaluated and compared against each CASE in turn. When a CASE 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 is checked. If no matches are found, the CASE ELSE is executed. Note that 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 are 'CASE >1' and 'CASE >10', a that evaluates to 11 (or more) will result in both CASE >1 and CASE >10 being executed)

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