Salesforce If else Statements

If else Statements

If else statements are used to control the conditional statement that are based on various conditions. It is used to execute the statement code block if the expression is true. Otherwise, it executes else statement code block.

Salesforce If else Statements

Syntax:

if (Boolean_condition)
//Statement1
else
//statement 2

Example: Check the condition for voting

integer age=20; 
if(age>=18)    //checks the condition
{
    System.debug('ELIGIBLE FOR VOTING');
}
else
{
    System.debug('NOT ELIGIBLE');
}

Output:

Salesforce If else Statements

In the above example, the conditional statement returns true because it matches the given condition.

Example:

integer marks=28;
if(marks>=33)
{
    System.debug('PASS');
}
else
{
    System.debug('FAIL');
}

Output:

Salesforce If else Statements

In the above example, the conditional statement returns false because it doesn`t match the given condition.

Example: Comparing two different ages        

integer age1=22;    //my age
integer age2=20;   //my brother`s age
if(age1>age2)
{
    System.debug('I am elder');
   }
else
{
    System.debug('My brother is elder');
}

Output:

Salesforce If else Statements

If else if Statement

Syntax:

if (expression1)
{
//statement
else if(expression2)
{
//statement
}
else if(expression3)
{
//statement
}
..
..
..
else
{
//statement
}                

Example: Positive Number

integer num=10;
if(num>0)
{
    System.debug('Number is positive');
}
else if(num<0)
{
    System.debug('Number is negative');
}
else
{
    System.debug('Number is Zero');
}

Output:

Salesforce If else Statements

Example: Negative Number

integer num = (-50);
if(num>0)
{
    System.debug('Number is positive');
   }
else if(num<0)
{
    System.debug('Number is negative');
}
else
{
    System.debug('Number is Zero');
}

Output:

Salesforce If else Statements

Example: Zero

integer num=(0);
if(num>0)
{
    System.debug('Number is positive');
}
else if(num<0)
{
    System.debug('Number is negative');
}
else
{
    System.debug('Number is Zero');
}

Output:

Salesforce If else Statements

Example:

integer place=1;
String medal_color;
if(place ==1)
{
    medal_color='gold';
}   
else if (place==2)
{
    medal_color='silver';   
}
else if(place==3)
{
    medal_color='bronze';
}
else
{
   medal_color='null';
}
System.debug('You have scored '+medal_color + 'Medal. Congratulations');

Output:

Salesforce If else Statements

Example:

integer top_score=99,second_score=80,your_score=85;  
if(your_score>top_score)
{
    System.debug('You have scored the highest marks');
}
else if((your_score<top_score)&& (your_score>second_score))
{
    System.debug('You have scored greater than the second score');
}
else if((your_score<top_score)&&(your_score<second_score)&&(your_score>65))
{
    System.debug('You can do better');
}  
else
{
    System.debug('Work hard');
}

Output:

Salesforce If else Statements

In the above example, we have created three integer variable top_score, second_score, your_score, and initialized them with 99, 80, 85 values.

In first if block, we are checking if your_score is greater than top_score i.e., 85>99, so the first if block will not get executed because the condition is false.

In the next else block, if your_score is less than top_score i.e., 99<85 (this condition is true) and the second condition is your_score is greater than second_score i.e., 85>80 (this condition is true).

There is a && operator. In the && operator, both the condition needs to be true. So, this block will get executed because both these conditions are true.

Once any of the blocks gets executed, the rest of the conditions will be skipped.

Example:

integer salary=250000;
Decimal bonus,salary_after_bonus;
if(salary>=200000)
{
    bonus=15;
}
else if(salary>=150000)
{
    bonus=12;
}
else if(salary>=10000)
{
    bonus=10;
}
else
{
    bonus=5;
}
salary_after_bonus=salary+(salary*(bonus/100));
System.debug('Salary '+salary);
System.debug('Bonus '+bonus+ '%');
System.debug('Salary after bonus'+ salary_after_bonus);

Output:

Salesforce If else Statements