Programs often need to make branching decisions. In MiniBasic this is provided by the IF ... THEN statement.
10 REM Square root program.
20 PRINT "Enter a number"
30 INPUT x
40 REM Square root of negative is not allowed
50 IF x < 0 THEN 20
60 PRINT "Square root of", x, "is", SQRT(x)
We have also introduced the REM statement. This simply adds comments to make the program easier for a human to understand. REM statements are ignored by the interpreter.
This program is actually not very effective. Really we should tell the user what is wrong. For this, we need the GOTO statement. A GOTO simply executes an unconditional jump.
10 REM Improved square root program
20 PRINT "Enter a number"
30 INPUT x
40 REM Prompt user if negative
50 IF x > = 0 THEN 80
60 PRINT "Number may not be negative"
70 GOTO 30
80 PRINT "Square root of ", x, "is", SQRT(x)
An IF ... THEN statement always takes a line number as an argument. This can be of the form IF x < y THEN b, as long as b holds a valid line number.
The operators recognised by the IF ... THEN statement are "=", "<>" (not equals), ">", "<", ">=", "<=".
We can also use AND and OR to build up complex tests
eg
IF age >= 18 AND age < 65 THEN x
Use parentheses to disambiguate lengthy tests.
e.g.
IF age >= 18 AND (age < 65 OR job$ = "Caretaker")
The IF ... THEN operators can also be applied to string variables. In this case the strings are ordered alphabetically.