NAME

if...then...else - if-then-else statement

SYNOPSIS

if NumExpr then statements [else statements]
if NumExpr goto LineLab [else statements]
if NumExpr goto LineNum [else statements]
if NumExpr then else statements

if NumExpr then
[statements]

[elseif NumExpr then
[statements]
] . . . 

[else
[statements]
]

end if

DESCRIPTION

The if...then...else statement is used to select one of two or more branches based on the value of a conditional expression.  NumExpr may be any numeric expression.  If NumExpr evaluates to a non-zero value, the conditional is said to be "true"; otherwise it is "false." The varieties of if...then...else statements are two: the single line statements and the "block if" statement. 

For the single line statements, note that in the Synopsis above a line number may appear in place of statements as shorthand for "goto LineNum" (see goto).  Also, note that "goto" may appear as shorthand for "then goto." LineNum is a line number and LineLab is a line label.  Statements are one or more colon separated statements, including if...then...else statements.  That is, if...then...else statements may be nested.  Execution proceeds as follows: The conditional is evaluated.  If true and a then clause appears, it is executed.  If false and a else clause appears, it is executed.  If a selected clause is missing and when the execution of a clause is completed, execution continues with the next executable statement after the if...then...else statement.  The then clause is composed of those statements that appear after the word then but before the word else (if present).  The else clause is composed of those statements that appear after the word else

For the block if statement, note that an if...then statement must appear as the only statement on a physical line of a file.  Also, note that an elseif...then, else or end if statement must appear as the first statement on a line.  The form of the block if statement is an if...then followed by zero or more elseif...then statements followed by an optional else statement followed by an end if statement.  Other statements may appear between these block if statements.  Block if statements may be nested.  Execution proceeds as follows: The conditional of the if...then statement is evaluated.  If true and statements appear after the then, they are executed.  If false, the conditional of the first elseif...then statement (if any) is evaluated.  If that conditional is true and statements appear after the then, they are executed.  The conditional of each elseif...then is evaluated in turn until a true value results.  If all are false and an else statement appears, the statements after the else are executed.  If a selected set of statements is missing and when the execution of a selected set of statements is completed, execution continues with the next executable statement after the end if statement. 

EXAMPLE

The statement
	if x < y then 10 else if x = y then 20 else 30
branches three ways: to line 10, 20 or 30 based on whether the comparison of "x" and "y" is less than, equal to or greater than.  This statement can be made more readable as a block if statement:
	if x < y then
		goto 10
	elseif x = y then
		goto 20
	else
		goto 30
	end if
The program
loop:	input "which year"; y%
	if y% = 0 then end
	if y% mod 4 = 0 then
		if y% mod 100 = 0 then
			if y% mod 400 = 0 then
				print y% "is a leap year"
			else	print y% "is not a leap year"
			end if
		else	print y% "is a leap year"
		end if
	else	print y% "is not a leap year"
	end if
	goto loop
might be run as follows:
	$ a.out
	which year? 1987
	 1987 is not a leap year
	which year? 1984
	 1984 is a leap year
	which year? 1900
	 1900 is not a leap year
	which year? 2000
	 2000 is a leap year
	which year? 0
	$

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber