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