4.1 Using if and else to make decisions

The concept

We have talked a little about if/else condition statements before. Today, let’s have a closer look at it. Create a new class named Kid.

public class Kid 
    private String name;
    private Boolean isMyKid;
    public Kid(String name, Boolean isMyKid)
        this.name = name;
        this.isMyKid = isMyKid;
    public void treatKid()
            System.debug('Take ' + name + ' to Disneyland.');
            System.debug('Give ' + name + ' some candy.');

You can create instances and try with treatKid() methods by yourself.

A bit explanation

if/else statement is pretty straight forward in the grammar and concept. In the parenthesis of if statement is the boolean expression. If the value is true, it will execute the if section. Otherwise, the else part.

But please keep in mind this is the first time your programme doesn’t execute in the one-line sequence from top to bottom. It can choose dynamically which statement to run and which not. That’s why if statement is so useful and powerful. We will cover more examples in the next few posts.


Add an additional variable in the Kid class called age. Write a function to tell whether the Kid is okay to take a beer or not.

Next Post

4.2 More branches with if and else

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *