If.... Then... Else Statement

Top  Previous  Next

 

Function:

A way to conditionally execute code.

Syntax:

 

if expression then

       statement1

       statement2

       

[ else

       statement1

       statement2

       

]

end if

 

or:

 

if expression then true_statement1 : true_statement2 …

Scope:

Local and HTML

See Also:

Select-Case Statement

 


Part

Description

expression

Required. The result of this expression (true or false) is then used to determine what code will execute.

statement[1, 2...]

Required. Statements to execute.

:

Optional. Separator for multiple statements on a single line. Covered under Programming Fundamentals.

Details

When the expression evaluates to true, the block of code immediately following the then keyword is executed. If it evaluates to false, the code immediately following the else keyword is executed; if there is no else keyword, program flow resumes from the line immediately following the end if keyword.

When using the single-line syntax (as in the lower example above), an if statement must not be terminated using an end if. This is the only construct under Tibbo Basic where line end matters. So, if you want to have several statements in such a construct, you need to place them all in the same line, and separate them with colons.

If... then... else statements may be nested.

note_further-wt

Currently, single-line if... then statements cannot contain an else clause.

The elseif syntax is not currently supported at all (even on multi-line if... then statements).

Examples

 

if net.failure=1 then

       if sys.runmode=0 then ser.setdata("Ethernet failure!"):ser.send 'massage when in debug mode

       sys.halt

else

       if net.linkstate=0 then

               ser.setdata("No Ethernet link!")

       else

               if net.linkstate=1 then

                       ser.setdata("Linked at 10Mbit/s.")

               else

                       ser.setdata("Linked at 100Mbit/s.")

               end if

       end if

       ser.send

end if