Switch statement in Java

Hello friends how are you, today in this blog i will teach you what is switch statement, syntax of switch , how switch statement works and many programs using switch statement in a very simple way. 

Let's start

 Switch statement in Java

  • Switch statement allows us to execute one statement from many statement and the statements are called case.
  • Inside the body of switch there are a number of cases and there is a single number is passed at the place of parameter to select and execute a case.
 

  • In the switch statement a value/number is passed in the place of parameter and the case from which the parameter is matched is executed.
  • If no case matched with parameter then default case will execute.
  • switch case does not support duplicate cases.
  • If we remove break from any case then the next case will execute automatically.
  • Default case can be written anywhere in the body of switch but it executes when no case matched.

This is very nice example to understand the concept of switch. Here we will take a variable day of integer type to store the number and we will pass this variable as a parameter in switch  and will print the result according to the value of day.

class Easy
{
 public static void main(String[] args) 
 {
  int day=2;
  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("No case matched");
  }
 }
}
/*
### Output ###
Tuesday
because day is equal to 2 and it matches with
case 2 so the output is Tuesday
*/

 If you are learning switch statement this type question is mostly asked. To make this program we will take a character variable ch , after taking user input  we will pass it into switch as a parameter and we will print the result according to cases matched.

As we know that A E I O U and a e i o u are vowels and the remaining alphabets are consonants.
here i am using only small alphabet but you can use both small and capital alphabet as per your requirements.

import java.util.Scanner;
class Easy
{
 public static void main(String[] args) 
 {
  Scanner in=new Scanner(System.in);
  char ch;
  System.out.println("Enter any alphabet");
  ch=in.next().charAt(0);
  switch(ch)
  {
   case 'a':
   case 'e':
   case 'i':
   case 'o':
   case 'u':
    System.out.println("Vowel");
    break;
   default:
    System.out.println("Consonant");
  }
 }
}
/*
### Output ###

Enter any alphabet
m
Consonant
*/

Here in the above example the input character is m and it is matching with any vowels so default case will execute and result will print as Consonant.

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. 

Try to learn something new and teach something new to other. Thanks.😊

Post a Comment

0 Comments