TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Switch-Case in Java
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Java Flow ControlJava Tutorials

Switch-Case in Java

Last updated: Sep 04, 2023 7:18 pm
By Meenakshi Agarwal
Share
5 Min Read
Java Switch Case with Examples
Java Switch Case with Examples
SHARE

This tutorial explains how to use switch-case in Java programs. It is a multi-branch statement that allows the execution of different pieces of code based on the result of an expression.

Contents
The flow of a Java programSequentialConditional:IterativeDescriptionSyntaxFlowchartFall through

The switch case block can accept a value of int, byte, char, or short types. From JDK7, it even started allowing Enums, Strings, and objects of Wrapper classes as well.

Java Switch Case with Examples
Java Switch Case with Examples

Basics of switch-case in Java

The tutorial has the following sections to help you learn quickly.

The flow of a Java program

Whenever you write a piece of code, the compiler has to convert it into bytecode and further give you the desired output. The flow of a program represents the execution order of underlying statements.

There can be three types of flows in a program:

Sequential

The sequential flow of a program is the normal flow. It means line 1 executes first, then line 2, line 3, and so on until the control reaches the end of your code.

Conditional:

The conditional flow of a program occurs when a specific part of the code executes leaving aside another piece. It means which part to run depends upon the result of conditional statements.

Java supports two conditional statements: if-else and switch-case.

Iterative

An iterative flow encounters when a block in a program runs repeatedly. Iterative flow is made sure by a structure called loops in Java. The user decides how many times the block runs in the program.

Switch-case statement in Java

Description

The switch statement is used when the deciding expression can take more than two values. It means to test the expression against a list of values.

We can draw similarities between Java if else statements and switch-case.

Each else-if block can be compared with the case block, and the variable is checked at every case. However, one big difference between them is visible through the concept of fall through.

The fall-through can occur with a switch case.

Syntax

The expression mentioned within the switch can take values of integer or other integer primitive data types(byte, short, long) or Strings. A switch block can include any number of case blocks.

The syntax of a switch case block in Java is to write the keyword ‘case’ along with the value with which you want to check, followed by a colon.

It is important to remember the values in case blocks must be of the same data type which is mentioned within the switch expression.

Switch-case in Java does not allow any other relational operation except equality. A general syntax for switch statements looks like:

switch (expression)
{
   case val1:
   {
       statement(s);
       break; // optional
   }
   case val2:
   {
       statement(s);
       break; // optional
   }
   default:
   {
       statement(s);
   }
}

An example of the same would be:

e.g.

int value = 10;
switch(value)
{
   case 10:
   {
      System.out.println("Value is 10");
      break;
   }

   case 20:
   {
      System.out.println("Value is 20");
      break;
   }

   default:
   {
      System.out.println("Default value is 0");
   }
}

Output:

Value is 10.

Flowchart

Let’s try to understand the Java switch case statement with the help of a flowchart.

When the variable matches with a certain case’s value, the statements within the case block run until an optional break statement occurs.

“break” is a keyword in Java that makes sure when reached, the conditional flow terminates, and the sequential control of flow is attained.

This means the flow of the program reaches the next line after the switch block. The flow diagram will help you visualize the role of the break statement:

Java Switch Case Statement

Fall through

It is not necessary to include break statements for all cases. However, it would not make any sense not to.

If the break skips for a matching case, then the compiler is still in the conditional flow, and irrespective of the value, matched or not, it keeps on executing the subsequent case blocks until it finds a break statement.

In Java, we call this scenario a fall-through. Refer to the below example illustrating this condition:

int value = 10;
switch(value)
{
   case 10:
   {
      System.out.println("Value is 10");
   }

   case 20:
   {
      System.out.println("Value is 20");
   }

   case 30:
   {
      System.out.println("Value is 30");
   }

   default:
   {
      System.out.println("Default value is 0");
   }
}

Output:

Value is 10
Value is 20
Value is 30
Value is 0

You Might Also Like

A Simple Guide to Exception Handling in Java

Difference Between Spring and Spring Boot

How to Use Java String Format with Examples

Java IRC Bot with Sample Code

Generate Random Number in Java – 10 Ways

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Java If, If-Else-If Conditions Java If-Else Conditions
Next Article Java While Loop Tutorial While Loop in Java

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x