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

Introduction to Course Content
Video about what we are going to learn throughout the Java Training Session .

Basic Concepts of Web Designing
An introduction to domain names, web servers, and website hosting 1)What is the web? In a nutshell, the web is a whole bunch of interconnected computers talking to one another. The computers (on the...

How can everyone prepare to clear any Java interview?
Java interview your java should be much strong then J2EE. core java and Advance java is the basic foundation for Interview. Some of the topic about which you should know before going for a java interview...

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

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