HomeContact

How to use Switch statements in Java

Published in Java
May 10, 2023
1 min read

Switch statements are used in the following format.

switch(expression) {
case x:
Code to execute if expression is x
break;
case y:
Code to execute if expression is y
break;
default:
Code to execute when the expression is neither x nor y
}

🌱Sample code

public static void main(String[] args) {
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
}

Tags

#Java#java.lang

Share


Previous Article
How to use While Loop in Java

Topics

Java
Other
Server

Related Posts

How to use BigInteger in Java - 2
May 31, 2023
1 min