bas7advr.hlp (Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
IF...THEN...ELSE Statement Details
  Syntax  Details  Example                 Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
IF...THEN...ELSE allows conditional execution based on the evaluation of a
Boolean expression.
 
Syntax 1 (single-line form)
  IF condition THEN thenpart [ELSE elsepart]
 
Syntax 2 (block form)
  IF condition1 THEN
     [statementblock-1]
  [ELSEIF condition2 THEN
     [statementblock-2]] ...
  [ELSE
     [statementblock-n]]
  END IF
 
    ■ The argument condition is an expression that BASIC evaluates as
      true (nonzero) or false (zero).
    ■ The argument statementblock includes any number of statements on
      one or more lines.
    ■ The argument thenpart includes the statements or branches performed when
      condition is true. The syntax of thenpart is:
 
        {statements | [GOTO] linenumber | GOTO linelabel}
 
           statements    One or more BASIC statements, separated
                             by colons.
           linenumber    A valid BASIC program line number.
           linelabel     A valid BASIC line label.
 
    ■ The argument elsepart includes the statements or branches performed
      when condition is false. The syntax is the same as thenpart.
      If the ELSE clause is not present, control passes to the next
      statement in the program.
 
Usage Notes
    ■ The advantages of the single-line and block forms of the syntax are:
 
        Block form                                Single-line form
        ═════════════════════════════════════     ══════════════════
        ■ Provides more structure and             ■ Best for short,
          flexibility by allowing conditional       straightforward
          branches across several lines.            tests where only
        ■ Tests more complex conditions.            one action is
        ■ Lets you use longer statements and        taken.
          structures.
        ■ Allows your program's structure to
          be guided by logic rather than by
          how many statements fit on a line.
        ■ Programs are usually easier to read,
          maintain, and debug.
 
    ■ In executing a block-form IF, BASIC tests the first Boolean expression
      (condition1). If the Boolean expression is true (nonzero),
      the statements following THEN are executed. If the first Boolean
      expression is false (zero), BASIC begins evaluating each ELSEIF
      condition in turn. When BASIC finds a true condition, the statements
      following the associated THEN are executed. If none of the ELSEIF
      conditions is 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.
    ■ BASIC looks at what appears after the THEN keyword to determine
      whether 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 have only a line number
      or line label in front of them. The block must end with an END IF
      statement.
    ■ Note that GOTO is optional with a line number but is required
      with a line label.
    ■ The single-line form is never required. Any program using single-line
      IF...THEN...ELSE statements can be written using block form.
    ■ 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