课程表

Apex课程

工具箱
速查手册

Apex - if elseif else语句

当前位置:免费教程 » 程序设计 » Apex

if语句后面可以跟一个可选的else if ... else语句,这对于使用single if ... else if语句测试各种条件非常有用。


语法:

  1. if boolean_expression_1 {
  2. /* Executes when the boolean expression 1 is true */
  3. } else if boolean_expression_2 {
  4. /* Executes when the boolean expression 2 is true */
  5. } else if boolean_expression_3 {
  6. /* Executes when the boolean expression 3 is true */
  7. } else {
  8. /* Executes when the none of the above condition is true */
  9. }

例如:

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

  1. //Execute this code in Developer Console and see the Output
  2. String customerName = 'Glenmarkone'; //premium customer
  3. Decimal discountRate = 0;
  4. Boolean premiumSupport = false;
  5. if (customerName == 'Glenmarkone') {
  6. discountRate = 0.1; //when condition is met this block will be executed
  7. premiumSupport = true;
  8. System.debug('Special Discount given as Customer is Premium');
  9. }
  10. else if (customerName == 'Joe') {
  11. discountRate = 0.5; //when condition is met this block will be executed
  12. premiumSupport = false;
  13. System.debug('Special Discount not given as Customer is not Premium');
  14. }
  15. else {
  16. discountRate = 0.05; //when condition is not met and customer is normal
  17. premiumSupport = false;
  18. System.debug('Special Discount not given as Customer is not Premium');
  19. }

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号