Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Switch Statement in Java:-

1) Switch Statement

It is used to solve option based program.it provides multiple options using multiple case statements.

If we want to develop a choice based program then we mostly prefer switch statements in Java.

Syntax of Switch:-


switch(option)
{
   case optionvalue:
     statement;
     break;
  ....

  default:
   statement:
   break;

}

at runtime option==optionvalue  then that statement will be executed.


/* WAP to choose social media platform according to intial char? */
                                                                                                                                                                         
import java.util.Scanner;
class Socialmedia
{

    public static void main(String args[])
    {
      char ch;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter you choice of social media to enter initial char");
      ch = sc.next().charAt(0);
      switch(ch)    // ch=='f'
      {
           case 'f':
           case 'F':
           System.out.println("You have choosen Facebook");
           break;
           case 'i':
           case 'I':
           System.out.println("You have choosen Instagram");
           break;
           case 't':
           case 'T':
           System.out.println("You have choosen Twitter");
           break;
           case 'w':
           case 'W':
           System.out.println("You have choosen Whatsapp");
           break;
           default:
           System.out.println("Wrong option");
           break; 

      }
      
 
    } 


}


Q) WAP to check vowel and consonant using a switch?

import java.util.Scanner;
class CheckVC
{

   public static void main(String args[])
   {
     Scanner sc = new Scanner(System.in);
     System.out.println("enter char");
     char ch=sc.next().charAt(0);
     switch(ch)
     {
              case 'a':
              case 'A':
              case 'e':
              case 'E':
              case 'i':
              case 'I':
              case 'o':
              case 'O':
              case 'u':
              case 'U':
              System.out.println("Vowel");
              break;
              default:
              System.out.println("Consonent");
              break; 


     }


  }

}


................................................................................................................................................


WAP to check even|odd using switch?



int num=5;
switch(num%2)
{
   case 0:
   System.out.
println("even");
   break;
   default:
   System.out.println("odd");
   break;
}


WAP to check the greater number using Switch?

class Greaterswitch
{
    public static void main(String args[])
    {
         int a=10,b=20;
         switch(a/b)
         {
             case 0:
             System.out.println("b is greater");
             break;
             default:
             System.out.println("a is greater");
             break; 
             


         }
         


   }

}
.........................................................................................................

2) Nested Switch Statement:-

We can write more than one switch statement using the nested sequence in Java.

Nested Switch Statement:-

switch(option)
{

    case optionvalue:
       switch(option)
       {
             case optionvalue:
                    Statement;
                     break;
            default:
                 Statement;
                 break;   


        }
   break;
  default:
     switch(option)
   {
         case optionvalue:
            statement;
             break;
        default:
             statement;
             break;


    }
 break;
}


}

WAP to check divisibility that number is divisible by 3 and 5 both?

import java.util.Scanner;
class CheckVC
{

   public static void main(String args[])
   {
      int num=3;
      switch(num%3)
      {
                case 0:
                 switch(num%5)
                  {
                       case 0:
                        System.out.println("divisible by both");
                        break;
                       default:
                       System.out.println("only divisible by 3");
                       break;

                  }
                 break;
               default:
                  switch(num%5)
                  {
                       case 0:
                        System.out.println("divisible by five");
                        break;
                       default:
                       System.out.println("not divisible by any");
                       break;

                  }
                break;

     }   


   


  }

}


.......................................................................................................

WAP to check the greater number using a switch?

The solution to this program:-
class Greater
{
  public static void main(String args[])
  {
      int a=20,b=100;
      switch(a/b)
      {
          case 0:
           System.out.println("b is greater");
           break;
          default:
           System.out.println("a is greater");
           break;   

      }            



   }

}

WAP to check the greatest number using a switch?

class Greater
{
  public static void main(String args[])
  {
      int a=9,b=30,c=7;
      switch(a/b)
      {
          case 0:
             switch(b/c)
             {
               case 0:
                System.out.println("c is greater");
                break;
               default:
                 System.out.println("b is greater");
                 break;   
          }
          break;
          default:
          switch(a/c)
             {
               case 0:
                System.out.println("c is greater");
                break;
               default:
                 System.out.println("a is greater");
                 break;   
          }
          break;

      }            



   }








}


Program to calculate the greatest using the conditional operator?


class SwitchGreater
{

    public static void main(String args[])
    {
          int a=10,b=2000,c=300;
          Object o = a>b;  //false 
          switch(o.toString().charAt(0))  //f
          {
                case 't':
                Object o1 = a>c;
                switch(o1.toString().charAt(0))
                {
                   case 't':
                     System.out.println("A is greatest");
                     break;
                  default:
                     System.out.println("C is greatest");
                     break;   

                } 
                break;
                default:
                Object o2 = b>c;  //true
                switch(o2.toString().charAt(0))  //t
                {
                   case 't':
                     System.out.println("b is greatest");
                     break;
                  default:
                     System.out.println("C is greatest");
                     break;   

                } 
               break;

           }


    }



}

