位置:首頁 > 高級語言 > 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