If Statement

Use the keyword if to implement a conditional statement. The syntax of the if statement has the following form:

if expression then
  statements
[else
  other statements]
end if

When expression evaluates to true, statements execute. If expression is false, other statements execute. The expression must convert to a boolean type; otherwise, the condition is ill-formed. The else keyword with an alternate block of statements (other statements) is optional.

Nested if statements

Nested if statements require additional attention. 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
end if
end if

The compiler treats the construction in this way:

if expression1 then
  if expression2 then
    statement1
  else
    statement2
  end if
end if

In order to force the compiler to interpret our example the other way around, we have to write it explicitly:

if expression1 then
  if expression2 then
    statement1
  end if
else
  statement2
end if
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code