Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Switch case statement in Java language

Posted on December 30, 2016September 28, 2020

Table of Contents

  • Switch case statement in Java language
    • Switch case Syntax in java
    • switch case Flow diagram in java
    • Example programs to switch case

Switch case statement in Java language

In this tutorial, we will discuss the concept of the Switch case statement in Java language

In this post, we will learn how to use the  switch case statement in java language

  • The switch statement is one of the java statement allows a unique variable to be checked for equality against a list of the case value of the same type variable(int, String, char can use anyone )
  • Switch case similar if-else statement functioning based Boolean expression.
  • Switch statement choose and executes one from multiple conditions, that multiple conditions are called  cases
  • Any number of the case may use within a switch and every case statement contain a value to compared with switch value. case and case value separated by a colon(case: value)
  • End of the switch  can have a default statement the default statement can be used for performing a task when becoming false

Switch case Syntax in java

Syntax

"<yoastmark

 

switch case Flow diagram in java

"<yoastmark

How to work switch statements

  • The switch statement is evaluated once
  • The value of the switch expression is compared with the value of each case.
  • When there is a match of the particular case, the associated block of statements is executed.
  • When there is not match any cases, the default statement is executed.

 

 

Example programs to switch case

Example 1
public class switchnotd{               // start class
public static void main(String args[]){   //start main method
int age=30;                               //variable Assignment
switch(age){                              //swich expression
case 10:                                 //if statement is true this case executed
System.out.println(“10”);                 //if false go to next case
break;
case 20:                               //if statement is true this case executed
System.out.println(“20”);                 //if false go to next case
break;
case 30:                                  //if statement is true this case executed
System.out.println(“30”);                  //if false go to next case
break;
default:System.out.println(“age not match”); // all case are false this statement executed
}
}                                     // end of main method
}                                      // end of class

"<yoastmark

Example 2

public class switchnotd{        // start of class
public static void main(String args[]){// start of main method
int age=10;                    //variable Assignment
switch(age){                   //swich expression
case 10:       //if statement is true this case executed
System.out.println(“10”);   //if false go to next case

case 20:        //if statement is true this case executed
System.out.println(“20”);//if false go to next case

case 30:         //if statement is true this case executed
System.out.println(“30”);//if false go to next case

default:System.out.println(“age not match”);
// all case are false this statement executed

}
}                                    / end of main method

}                                      // end of class

"<yoastmark

Example 3

class age{
public static void main (String args[]){

int age=20;

switch(age){
case 10:
System.out.println(“you age is ten”);
break;

case 20:
System.out.println(“you age is twenty”);
break;

case 30:
System.out.println(“you age is thirty”);
break;

case 40:
System.out.println(“you age is forty”);
break;

default:
System.out.println(“i don’t no age”);
break;
}

}

}

"<yoastmark

Similar post

Switch case in C language

Switch case in C++ language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes