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

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

CoreJAVA
Core Java Training High Level Course Content Trained by Java Architect 1. Core Java Programming Introduction of Java 2. Data types and Operators 3. Control Flow statements 4. OOPS and its application...
A

How to create excel sheets using Java?
HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet1 = workbook.createSheet("actions");for (int i = 0; i < 10; i++) { sheet1.createRow(i); for (int j = 0; j < 10; j++) { HSSFRow currRow =...

What Is Applet & Its Life Cycle?
What is Applet & its life cycle?Applet is a Java programme that can be embedded into HTML page.Java Applet runs on the java enables web browsers such as Mozilla & Internet Explorer.Applets are...
I

ICreative Solution

0 0
0

What Would Be Life Cycle Of A Fresher After Campus In An IT Company?
1. Basic Technical Training: Since freshers are not subject matter experts so gone through 3 - 6 months basic technical training within Organization. 2. Technical Assessment: HR sends freshers to various...
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