Make Calculator Using Switch Case in Java

The body of a switch statement is known as a switch block. Any statement immediately contained in a switch block may be labeled with one or more case or or default labels. The switch statement evaluates its expression and executs the appropriate case.

A switch case statement is a multiple-branching statement where, based on a condition, the control is transferred to one of the many possible points.The switch statement is useful when selecting some action from a number of alternatives. The switch statement allows for any number of possible execution paths.

The value inside the test expression is compared against the case labels that are constant expressions as in the following:

  • If a match is found, the statements following the label are executed. Execution continues until a break is encountered that transfers control to the statement following the switch statement.
  • If no match is found, control passes to the statements following the default label. The default label is optional, if no default label is provided, the switch statement does nothing when no match is found.

The following the are the advantages of a switch case statement:

  • Switch case is a faster execution.
  • Easier to debug.
  • Easier to understand.
  • Easier to maintain.

Example

  1. //code  
  2.   
  3. import java.util.Scanner;  
  4. class demo  
  5. {  
  6.     public static void main(String arg[])  
  7.     {  
  8.         int x= 0, y=0;  
  9.         Scanner sc=new Scanner(System.in);  
  10.         System.out.println("enter choice \n1.add \n2.sub \n3.mul \n4.div");  
  11.         int ch=sc.nextInt();  
  12.         switch(ch)  
  13.         {  
  14.         case 1:  
  15.             System.out.println("enter two number");  
  16.             x=sc.nextInt();  
  17.             y=sc.nextInt();  
  18.             x=x+y;  
  19.             System.out.println("result is="+x);  
  20.             break;  
  21.               
  22.         case 2:  
  23.             System.out.println("enter two number");  
  24.             x=sc.nextInt();  
  25.             y=sc.nextInt();  
  26.             x=x-y;  
  27.             System.out.println("result is="+x);  
  28.             break;  
  29.   
  30.         case 3:  
  31.             System.out.println("enter two number");  
  32.             x=sc.nextInt();  
  33.             y=sc.nextInt();  
  34.             x=x*y;  
  35.             System.out.println("result is="+x);  
  36.             break;  
  37.   
  38.         case 4:  
  39.             System.out.println("enter two number");  
  40.             x=sc.nextInt();  
  41.             y=sc.nextInt();  
  42.             x=x/y;  
  43.             System.out.println("result is="+x);  
  44.             break;  
  45.   
  46.         default:  
  47.             System.out.println("invalid number");  
  48.         }  
  49.     }  
  50. }  
Step 1

Name it "add.java"and save the file in any location. I saved it at "c:/kiran/program".

Step 2

Set the paath. Without the path setting our Java file is not compiled.



Step 3

Set the path (path set means where you save your Java file).



Step 4

Now write the following code for checking whether or not the Java file is compiling.

javac add.java



My Java program compiled successfully.

Step 5

Write the following code in a command prompt. Press Enter and see the output.

Java demo

// demo is a class name which is written in my "add.java" file.

Output



Step 6

Continue in output and see the full output.



I hope you like this file.

Happy coding.