Switch case with char

In previous post, we have read the switch statement in java. You must have seen we can use the switch statements with integers, String, and switch cases with wrapper classes. In this topic we see how we can use the java switch with char value. We can also read the some related articles that will help us to choose better strategy.
Here is the table content of the article will we will cover this topic.
1. switch statement in Java?
2. Rules of the switch statement Java
3. Nested switch statements in Java?
4. Does Java switch multiple cases?
5. switch case without a break statement?
6. switch case with char?
7. switch case with wrapper classes?

8. Why does float values are not allowed in the switch statement?
9. Why the break statement is used in the switch statement?

switch case with char?

The switch statement executes one block of the statement from multiple blocks of statements based on condition. In the switch statements, we have a number of choices and we can perform a different task for each choice.
As you already knew the execution of Switch statement is totally dependent on Switch expression. In this article how we can use the Switch case with char in expression.

Switch case with char
public class ExampleOfSwitch
 {  
	public static void main(String[] args)
	{  
	     //Declaring a variable for switch expression  
	     char alphabet = 'b';  
	     switch(alphabet) //Switch expression  
	     {  
		   	 //Case statements  
		   	case 'a': 
		   	System.out.println("This is character 'a' ");
		   	break;
		   	case 'b':
		   	System.out.println("This is character 'b' ");
		   	break;
		   	case 'c':
		   	System.out.println("This is character 'c' ");
		   	break;  
		        //Default case statement  
		   	default: System.out.println("Please enter valid input");  
	      }  
	  }  
}  

Output: This is character ‘b’

In this example, we want to print the name of the character based on the input entered by the user. Firstly, we declared a variable of char type. After that, we are using that variable in switch expression. The switch expression variable is a type of char now. So, we creating three cases of type char. Each case has different codes according to our requirements. We also provide a default case at the end of the switch if the user enters any wrong input which doesn’t match with any case then the default case will execute.

The Switch expression always allows a valid input. So we can try it in some different manner. Here we are declaring a String and provide only one character from the to Switch statement.

public class ExampleOfSwitch
 {  
	public static void main(String[] args)
	{  
	     //Declaring a String  
	     String alphabet = "HiHello";  
	     switch(alphabet.charAt(0)) //Switch expression  
	     {  
		   	 //Case statements  
		   	case 'H': 
		   	System.out.println("This is character 'H' ");
		   	break;
		   	case 'b':
		   	System.out.println("This is character 'b' ");
		   	break;
		   	case 'c':
		   	System.out.println("This is character 'c' ");
		   	break;  
		        //Default case statement  
		   	default: System.out.println("Please enter valid input");  
	      }  
	  }  
}  

1. Quiz, Read the below code and do answer.

public class ExampleOfSwitch
 {  
    public static void main(String[] args)
    {  
         //Declaring a String  
         String alphabet = "H";  
         switch(alphabet) //Switch expression  
         {  
                //Case statements  
               case 'H': 
               System.out.println("This is character 'H' ");
               break;
               case 'b':
               System.out.println("This is character 'b' ");
               break;
               case 'c':
               System.out.println("This is character 'c' ");
               break;  
                //Default case statement  
               default: System.out.println("Please enter valid input");  
          }  
      }  
}

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

public class ExampleOfSwitch
 {  
    public static void main(String[] args)
    {  
         //Declaring a String  
         char alphabet = 'H';  
         switch(alphabet) //Switch expression  
         {  
                //Case statements  
               case 'H': 
               System.out.println("This is character 'H' ");
               break;
               case 'b':
               System.out.println("This is character 'b' ");
               break;
               case 'c':
               System.out.println("This is character 'c' ");
               break;  
                //Default case statement  
               default: System.out.println("Please enter valid input");  
          }  
      }  
}

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

public class ExampleOfSwitch
 {  
    public static void main(String[] args)
    {  
         //Declaring a String  
         char alphabet = '5';  
         switch(alphabet) //Switch expression  
         {  
                //Case statements  
               case 'H': 
               System.out.println("This is character 'H' ");
               break;
               case 'b':
               System.out.println("This is character 'b' ");
               break;
               case '5':
               System.out.println("This is character '5' ");
               break;  
                //Default case statement  
               default: System.out.println("Please enter valid input");  
          }  
      }  
}

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class ExampleOfSwitch
 {  
    public static void main(String[] args)
    {  
         //Declaring a String  
         String alphabet = "Hi";  
         switch(alphabet.charAt(0)) //Switch expression  
         {  
                //Case statements  
               case 'H': 
               System.out.println("This is character 'H' ");
               break;
               case 'b':
               System.out.println("This is character 'b' ");
               break;
               case 'i':
               System.out.println("This is character 'i' ");
               break;  
                //Default case statement  
               default: System.out.println("Please enter valid input");  
          }  
      }  
}

Click on anyone to know the answer.

1 thought on “Switch case with char”

Leave a Comment