if語句后面可以有一個可選的else語句,當布爾表達式為false時執(zhí)行。
語法:
if boolean_expression { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ }如果布爾表達式的計算結(jié)果為true,則將執(zhí)行if代碼塊,否則將執(zhí)行代碼塊。
流程圖:
//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 { 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'); }由于“Glenmarkone”是一個高級客戶,所以if塊將基于條件執(zhí)行,在其余情況下,else條件將被觸發(fā)。
更多建議: