Apex - if elseif else 语句
if 语句后面可以跟一个可选的 else if...else 语句,这对于使用单个 if.. 测试各种条件非常有用。 .else if 语句。
语法
if...else if...else 语句的语法如下 −
if boolean_expression_1 {
/* 当布尔表达式 1 为 true 时执行 */
} else if boolean_expression_2 {
/* 当布尔表达式 2 为 true 时执行 */
} else if boolean_expression_3 {
/* 当布尔表达式 3 为 true 时执行 */
} else {
/* 当以上条件都不成立时执行 */
}
示例
假设我们的公司有两类客户:高级客户和普通客户。 根据客户类型,我们应该为他们提供折扣和其他福利,例如售后服务和支持。 以下程序显示了相同的实现。
//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');
}

apex_decision_making.html