Apex - if else语句
2019-10-26 16:26 更新
if-else语句
if语句后面可以有一个可选的else语句,当布尔表达式为false时执行。
语法:
if boolean_expression {
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}如果布尔表达式的计算结果为true,则将执行if代码块,否则将执行代码块。流程图:

示例:
假设,我们的化工公司有两类客户:高级和正常。 根据客户类型,我们应该提供折扣和其他好处,如售后服务和支持。 下面是这个的实现。
//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块将基于条件执行,在其余情况下,else条件将被触发。以上内容是否对您有帮助:

免费 AI IDE


更多建议: