Flow control

if then else instruction

Description

The if then else instruction is used to determine which instruction or set of instructions will be executed, depending on one or more conditions.

A condition is specified trough an expression of type yes_no.

The if then else instruction can be used in different ways, as shown in the following table:

Table 8.8. if then else variations

if thenOne set of instructions is executed if one condition is fulfilled.
if then elseOne set of instructions is executed if one condition is fulfilled and another set of instructions is executed if the same condition is not fulfilled.
if then elseif elseSeveral conditions are specified, and the first condition that is fulfilled determines which set of instructions is executed. If no condition is fulfilled then the optional else clause can be used to specify another set of instructions that will be executed in that case.

[Note]Note
The if then else instruction in Obix is very similar to the if then else statement in Java, C#, and other programming languages. It is a fundamental flow control instruction (statement) that allows executing instructions conditionally.

Syntax

Table 8.9. if then else syntax

ProductionSyntaxLinks
if_then_else_instruction

"if" expression "then"
   script_instruction *
( "else" "if" expression "then"
   script_instruction * ) *
( "else"
   script_instruction * ) ?
"end" "if" ?

the section called “if then else instruction”

Examples

Example 8.6. if then else examples

service instruction_examples

   command if_then_else_examples
      script
      
         // ask user to enter a, b or c on the system console
         const string user_input = se_console.co_ask_string ( "Please enter a, b or c: " )
         
         // 'if then' variation (simplest form)
         if user_input =v "a" then
            se_console.co_message ( "You typed a." )
         end if

         // 'if then else' variation
         if user_input =v "a" then
            console.message ( "You typed a." )
         else
            console.message ( "You didn't type a." )
            // nested if
            if user_input =v "A" then
               console.message ( "But you typed A." )
            end if
         end if

         // 'if then elseif else' variation
         if user_input =v "a" then
            console.message ( "You typed a." )
         else if user_input =v "b" then
            console.message ( "You typed b." )
         else if user_input =v "c" then
            console.message ( "You typed c." )
         else
            console.message ( "You didn't type a, b or c." )
            console.message ( "Shame on you!" )
         end if
         
         // alternative to achieve the same result:
         if user_input =v "a" or user_input =v "b" or user_input =v "c" then
            console.message ( "You typed " & user_input & "." )
         else
            console.message ( "You didn't type a, b or c." )
            console.message ( "Shame on you!" )
         end if

      end script
   end command
   
end service