BASIC Programming/Beginning BASIC/Control Structures/IF...THEN...ELSEIF...ELSE
| This page or section is an undeveloped draft or outline.
You can help to develop the work, or you can ask for assistance in the project room.
The IF...THEN...ELSEIF...ELSE control statement allows identifying if a certain condition is true, and executes a block of code if it is the case.
IF number<0 THEN PRINT "Number is negative" ELSEIF number>0 THEN PRINT "Number is positive" ELSE PRINT "Number is zero" END IF
In some implementations of BASIC (but permitted by most versions), the IF statement may need to be contained in one line. However, ELSEIF may not be available in this case, and there is no need for an explicit END IF:
IF number<0 THEN PRINT "Number is negative" ELSE PRINT "Number is non-negative"
This carries over into some implementations of BASIC where if the "IF...THEN" statement is followed by code on the same line then it is fully contained. That is, the compiler assumes the lines ends with "ENDIF", even if it not stated. This is important when dealing with nested "IF...THEN" clauses:
IF X<2 THEN IF 2<3 THEN PRINT "This is printed if X is 1" ELSE IF 3<4 THEN PRINT "This is printed if X is 3" END IF
The ELSE clause, while following the "IF 2<3" statement, is associated with the "IF X<2" statement, because the "IF 2<3" statement has a PRINT statement on the same line.