IF...THEN Statement Programming Example
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"