Apex - if 語句



一個 if 語句包括一個布林表示式,後面跟著一個或多個語句。

語法

if boolean_expression {
   /* statement(s) will execute if the boolean expression is true */
}

如果布林表示式計算結果為真,則 if 語句內的程式碼塊將被執行。如果布林表示式計算結果為假,則 if 語句結束後(在大括號後)的第一組程式碼將被執行。

流程圖

if statement

示例

假設我們的化工公司有兩個類別的客戶——高階和普通。應根據客戶型別,為他們提供折扣及其他福利,例如售後服務和支援。以下是對此的實現方法。

//Execute this code in Developer Console and see the Output
String customerName = 'Glenmarkone'; //premium customer
Decimal discountRate = 0;
Boolean premiumSupport = false;

if (customerName == 'Glenmarkone') {
   discountRate = 0.1; //when condition is met this block will be executed
   premiumSupport = true;
   System.debug('Special Discount given as Customer is Premium');
}

由於“Glenmarkone”是高階客戶,因此將基於該條件執行 if 塊。

apex_decision_making.htm
廣告