How do I use the switch statement?

The program below demonstrate how to use the switch statement. The switch statement can work with the byte, short, int and char primitive data types and the corresponding wrappers of these data type such as Byte, Short, Integer and Character. It also work with enumerated types, refer to the following example: How do I use enum in switch statement?.

The switch block or the body can contains one or more case or default labels. The switch statement evaluates its expression and evaluate the appropriate case.

You’ll also notice that after each case labels we have a break statement. This break statement causes the program execution to continue outside the switch block. Without using a break the case will fall-through to another case or default label.

package org.kodejava.basic;

import java.util.Scanner;

public class SwitchDemo {
    public static void main(String[] args) {
        System.out.println("The Planets");
        System.out.println("===================================");
        System.out.println("1. Mercury");
        System.out.println("2. Venus");
        System.out.println("3. Earth");
        System.out.println("4. Mars");
        System.out.println("5. Jupiter");
        System.out.println("6. Saturn");
        System.out.println("7. Uranus");
        System.out.println("8. Neptune");
        System.out.println();
        System.out.print("Please choose your favorite destination: ");

        Scanner scanner = new Scanner(System.in);
        int destination = scanner.nextInt();

        System.out.print("Welcome to ");
        switch (destination) {
            case 1:
                System.out.println("Mercury");
                break;
            case 2:
                System.out.println("Venus");
                break;
            case 3:
                System.out.println("Earth");
                break;
            case 4:
                System.out.println("Mars");
                break;
            case 5:
                System.out.println("Jupiter");
                break;
            case 6:
                System.out.println("Saturn");
                break;
            case 7:
                System.out.println("Uranus");
                break;
            case 8:
                System.out.println("Neptune");
                break;
            default:
                System.out.println("Invalid Destination");
        }
    }
}

When you run the program you’ll have to following on the screen:

The Planets
===================================
1. Mercury
2. Venus
3. Earth
4. Mars
5. Jupiter
6. Saturn
7. Uranus
8. Neptune

Please choose your favorite destination: 3
Welcome to Earth

How do I use the if-then-else statement?

The if-then-else control flow statement adds a secondary path to the if statement when the expression evaluates to false. When it evaluates to false the else block will be executed.

Below is the program that takes input of user test score and evaluates the score to get the corresponding grade.

package org.kodejava.basic;

import java.io.Console;

public class IfThenElseDemo {
    public static void main(String[] args) {
        System.out.print("Please enter your score: ");

        try {
            // Get an instance of system console for taking user
            // input.
            Console console = System.console();

            // Take user score input and convert the input value into
            // number.
            int score = Integer.parseInt(console.readLine());

            String grade;
            if (score >= 90) {
                grade = "A";
            } else if (score >= 80) {
                grade = "B";
            } else if (score >= 60) {
                grade = "C";
            } else if (score >= 50) {
                grade = "D";
            } else {
                grade = "F";
            }

            System.out.println("Grade = " + grade);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}

When the program executed you’ll need to input the test score and the program will give you the grade.

Please enter your score: 75
Grade = C

How do I use the if-then statement?

The if-then is the most basic control flow statement. It will execute the block of statement only if the test expression evaluated equals to true. Let’s see the example below:

package org.kodejava.basic;

public class IfThenDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;

        // If the evaluation of a > b is true, the if block will be
        // executed. In the block below we just print that the value
        // of "a" is bigger than "b".
        if (a > b) {
            System.out.printf("A(%d) is bigger than B(%d)%n", a, b);
        }

        // When we have only a single command inside a block we can
        // remove the opening and closing braces for the block ({..}).
        // But it is a good practice to always enclose the block with
        // braces, so that we know the block of code to be executed.
        if (b < a)
            System.out.printf("B(%d) is smaller that A(%d)%n", b, a);
    }
}

The result of the above program is:

A(10) is bigger than B(5)
B(5) is smaller that A(10)