• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Test Cases
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Java Switch Case Statement With Simple Program Examples

Java Switch Case Statement With Simple Program Examples

Last Updated on: November 29, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • How to Use Switch Cases In Java
  • Important Points Of Switch Statements

Java Switch Case Statement With Simple Program Examples: Today, we are going to learn another new topic of Core Java tutorial series. That is Switch Case and if you are not reading our previous post about If-Else In Java then by using that link you can understand that. Switch Case statements used when there is several options are available, and we need to perform some specific task as per the selection.

How to Use Switch Cases In Java

the syntax for switch-case statements it looks like below:

switch(variable)
{
case value1: statement1;
case value2: statement2;
case value3: statement3;
.
.
.
case valuen: statement;
[default : default_statements;]
}

Java Switch Statement Example

package java_Basics;

public class SwitchCase_Example 
{
   public static void main(String[] args) 
   {
      int i = 2; 
        switch (i) 
        { 
        case 0: 
            System.out.println("i is zero."); 
            break; 
        case 1: 
            System.out.println("i is one."); 
            break; 
        case 2: 
            System.out.println("i is two."); 
            break; 
        default: 
            System.out.println("i is greater than 2."); 
        } 
   }
}

Output:

i is two.

Important Points Of Switch Statements

  • The case value cannot duplicate
  • The entered Value and the case Value must be the same data type.
  • The Value for Case must be literal or constant. you can not use the variable in case.
  • The Break statement is used inside the switch case to terminate the flow of statement sequence.
  • The Break statement is optional. If there is no break, then the flow of execution will continue to the next case.
  • The default statement in switch case is optional, and that can appear anywhere of the switch block.
  • Java Switch case statement make the code more readable by omitting if..else..if statements.
  • Make sure you are going to use Switch case string when you are going to use Java 7 otherwise you will get an exception.
  • You can use nested switch statements which means you can use a switch case statement inside another switch case.

If You find anything wrong and if you want to add some more information then write in the comment section, and we are happy to update with your information.

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2022 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers