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
Java is a high level language which is far better than procedural languages like C, Fortran and Pascal. Java completely stands with the principles of OOPS (Object Oriented Programming Structure). The...
A

Abhisheak Saxena

0 0
0

ClassNotFoundException vs NoClassDefFoundError
ClassNotFoundException NoClassDefFoundError It is an exception and happens due to programmer’s mistake and can be recovered by updating the code. Thrown when an application tries...

Session Tracking In Java Servlets
Session Tracking: HTTP is a stateless protocol. Each request is independent of the previous one. However, in some applications, it is necessary to save state information so that information can be collected...


Class and Objects in Java
Class is a template or a blueprint which is used to describe an object. On other hand Object is a reference of a class which follows all the stuff written inside the class. How about taking the whole tour in the following video
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