TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Java switch statement

Overview

Java switch statement is a control structure and is used to when we need to make multiple decisions in the Java program. We can replace if-else-if ladder with switch statements.

Syntax

switch(){

case match1:

//code statements

break; // this is optional

case match2:

//code statements

break; // this is optiona;

…

default :

// code statements when no match

}

 

The break statement in the case block is optional. The purpose of the break statement is to execute only one case block. Upon break statement, the control jumps to the statement after the switch statement. When we omit the break statement in the case block, the next case statement is executed.

Java switch statement

switch statement can have a number of possible execution paths. Use switch to specify many alternative blocks of code to be executed.

Example:

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

 

 

Switch Case Demo JavaWin10

The output of the program is:

Tuesday

No break statements

Now, let’s omit the break statements in the case blocks an see what happens to the output.

//Java Tutorials - www.TestingDocs.com

public class SwitchCaseDemo {

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

}
 

 

Switch Case Example No Break

 

Tuesday
Wednesday
Thursday
Friday
Saturday
Not in week!!

 

We can clearly notice that all the case statements got executed, which is not the intended output.

In some cases, we may also have empty case blocks. In this case we are matching the alphabet character for both uppercase and lowercase.

case ‘a’:
case ‘A’:

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Java if-else statement with Examples› Java Loop Statements

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com