UrbanPro
true

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

SWITCH STATEMENT IN JAVA

Khushi A.
02/05/2019 0 1

switch statement - condition statement - only once the checking takes place so we recommend use break statement.

switch(condition/variable/choice)
{
case 1:
//statements
break;
case 2:
// statements
break;
case 3:
//statement
break;
case 4:
//statement
break;

default:
//statement
break;

}

 

EXAMPLE  

WE ARE CREATING A MENU DRIVEN PROGRAM OF CALCULATOR

import java.util.*;

public class CalculatorSwitch
{
public static void main(String agr[])
{
int a,b,c;
char ch;
Scanner sc = new Scanner(System.in);
System.out.println("enter two number");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("You have following choice to manipulate data, press the symbol as given below");
System.out.println("+ : Addition");
System.out.println("- : subtraction");
System.out.println("* : multiply");
System.out.println("/ : divide");
System.out.println("% : modulus");
System.out.println("^ : power");
ch = sc.next().charAt(0);

switch(ch)
{
case '+':
c= a+b;
System.out.println("Added = "+c);
break;
case '-':
c = a-b;
//c= a+b;
System.out.println("subtarct = "+c);
break;

case '*':
c= a*b;
System.out.println("Multiply = "+c);
break;

case '/':
c= a/b;
System.out.println("Divide = "+c);
break;
case '%':
c= a%b;
System.out.println("remainder = "+c);
break;
case '^':
c= a^b;
System.out.println("power= "+c);
break;
default:
System.out.println("Worng choice");
break;
}

}

 

OUTPUT

enter two number
4732
773
You have following choice to manipulate data, press the symbol as given below
+ : Addition
- : subtraction
* : multiply
/ : divide
% : modulus
^ : power
-
subtarct = 3959

0 Dislike
Follow 3

Please Enter a comment

Submit

Khushi A. | 10/07/2019

Break statement allows to terminate the code from the very next line. If we do not use break after very code then consecutive cases will also get executed without checking further, untill break statement or cases end up.

0 0

Other Lessons for You

Constructor Overloading
public class ConstructorOverloading { public ConstructorOverloading(){ System.out.println("default"); } public ConstructorOverloading(String a){ System.out.println("a"); } public ConstructorOverloading(int...
S

Sarthak C.

0 0
0

Simple Input and Output in CoreJava
We can use any of the following options based on the requirements to accept data from the user in corejava and show them Integer Input Output Character Input Output Float Input Output INPUT AND...

Android : Support multiple themes in an android application
If you are developing a theme based application to enhance user experience, then following steps needs to follow.We are taking example of an android application having 2 themes white and black.Step 1:Define...

Interview Tip : Q1) Why Strings are immutable in java ? What happen if it was mutable in java?
As we all know that Strings in java are immutabe in nature, now the question comes why the creator made it immutable in nature, although this field used maximum in any java program. The answer to this...

Java Program Sample Application
/* WAP to print given message on the console using java*/ // Class declaration in java class sample { // Main function public static void main(String args) { // function for printing on the console System.out.println("Hello...
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more