DEV Community

Cover image for Java Programming Language (Conditions)
Nihal Islam
Nihal Islam

Posted on • Edited on

Java Programming Language (Conditions)

Conditions

Conditions are used in code to determine whether a specific block of code should execute or be skipped.

if statement

if (condition) {...}
Enter fullscreen mode Exit fullscreen mode

if statement takes a boolean value as condition. If the value is true java executes the code between the block {...}. Otherwise skip it. For example,

public class Conditions {
    public static void main(String[] args) {
        if (true) {
            System.out.println("Inside if statement.");
        }
        System.out.println("Outside if statement.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Inside if statement.
Outside if statement.
Enter fullscreen mode Exit fullscreen mode

However, if the condition is false, the line between the blocks will be skipped.

if (false) {
    System.out.println("Inside if statement.");
}
System.out.println("Outside if statement.");
Enter fullscreen mode Exit fullscreen mode

Output

Outside if statement.
Enter fullscreen mode Exit fullscreen mode

Now here I hardcoded the true / false conditions. We can also use relational and logical operators as conditions. For example, I want to print

if a student gets above 89, the GPA will be 4.00
if a student gets between 85 to 89, the GPA will be 3.70

The statement will be like this,

public class Conditions {
    public static void main(String[] args) {
        int number = 92;
        // Use of Relational Operator
        if (number > 89) {
            System.out.println("GPA: 4.00");
        }
        // Use of Logical Operator
        if (number >= 85 && number <= 89) {
            System.out.println("GPA: 3.70");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

GPA: 4.00
Enter fullscreen mode Exit fullscreen mode

else if statement

else if (condition) {...}
Enter fullscreen mode Exit fullscreen mode

Suppose I want to print the grades like this,

int number = 92;
if (number > 89) {
    System.out.println("GPA: 4.00");
}
if (number > 84) {
    System.out.println("GPA: 3.70");
}
Enter fullscreen mode Exit fullscreen mode

Now there's a bug. Here the problem is that, the code is checking both of the conditions and both of them are true. So the output will execute both the blocks of codes.

Output

GPA: 4.00
GPA: 3.70
Enter fullscreen mode Exit fullscreen mode

But we don't want that as nobody gets two grades. Instead what we can do is we can use else if condition. When the code sees an if statement it checks whether the condition is true or false. If the condition is false only then the code will check the else if condition. If the if condition is true, then all the other else if conditions will be skipped.

int number = 92;
if (number > 89) {
    System.out.println("GPA: 4.00");
} else if (number > 84) {
    System.out.println("GPA: 3.70");
}
Enter fullscreen mode Exit fullscreen mode

Output

GPA: 4.00
Enter fullscreen mode Exit fullscreen mode

We can use as many else if statements as we want according to our needs.

else statement

else {...}
Enter fullscreen mode Exit fullscreen mode

The else statement will execute the block of code only when all the above if and else if conditions are false. else statement does not have a condition block.

int number = 80;
if (number > 89) {
    System.out.println("GPA: 4.00");
} else if (number > 84) {
    System.out.println("GPA: 3.70");
} else {
    System.out.println("You need to work hard");
}
Enter fullscreen mode Exit fullscreen mode

Output

You need to work hard
Enter fullscreen mode Exit fullscreen mode

First comes the if statement, then the else if statements if needed and then lastly else statement if needed.

Nested Conditions

You can write nested conditions as well. Suppose I want to figure out if I got A+ or A after getting CGPA 4.00.

int number = 98;
if (number > 89) {
    System.out.print("GPA: 4.00 ");
    //Nested Conditions inside the if block
    if (number > 96) {
        System.out.println("Grade: A+");
    } else {
        System.out.println("Grade: A");
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

CGPA: 4.00 Grade: A+
Enter fullscreen mode Exit fullscreen mode

We can write as many nested conditions as needed. Try to figure out if it's possible to write nested conditions inside else if and else statements as well.

switch statement

Java switch statement is similar to if...else statements. We can write switch statement in the following way,

switch (expression) {
    case value1:
        // code
        break;
    case value2:
        //code
        break;

        ...
        ...
    default:
        //code
}
Enter fullscreen mode Exit fullscreen mode

if...else statement checks whether the condition is true or false in the condition block. However, switch statement tries to match the expression from the (expression) block with the values from the case statements. If the value matches, the code of that case will be executed. And if none of the value matches the expression, then the code will execute the default value at the end. For example,

public class Conditions {
    public static void main(String[] args) {
        int number = 8;
        switch (number) {
            case 7:
                System.out.println("Extra small");
                break;
            case 8:
                System.out.println("Small");
                break;
            case 10:
                System.out.println("Large");
                break;
            case 11:
                System.out.println("Extra large");
                break;
            default:
                System.out.println("Medium");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Small
Enter fullscreen mode Exit fullscreen mode

Here the term break breaks the switch statement. Which means if the expression matches a case, then it will execute the code of that particular case and skip the rest of the cases. If I don't use the term break after every case, then the rest of the code will execute from the matching state. For example,

public class Conditions {
    public static void main(String[] args) {
        int number = 10;
        switch (number) {
            case 7:
                System.out.println("Extra small");
            case 8:
                System.out.println("Small");
            case 10:
                System.out.println("Large");
            case 11:
                System.out.println("Extra large");
            default:
                System.out.println("Medium");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Large
Extra large
medium
Enter fullscreen mode Exit fullscreen mode

Appendix

  • When there's an error or our desired output does not match the generated output because of some particular code, it's code bug. We can trace our code from beginning to find where the problem occurs and debug our code.
  • The white space between the curly braces is called code block. {...}. We use code block after classes, methods, statements, loops etc to execute code for that particular term.

Happy Coding

Top comments (0)