Conditional Statements

Conditional statements are implemented with the IF...THEN...ELSE...END IF structure.

See also: Boolean Evaluations, IF...THEN, Select Case

Example forms:

    IF test expression THEN expression(s)

    IF test expression THEN expression(s)1 ELSE expression(s)2

    IF test expression THEN
        one or more statements
    END IF

    IF test expression THEN
        one or more statements
    ELSE
        one or more other statements
    END IF

Description:

The IF...THEN statement provides a way to change program flow based on a test expression. For example, the following line directs program execution to branch label [soundAlarm] if fuel runs low.

    if fuel < 5 then [soundAlarm]

Another way to control program flow is to use the IF...THEN...ELSE statement. This extended form of IF...THEN adds expressiveness and simplifies coding of some logical decision-making software. Here is an example of its usefulness.

Consider:

[retry]
    input "Please choose mode, (N)ovice or e(X)pert?"; mode$
    if len(mode$) = 0 then print "Invalid entry! Retry" : goto [retry]
    mode$ = left$(mode$, 1)
    if instr("NnXx", mode$) = 0 then print "Invalid entry! Retry" : goto [retry]
    if instr("Nn", mode$) > 0 then print "Novice mode" : goto [main]
    print "eXpert mode"
[main]
    print "Main Selection Menu"

The conditional lines can be shortened to one line as follows:

if instr("Nn",mode$)> 0 then print "Novice mode" else print "eXpert mode"

Some permitted forms are as follows:

    if a < b then statement else statement
    if a < b then [label] else statement
    if a < b then statement else [label]
    if a < b then statement : statement else statement
    if a < b then statement else statement : statement
    if a < b then statement : goto [label] else statement
    if a < b then gosub [label1] else gosub [label2]

Any number of variations on these formats are permissible. The (a < b) BOOLEAN expression is of course only a simple example chosen for convenience. It must be replaced with the correct expression to suit the problem.

IF...THEN...END IF is another form using what are called conditional blocks. This allows great control over the flow of program decision making. Here is an example of code using blocks.

if qtySubdirs = 0 then
    print "None."
    goto [noSubs]
end if

A block is merely one or more lines of code that are executed as a result of a conditional test. There is one block in the example above, and it is executed if qtySubdirs = 0.

It is also possible to use the ELSE keyword as well with blocks:

if qtySubdirs = 0 then
    print "None."
else
    print "Count of subdirectories: "; qtySubdirs
end if

This type of coding is easy to read and understand. There are two blocks in this example. One is executed if qtySubdirs = 0, and one is executed if qtySubdirs is not equal to 0. Only one of the two blocks will be executed (never both as a result of the same test).

These conditional blocks can be nested inside each other:

if verbose = 1 then
    if qtySubdirs = 0 then
        print "None."
    else
        print "Count of subdirectories: "; qtySubdirs
    end if
end if

In the example above, if the verbose flag is set to 1 (true), then display something, or else skip the display code entirely.

Comparison to QBasic Conditional Statements

Liberty BASIC supports conditional blocks very similar to those in QBasic. The format looks like the following silly example:

if blah blah blah then
    some code here
end if

or like:

if blah blah blah then
    some code here
else
    some other code here
end if

Blocks may be nested:

if sis boom bah then
    if blah blah blah then
        some code here
    end if
else
    some other code here
end if

The elseif keyword is not supported. Here is an example using elseif (QBasic):

'QBasic only
if sis boom bah then
    print "Yippie!"
elseif la dee da the
    print "So what!"
end if

Instead in Liberty BASIC, it looks like this:

'Liberty BASIC - no elseif
if sis boom bah then
    print "Yippie!
else
    if la dee da then
        print "So what!"
    end if
end if