0% found this document useful (0 votes)
5 views

Switch Case

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Switch Case

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

When we have multiple options among the multiple options when we have to choose

paticular options in that case we will use switch case.

Syntax : switch(choice){

case 1:
break;
case 2:
break;
case 3:
break:
default :

Example :

package decisionMakingStatements;

import java.util.Scanner;

public class SwitchCaseDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Press 1 for Case1");
System.out.println("Press 2 for Case2");
System.out.println("Press 3 for Case3");
System.out.println("Press 4 for Case4");
System.out.println("Press 5 for Case5");
int ch = sc.nextInt();
switch (ch) {

case 1:
System.out.println("This is case 1");
break;
case 2:
System.out.println("This is case 2");
break;
case 3:
System.out.println("This is case 3");
break;
case 4:
System.out.println("This is case 4");
break;
case 5:
System.out.println("This is case 5");
break;
default:
System.out.println("Invalid choice");

}
}

Example :
package decisionMakingStatements;

import java.util.Scanner;

public class SwitchCaseDemo {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.println("Welcome to SBI Bank ATM");
System.out.println("Press 1 for withdraw");
System.out.println("Press 2 for deposit");
System.out.println("Press 3 for balance enquiry");
System.out.println("Press 4 for tranfer money");
System.out.println("Press 5 for Exit");
int ch = sc.nextInt();
switch (ch) {

case 1:
System.out.println("Withdraw Successful");
break;
case 2:
System.out.println("Deposit successful");
break;
case 3:
System.out.println("Your current bal is 2000.00");
break;
case 4:
System.out.println("Money tranfer successful");
break;
case 5:
System.out.println("Exit");
break;
default:
System.out.println("Invalid choice");

}
}

You might also like