if...elsif...else...endif Statements

An if statement consists of a boolean expression followed by one or more statements.
The syntax of if statement is −
if expression then
   -- Statements will execute if the expression is true
end if

If the boolean expression evaluates to true then the block of code inside the if statement is executed. If it evaluates to false, then the first set of code after the end of the if statement is executed.
#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

if (a + b) < 40 then
   printf(1, "%s\n", {"This is true if statement!"})
end if

if (a + b) > 40 then
   printf(1, "%s\n", {"This is not true if statement!"})
end if
This produces the following result −
This is true if statement!

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
The syntax of if...else statement is as follows −
if expression then
   -- Statements will execute if the expression is true
else
   -- Statements will execute if the expression is false
end if
#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

if (a + b) < 40 then
   printf(1, "%s\n", {"This is inside if statement!"})
else
   printf(1, "%s\n", {"This is inside else statement!"})
end if
This produces the following result −
This is inside if statement!

An if statement can be followed by any number of optional elsif...else statement, which is very useful to test various conditions using single if...elsif statement.
The syntax of if...elsif...else statement is as follows −
if expression1 then
   -- Executes when the Boolean expression 1 is true
elsif expression2 then
   -- Executes when the Boolean expression 2 is true
elsif expression3 then
   -- Executes when the Boolean expression 3 is true
else
   -- Executes when none of the above condition is true.
end if

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

if (a + b) = 40 then
   printf(1, "Value of (a + b ) is  %d\n", a + b )
elsif (a + b) = 45 then
    printf(1, "Value of (a + b ) is  %d\n", a + b )
elsif (a + b) = 30 then
    printf(1, "Value of (a + b ) is  %d\n", a + b )
else
    printf(1, "Value of (a + b ) is  %d\n", 0 )
end if
This produces the following result −
Value of (a + b ) is  30

An if statement can have a label clause just before the first then keyword. Note that an elsif clause can not have a label.
An if...lable is used just to name the if block and label names must be double quoted constant strings having single or multiple words. The label keyword is a case sensitive and should be written as label.
The syntax of label clause is as follows −
if expression label "Label Name" then
   -- Executes when the boolean expression  is true
end if

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20

if (a + b) = 40 label "First IF Block" then
   printf(1, "Value of (a + b ) is  %d\n", a + b )
elsif (a + b) = 45 then
   printf(1, "Value of (a + b ) is  %d\n", a + b )
elsif (a + b) = 30 then
   printf(1, "Value of (a + b ) is  %d\n", a + b )
else
   printf(1, "Value of (a + b ) is  %d\n", 0 )
end if
This produces the following result −
Value of (a + b ) is  30

It is always legal to nest if...else statements. This means you can have one if-else statement within another if-else statements.
The syntax of nested if...else is as follows −
if expression1 then
    -- Executes when the boolean expression1  is true
   if expression2 then
       -- Executes when the boolean expression2  is true  
   end if
end if

#!/home/euphoria-4.0b2/bin/eui

integer a = 10
integer b = 20
integer c = 0

if c = 0 then
   printf(1, "Value of c is equal to %d\n", 0 )
   if (a + b) = 30 then
      printf(1, "Value of (a + b ) is  equal to %d\n", 30)
   else
      printf(1, "Value of (a + b ) is equal to  %d\n", a + b )
   end if
else
   printf(1, "Value of c is equal to %d\n", c )
end if
This produces the following result −
Value of c is equal to 0
Value of (a + b ) is  equal to 30

 Print
 Add Notes
 Bookmark this page