qb45advr.hlp (Topic list)
IF...THEN...ELSE Statement Details
  QuickSCREEN      Details      Example      Contents      Index
──────────────────────────────────────────────────────────────────────────────
Block IF...THEN...ELSE Details
 
  IF booleanexpression1 THEN
     [statementblock-1]
  [ELSEIF booleanexpression2 THEN
     [statementblock-2]
  . . .
  [ELSE
     [statementblock-n]]
  END IF
 
  Part                                     Description
 
  booleanexpression1, booleanexpression2   Any expression that
                                           evaluates to true (nonzero)
                                           or false (zero)
 
  statementblock-1, statementblock-2,      One or more BASIC
  statementblock-n                         statements on one or more
                                           lines
 
In executing a block-form IF, QuickBASIC tests the first Boolean
expression (booleanexpression1). If the Boolean expression
is true (nonzero), the statements following THEN are executed.
If the first Boolean expression is false (zero), QuickBASIC begins
evaluating each ELSEIF condition in turn. When QuickBASIC finds
a true condition, the statements following the associated THEN
are executed. If none of the ELSEIF conditions are true, the
statements following the ELSE are executed. After the statements
following a THEN or ELSE are executed, the program continues
with the statement following the END IF.
 
The ELSE and ELSEIF blocks are both optional. You can have as many
ELSEIF clauses as you want in a block IF. Any of the statement blocks
can contain nested block IF statements.
 
QuickBASIC looks at what appears after the THEN keyword to determine
whether or not an IF statement is a block IF. If anything other than
a comment appears after THEN, the statement is treated as a single-
line IF statement.
 
A block IF statement must be the first statement on a line. The ELSE,
ELSEIF, and END IF parts of the statement can only have a line number
or line label in front of them. The block must end with an END IF
statement.
 
The block form of the IF...THEN command provides several advantages over
the single-line form:
 
  ■ The block form provides more structure and flexibility than the
    single-line form by allowing conditional branches across several
    lines.
  ■ With the block form, more complex conditions can be tested.
  ■ The block form lets you use longer statements and structures
  ■ The block form allows your program's structure to be guided by
    logic rather than by how many statements fit on a line.
  ■ Programs that use block-form IF...THEN...ELSE are usually easier
    to read, maintain, and debug.
 
 
Single-line IF...THEN...ELSE
 
Syntax
  IF booleanexpression THEN thenpart [ELSE elsepart]
 
The single-line form of the statement is best used for short,
straightforward tests where only one action is taken.
 
The following list describes the parts of the single-line form:
 
  Part                 Description
  booleanexpression    Any expression that evaluates to true (nonzero)
                       or false (zero).
  thenpart, elsepart   The statements or branches performed when
                       booleanexpression is true (thenpart) or false
                       (elsepart). Both parts have the same syntax,
                       which is described below.
 
The thenpart and the elsepart both have the following syntax:
 
  {statements | [GOTO]linenumber | GOTO linelabel }
 
The following list describes the parts of the thenpart and
elsepart syntax:
 
  Part         Description
  statements   One or more BASIC statements, separated by colons
 
  linenumber   A valid BASIC program line number
 
  linelabel    A valid BASIC line label
 
Note that GOTO is optional with a line number but is required
with a line label.
 
The thenpart is executed if the booleanexpression is true; if the
booleanexpression is false, the elsepart is executed. If the ELSE
clause is not present, control passes to the next statement in the
program.
 
You can have multiple statements with a condition, but they must be
on the same line and separated by colons:
 
  IF A > 10 THEN A = A + 1: B = B + A: LOCATE 10,22: PRINT B,A