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"


It is used to solve option based program, switch provide multiple option statement and we can switch into a particular statement.

The switch statement is also called jumping statement because switch option can jump into any option value.

switch(option)
{
     case option value:
      statement;
      break;
      case option value:
       statement
       break;
       default: (optional statement)
        statement;
        break;

}

at runtime switch statement will compare from option==optionvalue if it is equal then that statement will be executed.

Question for Switch Statement?

Q)WAP to display "YES","NO" and "CANCEL" when user press 'y','n' and 'c'?

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);
       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("Cancel");
          break;

       }

    }


}

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

WAP to check even ,odd using switch statement?


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


 

}


ASSIGNMENT:-
Q1) WAP to check vowel, consonant and special char using Switch?
Solution:-


class Checkvc
{
 public static void main(String args[])
 {
      char ch='c';
   
      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:
           int asc = ch;
           Object o = asc>=65 && asc<=91 || asc>=97 && asc<=123;
           switch(o.toString().charAt(0))
           {
               case 't':
               System.out.println("consonent");
               break;
               default:
               System.out.println("special");
               break;
         


           }
           break;
           



       }       


 }


}
Q2) wap to check that entered number is one digit, two digits or three-digit?


Q3) WAP to check greatest number using a switch?

class Greatest
{
   public static void main(String args[])
   {
      int a=12,b=34,c=11;
      switch(a/b)
      {
          case 0:
          switch(b/c)
          {
               case 0:
                System.out.println("c is greatest");
                break;
                default:
                System.out.println("b is greatest");
                break;
         }
         break;
         default:
         switch(a/c)
         {
              case 0:
              System.out.println("c is greatest");
              break;
              default:
              System.out.println("a is greatest");
              break;


         }

      }



    }



}


Q4) WAP to perform addition,subtraction, multiplication, division when user press +,-,*,/ and %  option.















3 Comments

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

  1. import java.util.Scanner;
    class Calci
    {
    public static void main(String fmgs[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter 1'st Number");
    int a=sc.nextInt();
    System.out.println("Enter 2'nd Number");
    int b=sc.nextInt();
    System.out.println("What would you like to do\n + for addition \n - for subtraction \n * for multiplication \n / for division \n % for modulus \n");
    char c=sc.next().charAt(0);
    switch(c)
    {
    case '+' :
    System.out.println(a+b);
    break;
    case '-' :
    System.out.println(a-b);
    break;
    case '*' :
    System.out.println(a*b);
    break;
    case '/' :
    System.out.println(a/b);
    break;
    case '%' :
    System.out.println(a%b);
    break;
    }
    }
    }

    ReplyDelete
  2. import java.util.Scanner;
    class Greatest
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter 1'st Number");
    int a=sc.nextInt();
    System.out.println("Enter 2'nd Number");
    int b=sc.nextInt();
    switch(a/b)
    {
    case 0 :
    System.out.println("2'nd Number is greatest");
    break;
    default :
    System.out.println("1'nd Number is greatest");
    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