Switch case statement in Java language

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

 

switch case Flow diagram in java

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

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

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;
}

}

}

Similar post

Switch case in C language

Switch case in C++ language

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.