Switch Statement in Java:-

0
it is used to solve option based program ,using provide case statement to represent multiple option's.

Switch is also called jumping statement because we can switch any case statement according to

option value.

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

}


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

char ch='y';
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 greater number using switch?

Solution:-

class Swtch
{
public static void main(String args[])
{
int a,b;
a=100;
b=200;
Object o=a>b;

switch(o.toString())
{
   case "true":
   System.out.println("a is greater");
   break;
   default:
   System.out.println("b is greater");
   break;

}
}

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

}
}

}






WAP to  check vowel ,consonant  ,numeric and special using switch?

Solution:-

class Swtch
{
public static void main(String args[])
{
char ch='b';

switch(ch)
{
   case 'a':
   case 'e':
   case 'i':
   case 'o':
   case 'u':
   System.out.println("vowel");
   break;
   case '@':
   case '$':
   case '#':
   case '!':
   System.out.println("special");
   break;
   default:
   int asc=ch;
   Object o=asc>=48 && asc<57;
   switch(o.toString())
   {
       case "true":
       System.out.println("numeric");
       break;
       default:
       System.out.println("consonent");
       break;

   }
   break;   

}
}

}







Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)