Assignment of Switch:-


Q)WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?


Q) WAP to check leap year using a switch?


Q) WAP  to check entered char is numeric, alphabets, or special using switch? 


import java.util.Scanner;
class SwitchExample
{
   public static void main(String args[])
   {
        char ch;
       
        Scanner sc = new Scanner(System.in);
        System.out.println("enter char");
        ch=sc.next().charAt(0);
         
        Object o=ch>=48 && ch<=57;
        
        Object o1 = ch>=65 && ch<=91 || ch>=97 && ch<=123;
    
        switch(o.toString())
        {
           case "true":
           System.out.println("digit");
           break;
           default:
           switch(o1.toString())
           {
           case "true":
           System.out.println("alphabets");
           break;
           default:
           System.out.println("Special"); 
           }
           break;

        }        

   }




}



19 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. class Leapyearswitch
    {
    public static void main(String args[])
    {
    int num=2012;
    switch(num%4)
    {
    case 0:
    System.out.println("Leap year");
    break;
    default:
    System.out.println("not a leap year");
    break;
    }
    }

    }

    ReplyDelete
  2. class Leapyearswitch
    {
    public static void main(String args[])
    {
    int num=2012;
    switch(num%4)
    {
    case 0:
    System.out.println("Leap year");
    break;
    default:
    System.out.println("not a leap year");
    break;
    }
    }

    }

    ReplyDelete
  3. import java.util.Scanner;
    class evenodd
    {
    public static void main(String[]args)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");

    int i=sc.nextInt();

    switch(i%2)
    {
    case 0:
    System.out.println("even");
    break;
    default:
    System.out.println("odd");
    break;
    }

    }
    }

    ReplyDelete
  4. import java.util.Scanner;
    class CheckVC
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");
    char x=sc.next().charAt(0);
    switch(x)
    {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
    System.out.println("Vowel");
    break;
    default:
    System.out.println("consonent value");

    }
    }
    }

    ReplyDelete
  5. import java.util.Scanner;
    class Greatnoo
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value of a");
    int a=sc.nextInt();
    System.out.println("enter the value of b");
    int b=sc.nextInt();
    switch(a/b)
    {
    case 0:
    System.out.println("b is greater than a");
    break;
    default:
    System.out.println("a is greater than b");
    break;
    }
    }
    }

    ReplyDelete
  6. import java.util.Scanner;
    class CheckDIV
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    int a=3;
    switch(a%3)
    {
    case 0:
    switch(a%5)
    {
    case 0:
    System.out.println("divisible by both");
    break;
    default:
    System.out.println("only div by 3");
    break;
    }
    break;
    default:
    switch(a%5)
    {
    case 0:
    System.out.println("div by 5");
    break;
    default:
    System.out.println("not div by any");
    break;
    }
    break;
    }
    }
    }

    ReplyDelete
  7. class Greatnoswitch
    {
    public static void main(String[]args)
    {
    int a=9, b=30, c=7;

    switch(a/b)
    {
    case 0:
    switch(b/c)
    {
    case 0:
    System.out.println("c is greater");
    break;
    default:
    System.out.println("b is greater");
    break;
    }
    break;
    default:
    switch(a/c)
    {
    case 0:
    System.out.println("c is greater");
    break;
    default:
    System.out.println("a is greater");
    break;
    }
    break;
    }
    }
    }

    ReplyDelete
  8. import java.util.Scanner;
    class yesnocancle
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");
    char ch=sc.next().charAt(0);

    switch(ch)
    {
    case 'y':
    System.out.println("yes");
    break;
    case 'n':
    System.out.println("no");
    break;
    case 'c':
    System.out.println("cancle");
    break;
    default:
    System.out.println("something incorrect");
    break;
    }
    }
    }

    ReplyDelete
  9. import java.util.Scanner;
    class leapyear
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the year");
    int year=sc.nextInt();

    switch(year%4)
    {
    case 0:
    System.out.println("leap year");
    break;
    default:
    System.out.println("this is not a leap year,..");
    }
    }
    }

    ReplyDelete
  10. import java.util.Scanner;
    class EO
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter number...");
    int num=sc.nextInt();

    switch(num%2)
    {
    case 0:
    System.out.println("even");
    break;
    default:
    System.out.println("odd");
    break;
    }
    }
    }

    ReplyDelete
  11. // Akshay


    class Year
    {

    public static void main(String args[])
    {
    int num=2021;
    switch(num%4)
    {
    case 0:
    switch(num%100)
    {
    case 0:
    System.out.println("leap");
    break;
    default:
    System.out.println("non leap");
    break;

    }
    break;
    default:
    switch(400)
    {
    case 0:
    System.out.println("leap");
    break;
    default:
    System.out.println("non leap");
    break;

    }
    break;

    }





    }

    }

    ReplyDelete
  12. import java.util.*;
    class Switch
    {
    public static void main(String arg[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter char");
    char ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'a':System.out.println("vowel");
    break;
    case 'e':System.out.println("vowel");
    break;
    case 'i':System.out.println("vowel");
    break;
    case 'o':System.out.println("vowel");
    break;
    case 'u':System.out.println("vowel");
    break;

    default:System.out.println("consonent");


    }
    }
    }

    ReplyDelete
  13. import java.util.*;
    class EO
    {
    public static void main(String arg[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter number");
    int x=sc.nextInt();
    switch(x%2)
    {
    case 0:System.out.println("Even");
    break;

    default:
    System.out.println("odd");
    }
    }
    }

    ReplyDelete
  14. import java.util.*;
    class Y
    {
    public static void main(String arg[])
    {

    Scanner sc=new Scanner(System.in);
    System.out.println("Enter ");
    char ch=sc.next().charAt(0);
    switch(ch)
    {
    case 'y':System.out.println("yes ");
    break;
    case 'n': System.out.println("no");
    break;
    case 'c':System.out.println("cancel");
    }

    }
    }

    ReplyDelete
  15. import java.util.*;
    class Leap
    {
    public static void main(String arg[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter char");
    int x=sc.nextInt();
    switch(x%400)
    {
    case 0:System.out.println("leap year");

    switch(x%100!)
    {
    System.out.println("not leap");
    break;
    default :

    System.out.println("leap year");
    }
    }



    }
    }

    ReplyDelete

  16. /*WAP to display "yes", "no" and "cancel" when user assign 'y','n', and 'c'?*/
    import java.util.Scanner;
    class EnterChoice
    {

    public static void main(String args[])
    {
    char ch;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter you choice ");
    ch = sc.next().charAt(0);
    switch(ch)
    {
    case 'y':
    case 'Y':
    System.out.println("yes");
    break;
    case 'n':
    case 'N':
    System.out.println("no");
    break;
    case 'c':
    case 'C':
    System.out.println("cancle");
    break;

    default:
    System.out.println("invalid choice");
    break;

    }


    }


    }

    ReplyDelete
  17. import java.util.*;
    class LeapYear
    {
    public static void main(String arg[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter char");
    int num =sc.nextInt();

    switch(num%4)
    {

    case 0:
    switch(num%100)
    {
    case 0:
    System.out.println("not leap");
    break;
    default:
    System.out.println(" leap");
    break;


    }
    break;
    default:
    switch(400)
    {
    case 0:
    System.out.println(" leap");
    break;
    default:
    System.out.println("not leap");
    break;


    }
    break;
    }


    }
    }

    ReplyDelete

  18. import java.util.Scanner;
    class EnterPin1
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int num = 1000;
    int pin =0;
    int a = sc.nextInt();
    switch(a%num)
    {
    case 0 :
    System.out.println("Enter amount");
    break;
    default:
    System.out.println("1.you eneterd invalid pincode please try Again");
    int number = sc.nextInt();
    switch(a%num){
    case 0 :
    System.out.println("Pincode successfully entered");
    System.out.println("Enter amount");
    int amount = sc.nextInt();
    break;
    default:
    System.out.println("2.you eneterd invalid pincode please try Again");
    int b = sc.nextInt();

    }
    switch(a%num)
    {
    case 0 :
    System.out.println("Pincode successfully entered");
    System.out.println("Enter amount");
    int amount = sc.nextInt();
    break;
    default:
    System.out.println("3.you eneterd invalid pincode please try Again after 30 min");
    }
    }
    }
    }

    ReplyDelete
  19. import java.util.Scanner;
    class B_Banking
    {
    public static void main(String args[])
    {
    Scanner sc = new Scanner(System.in);
    int pincode = sc.nextInt();
    int i = 1;
    int count = 1 ;int balance = 5000 ;
    while(i<=pincode)
    {
    pincode = pincode / 10;
    count++;
    i++;
    }
    //System.out.println(count);

    switch(count%6)
    {
    case 0 :
    System.out.println("Press C for Credit");
    System.out.println("Press D for Debit");
    System.out.println("Press B Check Balance");
    System.out.println("Press R for Repeat");
    System.out.println("Press E for Exit");
    char ch = sc.next().charAt(0);


    switch(ch)
    {
    case 'C':
    case 'c':
    System.out.println("Credit");
    System.out.println("Enter Amount");

    break;

    case 'D':
    case 'd':
    System.out.println("Debit");
    System.out.println("Enter Amount");

    break;

    case 'B':
    case 'b':
    System.out.println("Check Balance");
    System.out.println("Balance = " +balance);
    break;

    case 'R':
    case 'r':
    System.out.println("Repeat");
    break;

    case 'E':
    case 'e':
    System.out.println("Exit");
    break;

    default:
    System.out.println("Invalid key");
    break;
    }


    break;
    default: System.out.println("Invalid"); break;
    }

    }
    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post