Search Here

Apex - if else Statement

 


Apex - if statement

Suppose, our Chemical company has customers of two categories – Premium and Normal. Based on the customer type, we should provide them discount and other benefits like after sales service and support. Following is an implementation of this.


//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');

}


nested if statement

As 'Glenmarkone' is a premium customer so the if block will be executed based on the condition.

We can also have the nested if-else statement for complex condition as given below -


String pinCode = '12345';

String customerType = 'Premium';

if (pinCode == '12345') {

   System.debug('Condition met and Pin Code is'+pinCode);

   if(customerType = 'Premium') {

   System.debug('This is a Premium customer living in pinCode 12345');

   }else if(customerType = 'Normal') {

     System.debug('This is a Normal customer living in pinCode 12345');

   }

}else {

   //this can go on as per the requirement

   System.debug('Pincode not found');

}


Apex - if elseif else statement

public class ConstantsExample {

    // Define a constant for the maximum score

    public final Integer MAX_SCORE = 100;


    public void displayScore(Integer score) {

        if (score > MAX_SCORE) {

            System.debug('Invalid score: exceeds the maximum allowed score.');

        } else {

            System.debug('Score is valid: ' + score);

        }

    }


    public static void main() {

        ConstantsExample example = new ConstantsExample();

        Integer userScore = 95;


        System.debug('Maximum Allowed Score: ' + example.MAX_SCORE);

        example.displayScore(userScore);

    }

}

We create a class ConstantsExample to encapsulate our constant and a method displayScore to demonstrate its usage.

Inside the class, we define a constant MAX_SCORE with the final keyword, and we set it to 100. This means that MAX_SCORE cannot be modified after it is assigned a value.

The displayScore method checks if a user's score exceeds the maximum allowed score (the constant) and provides feedback accordingly.

In the main method, we create an instance of the ConstantsExample class and display the maximum allowed score and a user's score using the displayScore method.


if...else statement


In Apex, you can use conditional statements like if, else if, and else to control the flow of your code based on specific conditions. 


public class IfElseExample {

    public static void main() {

        Integer number = 42;


        if (number > 50) {

            System.debug('The number is greater than 50.');

        } else if (number < 50) {

            System.debug('The number is less than 50.');

        } else {

            System.debug('The number is equal to 50.');

        }

    }

}

We create a class called IfElseExample and a static main method.


Inside the main method, we declare an integer variable number with a value of 42.


We use the if, else if, and else statements to check the value of number against multiple conditions.


If number is greater than 50, the code inside the first if block is executed, and it prints "The number is greater than 50."


If number is less than 50, the code inside the else if block is executed, and it prints "The number is less than 50."


If neither of the previous conditions is met, the code inside the else block is executed, and it prints "The number is equal to 50."




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.