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



if 语句

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

语法

if语句的语法如下:

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

如果布尔表达式计算结果为真,则执行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语句,当布尔表达式为假时执行。

语法

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
广告