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:

Example: Check the condition for voting

Output:

Salesforce If else Statements

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

Example:

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        

Output:

Salesforce If else Statements

If else if Statement

Syntax:

Example: Positive Number

Output:

Salesforce If else Statements

Example: Negative Number

Output:

Salesforce If else Statements

Example: Zero

Output:

Salesforce If else Statements

Example:

Output:

Salesforce If else Statements

Example:

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:

Output:

Salesforce If else Statements

Pin It on Pinterest

Share This