Skip to main content

Control Flow

Making Logical Decisions in Programming: Comparing Values and Variables

Introduction

In programming, the comparison of values and variables plays a crucial role in making decisions and building logical structures.

To illustrate, imagine a scenario where we have to evaluate whether a student has passed or failed an examination. This evaluation will require comparing the student's achieved score with the designated passing grade.

Using comparison operators like ">=" (which signifies "greater than or equal to"), the program checks if the student's score meets or exceeds the passing threshold. If this condition holds true, the program outputs "true"; otherwise, it will return "false."

For example:

class Main {
    public static void main(String[] args) {
        
        int score = 40;
        int passingScore = 25;

        System.out.println(score >= passingGrade);
    }
}

Output

true

Here, score is 40, which is greater than passingScore 25. Hence, we get true as output.

Comparison Operators

In programming, comparison operators are important tools for decision-making. These operators compare two values or variables and produce results in the form of Boolean outputs, i.e., either true or false. These Boolean values essentially serve as the foundation for conditional statements and logical operations, enabling the program to execute specific actions based on the outcome of these comparisons.

Operator Name Example Remarks
== Equal to x == y compares two values/variables and returns true if they are equal, otherwise, it returns false.
!= Not Equal to x != y compares two values/variables and returns true if they are not equal, otherwise, it returns false.
> Greater than x > y compares two values/variables and returns true if first is greater than second; if not, it returns false.
< Less than x < y compares two values/variables and returns true if first is lesser than second; if not, it returns false.
>= Greater than or equal to x >= y compares two values/variables and returns true if first is greater than or equal to second; if not, it returns false.
<= Less than or equal to x <= y compares two values/variables and returns true if first is lesser than or equal to second; if not, it returns false.

Let's try to understand the above operator with the help of an example-

class Main {
    public static void main(String[] args) {
    
        int num1 = 25;
        int num2 = 18;
    
        // 25 greater than 25 is false
        System.out.println(num1 > 25);
    
        // 25 greater than or equal to 25 is true
        System.out.println(num1 >= 25);

        // 18 greater than or equal to 18 is true
        System.out.println(num2 >= num2);

        // 18 greater than or equal to 18 is true
        System.out.println(num2 >= 18);
        
        // 18 greater than 25 is false
        System.out.println(num2 > num1);
    
        // 18 greater than 17 is true
        System.out.println(num2 > 17);

        System.out.println(num1 >= 25.0);   // true
        System.out.println(25.0 > num1);    // false
 
        System.out.println(24.9 >= num1);   // false
        System.out.println(num1 >= 34.9);   // true
    }
}

Output

false
true
true
true
false
true
true
false
false
true

Practice Problem

Write a program to check if a Number is Positive or Not.

Java Code
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    // take number input
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number:");
    int num = input.nextInt();

    // > operator checks if the number is greater than 0 or not.
    System.out.println(num > 0);
    input.close();

  }
}
Sample Output1
Enter a number:
12
true
Sample Output2
Enter a number:
-1
false

Logical Operators

Now that we have learned about boolean expressions, Logical operators help us combine different conditions in programming to make decisions. For instance, we can use them to check if a number is both greater than 5 and less than 10.

let's explore the three logical operators in Java.

Operator Name Meaning Example
! Not Inverts the result; gives false if the result is true !(num > 0)
|| Or Returns true if atleast one of the statements is true (num1 > 0) || (num2 > 0)
&& And Returns true if both the statements are true (num1 > 0) && (num2 > 0)
class Main {
    public static void main(String[] args) {
 
        int num = 22;
        double gpa = 3.6;
 
        // true because both (num >= 18) and (gpa > 3.5) are true
        System.out.println(num >= 18 && gpa > 3.5); // true
 
        // true because (num >= 18) is true even though (gpa > 3.7) is false
        System.out.println(num >= 18 || gpa > 3.7); // true
 
        // true because (gpa > 3.5) is true even though (num >= 25) is false
        System.out.println(num >= 25 || gpa > 3.5); // true
 
        // true because (num > 21) is true
        System.out.println(!(num > 21)); // false
    }
}

Quiz: