Return to main page
View source or report issues on GitHub

Lesson 1.3 - If statements and logical operators

Many methods that you write in programming will need to make some sort of decision where they will do one thing in some situations, and a different thing in other situations.

In programming, we make decisions using an if statement:star: also known as a conditional statement:star:. Here is an example of how we can use an if statement in java to choose between several different options.

1
2
3
4
5
6
7
8
/** Determines whether a student failed a test based on marks out of 100 */
public boolean didStudentFail(int marks) {
    if (marks < 55) {
        return true;
    } else {
        return false;
    }
}

The command return:star: in this code represents the ANSWER that the method provides. Note the syntax - the parentheses (), curly brackets {}, and semi-colons ; are all important to the correct functioning of this code.

if statements always follow the same format:

1
2
3
4
5
if (/*test that results in true or false*/) {
    //code that only runs when the test is true
} else {
    //code that runs if the test is false (optional!)
}

You can stack multiple mutually exclusive if statements like so:

1
2
3
4
5
6
7
8
9
10
11
12
/** returns A, B, C, or F based on marks out of 100*/
public String getGrade(int marks) {
    if (marks < 55) {
        return "F";
    } else if (marks < 70) {
        return "C";
    } else if (marks < 85) {
        return "B";
    } else {
        return "A";
    }
}

Quiz Yourself about this method

  1. What is the name of this method?

    Click to expand answer

    getGrade

  2. What are the String literals in this code?

    Click to expand answer

    “A”, “B”, “C”, and “F”.

  3. If the student earns 82 marks, what will their grade be?

    Click to expand answer

    “B”

Logical Operators

An operator:star: is a symbol that is used in programming to evaluate something - for example, + is an operator that can be used to add two numbers together. A logical operator:star: is an operator that results in true or false. You have already seen this several times above, using the < and > operators. Here are all of the logical operators:

Operator Code Sample

Less Than (<) and Greater Than (>)

1
2
3
int a = 2;
a < 3; //true
a > 8; //false

Less Than or Equal (<=) and Greater Than or equal (>=)

1
2
3
4
int a = 2;
a <= 2; //true
a <= 3; //true
a >= 8; //false

Equals (==) (two equals signs!)

1
2
3
int a = 2;
a == 2; //true
a == 8; //false

Not Equal to (!=)

1
2
3
int a = 2;
a != 2; //false
a != 8; //true

And (&&) Used to check if two boolean statements are both true

1
2
3
int a = 2;
int b = 3;
b > 2 && a < 5; // false because the first is false

Or (||) Used to check if either (or both) boolean statements are true

1
2
3
int a = 2;
int b = 3;
b > 2 || a < 5; // true because the second is true

Not (!) Used to reverse the value of a boolean statement

1
2
3
int a = 2;
int b = 3;
! (b > 2); // true because b > 2 is false

Using boolean variables in if statements

Often, you will have a variable or parameter that represents a true or false value and you will want to test that value with an if statement. In those situtations it is best to use the variables DIRECTLY in the if statement, without using ==, as shown below. It works to do something like if (isSunny == true) but it is extra work and can lead to errors that are hard to find if you forget an = sign.

1
2
3
4
5
6
7
8
9
10
// the following method will return true if it is sunny OR if it is not a workday
public boolean inAGoodMood(boolean isSunny, boolean isWorkday) {
    if (isSunny) { // if it is sunny this will be true
        return true;
    } else if (! isWorkday) { // the ! is "not" so this means if it is not a workday
        return true;
    } else {
        return false;
    }
}

Quiz Yourself

  1. Write a method called sumOrZero (including the full signature!) that will take two integers as parameters, and return their sum, UNLESS the two integers are the same, in which case it will return 0. Note that you can use the test a == b to check if two variables a and b are equal.

    Click to expand answer
    1
    2
    3
    4
    5
    6
    7
    
    public int sumOrZero(int a, int b) {
        if (a == b) {
            return 0;
        }else {
            return a + b;
        }
    }
    
  2. After the code below has finished running, what will be the value of b1, b2, and b3?

    1
    2
    3
    4
    5
    
    int a = 17;
    int b = 12;
    boolean b1 = (a <= 17);
    boolean b2 = (b > 12);
    boolean b3 = b1 || b2;
    
    Click to expand answer

    b1 is true, b2 is false, b3 is true

  3. After the code below has finished running, what will be the value of b1, b2, and b3?

    1
    2
    3
    4
    5
    
    int a = 17;
    int b = 12;
    boolean b1 = (a == 17);
    boolean b2 = ! (b > 12);
    boolean b3 = b1 && b2;
    
    Click to expand answer

    b1 is true, b2 is true, b3 is true