DEV Community

nadirbasalamah
nadirbasalamah

Posted on

Java Tutorial - 2 Control Flow (selection)

The condition selection is a commonly used in Java to execute a certain code based on the specific condition.

if Statement

To create an if statement, use this syntax.

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

This is the example of if usage to detect a number is an even or odd number.

public class MyApp {
    public static void main(String[] args){
        int num = 24;
        if (num % 2 == 0) {
            System.out.println("Even number");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

This is the output:

Even number

Enter fullscreen mode Exit fullscreen mode

Based on the code above, the variable called num is compared using == operator to check if the result of num % 2 (num mod 2) is equals zero. If it is true, the Even number text is printed out.

This is the list of comparation operator that can be used in Java.

Operator Description
== Equals to
!= Not equals to
> Greater than
>= Greater than or equals
< Less than
<= Less than or equals

The other operators like AND (&&) and OR (||) is available.

The AND (&&) operator is an operator that returns true if the two conditions returns true.

Condition Condition Result
False False False
False True False
True False False
True True True

The OR (||) operator is an operator that returns true if the one or two conditions returns true.

Condition Condition Result
False False False
False True True
True False True
True True True

if else Statement

The if statement can be added with else to execute another code if the condition inside if is not match. Here it is the basic syntax.

if (condition) {
    // code..
} else {
    // code..
}
Enter fullscreen mode Exit fullscreen mode
public class MyApp {
    public static void main(String[] args){
        int num = 91;
        if (num % 2 == 0) {
            System.out.println("Even number");
        } else {
            System.out.println("Odd number");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

The output:

Odd number

Enter fullscreen mode Exit fullscreen mode

Based on that code, the condition inside if statement is not match because the result of 91 % 2 is not equals zero then the code inside else block is executed.

The multiple condition selection is available with if else if statement. Here it is the basic syntax.

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

This is the example of the if else if usage. In this case, the simple program is created to define the grade in letter based on the grade in number.

Condition Result
0-50 D
51-65 C
66-79 B
80-100 A
import java.util.Scanner;

public class MyApp {
    public static void main(String[] args){
        // create an input
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a grade: ");
        // get the grade in number from the input
        int grade = scanner.nextInt();

        // define the grade in letter variable
        char result = 0;

        // condition selection
        if (grade <= 50 && grade >= 0) {
            result = 'D';
        } else if (grade >= 51 && grade <= 65) {
            result = 'C';
        } else if (grade >= 66 && grade <= 79) {
            result = 'B';
        } else if (grade >= 80 && grade <= 100) {
            result = 'A';
        } else {
            System.out.println("Invalid grade");
            // stop the execution using return
            return;
        }

        // print out the result
        System.out.println("The result is: " + result);
    }
}
Enter fullscreen mode Exit fullscreen mode

The output (with the input):

Enter a grade: 77
The result is: B

Enter fullscreen mode Exit fullscreen mode

Based on the code above, the if else if selection is suitable for a range condition.

The if or if else if statement is suitable for a condition that has a specific range.

switch case Statement

Another way to create a condition selection is using switch case. This is the basic syntax.

switch(variable) {
    case condition:
        // code..
        break;
    case condition:
        // code..
        break;
    default:
        // code..
        break;
}
Enter fullscreen mode Exit fullscreen mode

This is the example of using switch case. The program is created to define the topics that needed.

import java.util.Scanner;

public class MyApp {
    public static void main(String[] args){
        // create an input
        Scanner scanner = new Scanner(System.in);

        System.out.print("What do you want to learn? ");
        String input = scanner.nextLine();

        // convert the all letters in input to lowercase
        String learn = input.toLowerCase();

        // condition selection
        switch (learn) {
            case "front end":
                System.out.println("Learn HTML, CSS and JS");
                break;
            case "back end":
                System.out.println("Learn Java, PHP and Go");
                break;
            case "mobile":
                System.out.println("Learn Android and iOS development");
                break;
            default:
                System.out.println("Learn the related topics");
                break;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

The output (with the input).

What do you want to learn? mobile
Learn Android and iOS development

Enter fullscreen mode Exit fullscreen mode

Based on that code, the case keyword is used to define the case or condition. The break keyword is needed to avoid the other code that is not match with the case condition being executed. The default keyword is used to execute the code if the other cases is not match.

the switch case selection is suitable for a specific condition that is not a range condition.

Sources

  • Check out this resource to learn more about condition selection.

I hope this article is helpful for learning the Java programming language. If you have any thoughts or comments you can write in the discussion section below.

Top comments (0)