17.07.2013 Views

GWBASIC User's Manual

GWBASIC User's Manual

GWBASIC User's Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

GW-BASIC <strong>User's</strong> Guide<br />

IF ... THEN ... ELSE Statement<br />

Purpose:<br />

To make a decision regarding program flow based on the result returned by an expression.<br />

Syntax:<br />

IF expression[,] THEN statement(s)[,][ELSE statement(s)]<br />

IF expression[,] GOTO line number[[,] ELSE statement(s)]<br />

Comments:<br />

If the result of expression is nonzero (logical true), the THEN or GOTO line number is executed.<br />

If the result of expression is zero (false), the THEN or GOTO line number is ignored and the<br />

ELSE line number, if present, is executed. Otherwise, execution continues with the next<br />

executable statement. A comma is allowed before THEN and ELSE.<br />

THEN and ELSE may be followed by either a line number for branching, or one or more<br />

statements to be executed.<br />

GOTO is always followed by a line number.<br />

If the statement does not contain the same number of ELSE's and THEN's line number, each ELSE<br />

is matched with the closest unmatched THEN. For example:<br />

IF A=B THEN IF B=C THEN PRINT "A=C" ELSE PRINT "A < > C"<br />

will not print "A < > C" when A < > B.<br />

If an IF...THEN statement is followed by a line number in the direct mode, an "Undefined<br />

line number" error results, unless a statement with the specified line number was previously<br />

entered in the indirect mode.<br />

Because IF ..THEN...ELSE is all one statement, the ELSE clause cannot be on a separate line.<br />

It must be all on one line.<br />

Nesting of IF Statements<br />

IF...THEN...ELSE statements may be nested. Nesting is limited only by the length of the line.<br />

For example, the following is a legal statement:<br />

100 IF X > Y THEN PRINT "GREATER" ELSE IF Y > X THEN&<br />

110 PRINT "LESS THAN"<br />

200 ELSE PRINT "EQUAL"<br />

file:///C|/Documents%20and%20Settings/Lorenzo/Desktop/GW%20Basic/IF.html (1 of 2)28/03/2004 21.29.29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!