Switch Case - Java

It is a conditional control statement. all the statements will execute based on the matching case constant. every case structure is terminated by break statement.
Syntax:-
            switch(var)
{
                        case constant1:
           
                                    Statements1;
                        break;

                        case constant2:

                                    Statements2;
break;

- -  - --
Default:
Statements ;
}

WRITE A PROGRAM ON SWITCH STATEMENT.

class Sw

{
public static void main(String args[])
{
int i=10;
switch(i)
{
case 5:
case 10:
{
System.out.println("Below 10");
break;
}
case 11:
case 20:
{
System.out.println("Below 20");
break;
}
default:
System.out.println("MOre than 20");
}
}
}

WRITE A MENU DRIVEN PROGRAM ON SWITCH STATEMENT.

import java.io.*;
class Simple
{
public static void main(String argc[]) throws IOException
{
char ch;
int a=10,b=5;
System.out.println(“\n1.Add”);
System.out.println(“\n2.Sub”);
System.out.println(“\n3.mul”);
System.out.println(“\n4.Div”);
System.out.println(“\n5.Exit”);
System.out.println(“\n Enter Choice”);
ch=(char)System.in.read();

 switch(ch)
{
case '1':
System.out.println(“Sum of 2 nos”+(a+b));
break;

case '2':
System.out.println(“Sum of 2 nos”+(a-b));
break;
case '3':
System.out.println(“Sum of 2 nos”+(a*b));
break;
case '4':
System.out.println(“Sum of 2 nos”+(a/b));
break;
default:
System.exit(0);
}
}
}

 

For
Online Classes

Contact Us: 9885348743