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

Java Advantages
In this video, learn about Java and its advantages. Also, check out the difference between Java and C++, Java development kit, Java Run Time Environment (JRE) with a proper demonstration program for better clarity.

Overloading in JAVA
When a class contains more than one method with the same method name but different argument types, then it is called Overloading. Methods are said to be Overloaded methods. Also, know as Compile time...

GIT
In software engineering, software configuration management (SCM) is the task of tracking and controlling changes in the software, part of the larger cross-disciplinary field of configuration management....
R

On the Job training is always best
On the job training always provides an opportunity to learn the best industry practices. While you work on real time you would encounter many challenges that will force you to learn many new things. Class...
M

doWhile example in Java
public class doWhilePracticleEx { public void test() { } public static void main(String args) { String q1 = "Who is PM of India?"; String a1 = "Sonia Gandhi"; String a2 = "Rahul Gandhi"; String a3...
S

Sarthak C.

0 0
0
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