Java switch statement

Conditional branching using Java

If you use an if statement


class Main {
  public static void main(String[] args) {
    int number = 12;

  if(number % 3 == 0){
        System.out.println("Dividable by 3");
  else if(number % 3 == 1){
        System.out.println("Divide by 3 to get 1 surplus");
  else
        System.out.println("Divide by 3 to get 2");
  }

 }
}

With a switch statement

class Main {
  public static void main(String[] args) {
    int number = 12;
    switch(number % 3){
      case 0:
        System.out.println("Dividable by 3");
        break;
      case 1:
        System.out.println("Divide by 3 to get 1 surplus");
        break;
      case 2:
        System.out.println("Divide by 3 to get 2");
        break;
    }
    
  }
}

Recommended Posts

Java switch statement
switch statement
[Java] Branch enum with switch statement
[Java] Really scary switch statement story
Java for statement
Basics of java basics ② ~ if statement and switch statement ~
[Java] for statement, while statement
Java instruction execution statement
[Java] for statement / extended for statement
Switch java with direnv
(Memo) Java for statement
Studying Java-Part 11-switch statement
Java switch statement and break, Kotlin when expression ...
[Swift] switch statement using tuples
[Java] Basic statement for beginners
Java if and switch statements
Java programming basics practice-for statement
Java
Java programming basics practice-switch statement
Let's understand the switch statement!
Seasonal display with Java switch
Java
New grammar for Java 12 Switch statements
Java learning memo (while statement, do-while statement)
Java study # 4 (conditional branching / if statement)
I met a great switch statement
Java learning (0)
[Java] array
Java protected
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
[Introduction to Java] Conditional branching (if statement, if-else statement, else if statement, ternary operator, switch statement)
Java methods
Java method
Java array
[Java] ArrayDeque
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review