IF Statements

<< Click to Display Table of Contents >>

Home  mIoTa BASIC > mIoTa BASIC Language Reference > Statements > Conditional Statements >

IF Statements

Use the keyword 'IF' to implement a conditional statement. The syntax of the if statement takes the following form using IF, THEN and ELSE keywords ..

 

if expression then statement1 [else statement2];

 

If 'expression' evaluates to true then 'statement1' executes. If 'expression' is false then 'statement2' executes.

 

The expression must evaluate to a boolean type; otherwise, the condition is ill-formed.

 

The ELSE keyword with an alternate statement (statement2) is optional.

 

Nested IF statements

A general rule is that the nested conditionals are parsed starting from the innermost conditional, with each ELSE bound to the nearest available IF on its left ..

 

if expression1 then

  if expression2 then statement1

    else statement2;

 

The compiler treats the above example in this way ..

 

if expression1 then

begin  

 if expression2 then

   statement1  

 else

   statement2;

end;

 

Only if expression1 evaluates to true will statement1 or statement2 be executed according to the result of expression2.

 

In order to force the compiler to interpret our example the other way around, we have to write it explicitly using BEGIN and END ..

 

if expression1 then

begin

 if expression2 then statement1

end

else

 statement2;

 

Now, statement1 will be executed if expression1 evaluates to false, otherwise statement1 will be executed if expression2 evaluates to true.