目录

if statement

if语句

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

语法 (Syntax)

if语句的语法是 -

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

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

例子 (Example)

#!/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时执行。

语法 (Syntax)

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

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

例子 (Example)

#!/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语句测试各种条件非常有用。

语法 (Syntax)

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

例子 (Example)

#!/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语句可以在first then关键字之前有一个label子句。 请注意, elsif子句不能有标签。

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

语法 (Syntax)

label子句的语法如下 -

if expression label "Label Name" then
   -- Executes when the boolean expression  is true
end if

例子 (Example)

#!/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语句。

语法 (Syntax)

嵌套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

例子 (Example)

#!/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
↑回到顶部↑
WIKI教程 @2018