15. Conditional Execution

[<<<] [>>>]

Conditional execution is a vital point in any programming language. Conditional execution can be performed using the statement IF. This statement has the following format:

If expression then
 ' Code to execute when the
 ' expression is true
End if

Or it may have the format:

If expression then
 ' Code to execute when the
 ' expression is TRUE
Else
 ' Code to execute when the
 ' expression is FALSE
End if

Or even it may have the format:

If expression then
 ' Code to execute when the
 ' expression is TRUE

Elseif expression2 then ' Code to execute when the ' expression2 is TRUE End if

And even the format:

If expression then
 ' Code to execute when the
 ' expression is TRUE
Elseif expression2 then
 ' Code to execute when the
 ' expression2 is TRUE
Else
 ' Code to execute when both
 ' expression and expression2 are
 ' FALSE.
End if

When closing a conditional execution construct with the keyword ENDIF you can write it as one word or as two separate words, like END IF. Also you have a great freedom spelling the keyword ELSEIF. You can use any of the followings: ELSE IF (two words), ELSEIF, ELSIF or ELIF.

Most BASIC languages have the single line format of the IF statement where the conditionally executed command is on the same line as the command IF and there is no ENDIF required. You can have the same in ScriptBasic. You can write:

IF expression THEN command

In this case the command is executed only if the expression standing after the keyword IF is TRUE. Unfortunately you can not have ELSE in this case. If you need the ELSE branch then use the multi line format of the conditional execution closing the ELSE branch with ENDIF.

When using the single-line format of the command IF there are some restrictions on the command that follows. The command executed conditionally can not be any declaration type command, like Const, Module or Declare Sub and can not be starting or ending a loop. Finally this command can not be another IF, ELSEIF, ELSE or ENDIF.


[<<<] [>>>]