×
Apex 编程教程Apex 环境Apex 示例Apex 数据类型Apex 变量Apex 字符串Apex 数组Apex 常量Apex 决策Apex 循环Apex 集合Apex 类Apex 类方法Apex 对象Apex 接口Apex DMLApex 数据库方法Apex SOSLApex SOQLApex 安全性Apex 调用Apex 触发器Apex 触发设计模式Governer Limits调节器限制Apex 批量处理Apex 调试Apex 测试Apex 部署Apex 字符串Apex 数组

Apex if elseif else语句


Apex 决策Apex 决策


if语句后面可以跟一个可选的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 */
}

例如:

假设,我们的化工公司有两类客户:高级和正常。 根据客户类型,我们应该提供折扣和其他好处,如售后服务和支持。 下面是这个的实现。

//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 决策Apex 决策


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)