Q(uick)BASIC Statement: IF...THEN...ELSE

Quick View

IF...THEN...ELSE

A control flow statement that allows conditional execution or branching, based on the evaluation of an expression that must be either true or false

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • IF condition1 THEN[statementblock-1][ELSEIF condition2 THEN[statementblock-2]]…[ELSE[statementblock-n]]END IF
  • IF condition THEN statements [ELSE statements]
Description/Parameter(s)
condition1
condition2
Any expression that can be evaluated as true (nonzero) or false (zero).
statementblock-1
statementblock-2
statementblock-n
One or more statements on one or more lines.
statements One or more statements, separated by colons.
Example
INPUT "1 or 2? ", i% IF i% = 1 OR i% = 2 THEN PRINT "OK" ELSE PRINT "Out of range" END IF
Description/Parameter(s)

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, statementblock-n One or more BASIC 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 linelabelA 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
Examples

These examples show the use of single-line and block IF...THEN...ELSE statements.
Here is the single-line form:

CLS ' Clear screen DO INPUT "Enter a number greater than 0 and less than 10,000:", X IF X >= 0 AND X < 10000 THEN EXIT DO ELSE PRINT X; "out of range" LOOP IF X<10 THEN Y=1 ELSE IF X<100 THEN Y=2 ELSE IF X<1000 THEN Y=3 ELSE Y=4 PRINT "The number has"; Y; "digits"

Here is the block form, which is easier to read and more powerful:

CLS ' Clear screen DO INPUT "Enter a number greater than 0 and less than 100,000:", X IF X >= 0 AND X < 100000 THEN EXIT DO ELSE PRINT X; "out of range" END IF LOOP IF X < 10 THEN Y = 1 ELSEIF X < 100 THEN Y = 2 ELSEIF X < 1000 THEN Y = 3 ELSEIF X < 10000 THEN Y = 4 ELSE Y = 5 END IF PRINT "The number has"; Y; "digits"
Syntax
  • IF condition THEN thenpart [ELSE elsepart]
  • IF condition1 THEN[statementblock-1][ELSEIF condition2 THEN[statementblock-2]] …[ELSE[statementblock-n]]END IF
Description/Parameter(s)
  • 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 flexibility by allowing conditional branches across several lines.
  • Tests more complex conditions.
  • Lets you use longer statements and 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.
  • Best for short, straightforward tests where only one action is taken.
  • 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
Examples

These examples use single-line and block IF...THEN...ELSEstatements to determine how many digits are in a number.
Here is the single-line form:

CLS 'Clear screen. DO INPUT "Enter a number greater than 0 and less than 10,000:", X IF X >= 0 AND X < 10000 THEN EXIT DO ELSE PRINT X; "out of range" LOOP IF X < 10 THEN Y = 1 ELSE IF X < 100 THEN Y = 2 ELSE IF X < 1000 THEN Y = 3 ELSE Y = 4 PRINT "The number has"; Y; "digits"

Here is the block form, which is easier to read and more powerful:

CLS 'Clear screen. DO INPUT "Enter a number greater than 0 and less than 100,000:", X IF X >= 0 AND X < 100000 THEN EXIT DO ELSE PRINT X; "out of range" END IF LOOP IF X < 10 THEN Y = 1 <b>ELSEIF</b> X < 100 THEN Y = 2 <b>ELSEIF</b> X < 1000 THEN Y = 3 <b>ELSEIF</b> X < 10000 THEN Y = 4 ELSE Y = 5 END IF PRINT "The number has"; Y; "digits"