Table of Contents
Introduction
IF is a conditional statement that controls the flow of our program. It can give us TRUE or FALSE as an output.
If Statements In Salesforce
IF statements work in Salesforce in the same way as in Java or any other programming language. It is used to evaluate the criteria.
If the condition is true, it will execute the code written in that block and if it’s not true, it will go to the next else if block and execute the code when else if the condition is true. If none of the Boolean conditions are true, it will execute the block of code written in else.
Syntax
public class IfExample{ public static void ifStateExam(){ if(boolean_condition){ //code //when first condition is true }else if(boolean_condition){ //when above condition is true }else{ //when none of the condition is true } } }
Example
1. String day = ‘Tuesday’;
if(day == 'Monday' || day == 'Tuesday' || day == 'Wednesday' || day == 'Thursday' || day == 'Friday'){ System.debug('Office Time'); }else{ System.debug('Party Time'); }
OUTPUT: Office Time
2. String today = ‘Saturday’;
if(today == 'Monday' || today == 'Tuesday' || today == 'Wednesday' ||today == 'Thursday' || today == 'Friday'){ System.debug('Office Time'); }else if (today == ‘Saturday’){ System.debug('Go for movie'); }else{ System.debug('Go for party'); }
OUTPUT: Go for movie
3. Double total = 95.5;
if(total >= 90){ System.debug('You got A grade'); }else if(total >= 70 && total < 90){ System.debug('You got B grade'); }else if(total >= 60 && total < 70){ System.debug('You got C grade'); }else if(total >= 50 && total < 60){ System.debug('You got D grade'); }else{ System.debug('You got E grade'); }
OUTPUT: You got A grade
0 Comments