Switch statement



/**
    Keyboard input a number 0, 1 , 2, 3
    0 : Output The airplane moves up
    1 : Output The airplane  moves down
    2 : Output The airplane moves to the left
    3:  Output The airplane moves to the right
        Otherwise the output does not move
*/
import java.util.Scanner;

public class SwitchStatement {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Keyboard enters a number 0: up, 1: down, 2: left, 3: right");
        int num = in.nextInt();

        //switch statement
        switch (num) {
        case 0:
            System.out.println("Airplane moves up");
            break; // terminate the code to continue execution
        case 1:
            System.out.println("Airplane moves down");
            break;
        case 2:
            System.out.println("Airplane moves to the left");
            break;
        case 3:
            System.out.println("Airplane moves to the right");
            break;
        default:
            System.out.println("Airplane does not move");
        }
    }
}
/**
    Summary:
    1. default:         is the default execution when there is no matching condition
    2. break:        the switch is terminated. If there is no break statement, the program will continue to execute.
    3. The switch (expression) expression can only be a byte, short, int, char type constant
        String jdk1.6 version is not working, but after jdk1.7 version String can be used in switch
*/



Result:

Keyboard enters a number 0: up, 1: down, 2: left, 3: right
2
Airplane moves to the left



Easy Learning Java


Easy Learning Design Patterns Java Practice


Easy Learning Data Structures & Algorithms Java Practice


Java Parsing Collection XML JSON


Easy Learning JDBC + MySQL


Easy Learning Javascript


Easy Learning Design Patterns Javascript


Easy Learning Design Patterns ES6+ Javascript


Easy Learning Data Structures & Algorithms Javascript


Easy Learning Data Structures & Algorithms ES6+Javascript


Easy Learning Oracle SQL


Easy Learning JDBC + Oracle


Easy Learning Python 3


Easy Learning Data Structures & Algorithms Python 3


Easy Learning Design Patterns Python 3


Easy Learning HTML CSS


Easy Learning MySQL SQL


Easy Learning C#


Easy Learning Data Structures & Algorithms C#


Easy Learning C


Easy Learning Data Structures & Algorithms C

Switch statement