位置:首页 > 高级语言 > Euphoria在线教程 > Euphoria if...elsif...else...endif 语句

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

if 语句:

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

语法:

if语句的语法是:

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

if 布尔表达式的值为true,那么里面的代码块,if语句会被执行。如果不是第一组结束后的代码,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  子句不能有一个label。

if label块和标签名称必须用双引号的字符串常量,有单个或多个字。label关键字是区分大小写的,应该写成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