Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Java Switch Statement
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. Java Tutorials
  3. Java Switch Statement

Java Tutorial

Basic JAVA Fundamentals

  • JAVA Tutorial Content
  • What Is Java
  • History Of Java
  • Java Environment
  • Features Of JAVA
  • C++ Vs Java
  • Java Basic Syntax
  • First Program Of Java
  • Java Identifiers
  • Java Modifiers
  • Java Variable Types
  • Java Basic Datatypes
  • Java Keywords

Control Statements

  • Control Statements
  • Java If Else Statement
  • Nested If Statement In Java
  • Java Switch Statement

JAVA Loops

  • Loop In Java
  • For Loop In JAVA
  • Foreach Loop In JAVA

Java Switch Statement

Java Switch Statement

The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement.

This is an easier implementation to the if-else statements. The keyword "switch" is  followed by an expression that should evaluates to byte, short, char or int primitive data types ,only. In a switch block there can be one or more labeled cases. The expression that creates labels for the case must be unique. The switch expression is matched with each case label. Only the matched case is executed ,if no case matches then the default statement (if present) is executed.

Syntax

switch ( expression )
{
 case value1 :
  // code executed
  break;
 case value2:
  // code executed
  break;
...
...
 case valueN:
  // code executed
  break;
...
...
 default: 
  // code executed
  break;
}

Java Switch Statement Example :

public class SwitchCaseExample
{
   public static void main(String[] args)
   {
    int day = 3;
        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("Thrusday");
            break;
            case 5:
             System.out.println("Friday");
            break;
            case 6:
             System.out.println("Saturday");
            break;
            case 7:
             System.out.println("Sunday");
            break;
            default:
             System.out.println("Invalid entry");
            break;
       }
    }
}

Output :

Wednesday

Java Switch Statement Example With User Input:

 

import java.util.Scanner;  //Needed for Scanner object
public class SwitchCaseExWithUserInput
{
   public static void main(String[] args)
   {
    Scanner scanNumber= new Scanner(System.in);
     // Get the Day.
      System.out.print("Enter a Day Number 1 to 7 : ");
      int dayN = scanNumber.nextInt();
        switch (dayN) {
            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("Thrusday");
            break;
            case 5:
             System.out.println("Friday");
            break;
            case 6:
             System.out.println("Saturday");
            break;
            case 7:
             System.out.println("Sunday");
            break;
            default:
             System.out.println("Invalid entry");
            break;
       }
    }
}

Output :

Enter a Day Number 1 to 7 : Friday

Java Switch Statement is fall-through

The java switch statement is fall-through. It means it executes all statement after first match if break statement is not used with switch cases.

Java Switch Statement Example:

public class SwitchExample3 {  
public static void main(String[] args) {  
    int number=20;  
    switch(number){  
    case 10: System.out.println("10");  
    case 20: System.out.println("20");  
    case 30: System.out.println("30");  
    default:System.out.println("Not in 10, 20 or 30");  
    }  
}  
}  

Output :

20
30
Not in 10, 20 or 30

 

Series: Java Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
switch case java string switch case in java example programs java switch multiple case java switch case multiple values java switch enum java switch case enum switch statement example case expressions must be constant expressions

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.