if...elsif...else...endif 语句


if语句_

if语句由一个布尔表达式后跟一个或多个语句组成。

句法

if语句的语法是 -

if expression then
   -- Statements will execute if the expression is true
end if

如果布尔表达式的计算结果为 true,则执行 if 语句内的代码块。如果计算结果为 false,则执行 if 语句结束后的第一组代码。

例子

#!/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 is true if statement!

if ...else语句

if语句后面可以跟一个可选的else语句,该语句在布尔表达式为 false 时执行

句法

if...else语句的语法如下 -

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 is inside if statement!

if ...elsif...else语句

if语句后面可以跟任意数量的可选elsif...else语句,这对于使用单个 if...elsif 语句测试各种条件非常有用。

句法

if...elsif...else语句的语法如下 -

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

这会产生以下结果 -

Value of (a + b ) is  30

if ...label...then语句

if语句可以在第一个then关键字之前有一个标签子句。请注意,elsif子句不能有标签。

if …lable仅用于命名 if 块,并且标签名称必须是具有单个或多个单词的双引号常量字符串。label 关键字区分大小写,应写为label

句法

标签子句的语法如下 -

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

这会产生以下结果 -

Value of (a + b ) is  30

嵌套if...else语句

嵌套if...else语句始终是合法的。这意味着您可以在另一个 if-else 语句中包含一个 if-else 语句。

句法

嵌套if...else的语法如下 -

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

这会产生以下结果 -

Value of c is equal to 0
Value of (a + b ) is  equal to 30
euphoria_branching.htm