Apex - if elseif else語句

2019-10-26 16:26 更新

if語句后面可以跟一個(gè)可選的else if ... else語句,這對于使用single if ... else if語句測試各種條件非常有用。


語法:

if boolean_expression_1 {
   /* Executes when the boolean expression 1 is true */
} else if boolean_expression_2 {
   /* Executes when the boolean expression 2 is true */
} else if boolean_expression_3 {
   /* Executes when the boolean expression 3 is true */
} else {
   /* Executes when the none of the above condition is true */
}

例如:

假設(shè),我們的化工公司有兩類客戶:高級和正常。 根據(jù)客戶類型,我們應(yīng)該提供折扣和其他好處,如售后服務(wù)和支持。 下面是這個(gè)的實(shí)現(xiàn)。

//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');
}
else if (customerName == 'Joe') {
    discountRate = 0.5; //when condition is met this block will be executed
    premiumSupport = false;
    System.debug('Special Discount not given as Customer is not Premium');
}
else {
    discountRate = 0.05; //when condition is not met and customer is normal
    premiumSupport = false;
    System.debug('Special Discount not given as Customer is not Premium');
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號