java switch statement in Java is used to execute one block of code among multiple alternatives based on the value of a variable. It is an alternative to using multiple if-else
conditions.
public class Switch1 { public static void main(String[] args) { int x=3; switch(x) { case 0: System.out.println("0"); break; case 1: System.out.println("1"); break; case 2: System.out.println("2"); break; case 3: System.out.println("3"); break; default: System.out.println("Differt Number"); } } }
Output
3
public class Switch2 { public static void main(String[] args) { int x=1; switch(x) { case 0: System.out.println("John"); break; case 1: System.out.println("Anne"); break; case 2: System.out.println("Peter"); break; default: System.out.println("Differt"); } } }
Output
Anne
public class Switch1 { public static void main(String[] args) { switch(0) { case 0: System.out.println("John"); break; case 1: System.out.println("Anne"); break; case 2: System.out.println("Peter"); break; default: System.out.println("Different "); } } }
Output
John
public class Switch2 { public static void main(String[] args) { switch(1) { case 0: System.out.println("John"); case 1: System.out.println("Anne"); case 2: System.out.println("Peter"); default: System.out.println("Differt "); } } }
Output
Anne
Peter
Differt
Example of Calculator
import java.util.*; class Example{ public static void main(String args[]){ Scanner input = new Scanner(System.in); System.out.print("Enter Number 1 : "); int number1 = input.nextInt(); System.out.print("Enter Number 2 : "); int number2 = input.nextInt(); System.out.println(); System.out.println("A. Adition"); System.out.println("B. Subtraction"); System.out.println("C. Division"); System.out.println("D. Multiplication."); System.out.println(); System.out.print("Please select one option : "); String option = input.next(); System.out.println(); switch(option){ case "A": System.out.println("Add : " + (number1 + number2)); break; case "B": if(number1 > number2){ System.out.println("Sub : " + (number1 - number2)); }else{ System.out.println("Sub : " + (number2 - number1)); } break; case "C": if(number1 > number2){ System.out.println("Sub : " + (number1/number2)); }else{ System.out.println("Sub : " + (number2/number1)); } break; case "D": System.out.println("Multi : " + (number1 * number2)); break; default : System.out.println("Please select valid option"); } } }
Output
Enter Number 1 : 45
Enter Number 2 : 67
A. Adition
B. Subtraction
C. Division
D. Multiplication.
Please select one option : A
Add : 112
Grade Calculation
import java.util.*; class Example{ public static void main(String args[]){ Scanner input = new Scanner(System.in); System.out.print("Enter Student Name : "); String name = input.next(); System.out.print("Enter Student Address : "); String address = input.next(); System.out.print("Enter Number of Subjects : "); int number_of_subjects = input.nextInt(); System.out.println(); int marks = 0; int max = 0; int total = 0; for(int i = 0 ; i < number_of_subjects ; i++){ System.out.print("Enter Subject " + (i + 1) + " Marks : "); marks = input.nextInt(); if(max < marks){ max = marks; } total = total + marks; } boolean flag = true; while(flag){ System.out.println(); System.out.println("A. Show Student Details."); System.out.println("B. Show Maximum Marks."); System.out.println("C. Show Total, Average & Result."); System.out.println("X. Exit."); System.out.println(); System.out.print("Please select one option : "); String option = input.next(); System.out.println(); switch(option){ case "A": System.out.println("Student Name : " + name); System.out.println("Student Address : " + address); break; case "B": System.out.println("Maximum Marks : " + max); break; case "C": double average = total / (double)number_of_subjects; String result = "Fail"; if(average >= 50){ result = "Pass"; } System.out.println("Total : " + total); System.out.println("Average : " + average); System.out.println("Result : " + result); break; case "X": System.out.println("Good bye...."); flag = false; break; default : System.out.println("Please select valid option."); } } } }
Output
Enter Student Name : Raja
Enter Student Address : India
Enter Number of Subjects : 5
Enter Subject 1 Marks : 60
Enter Subject 2 Marks : 80
Enter Subject 3 Marks : 60
Enter Subject 4 Marks : 45
Enter Subject 5 Marks : 60
A. Show Student Details.
B. Show Maximum Marks.
C. Show Total, Average & Result.
X. Exit.
Please select one option : A
Student Name : Raja
Student Address : India
A. Show Student Details.
B. Show Maximum Marks.
C. Show Total, Average & Result.
X. Exit.
Please select one option :
class Example{ public static void main(String args[]){ for(int i = 1 ; i <= 10 ; i++){ if(i == 5){ break; } System.out.println(i); } System.out.println("Hello Java"); } }
Output
1
2
3
4
Hello Java
class Example{ public static void main(String args[]){ for(int i = 1 ; i <= 10 ; i++){ if(i == 5){ return; } System.out.println(i); } System.out.println("Hello Java"); } }
Output
1
2
3
4
class Example{ public static void main(String args[]){ for(int i = 1 ; i <= 10 ; i++){ if(i == 5){ continue; } System.out.println(i); } System.out.println("Hello Java"); } }
Output
1
2
3
4
6
7
8
9
10
Hello Java
import java.util.*; class Example{ public static void main(String args[]){ Scanner input = new Scanner(System.in); int marks = 0; int total = 0; for(int i = 0 ; i < 5 ;){ System.out.print("Enter Subject " + (i + 1) + " Marks : "); marks = input.nextInt(); System.out.println(); if(marks > 100){ System.out.println("Invali Marks..."); System.out.println("Please Re-Enter..."); System.out.println(); continue; }else if(marks < 0){ System.out.println("Invali Marks..."); System.out.println("Please Re-Enter..."); System.out.println(); continue; } total = total + marks; i++; } System.out.println("Total : " + total); } }
Output
Enter Subject 1 Marks : 67
Enter Subject 2 Marks : 89
Enter Subject 3 Marks : 54
Enter Subject 4 Marks : 67
Enter Subject 5 Marks : 89
Total : 366
class Example{ public static void main(String args[]){ String word = "Apple"; String word2 = "Apple"; if(word == word2){ System.out.println("Same"); }else{ System.out.println("Not Same"); } } }
Output
Same
import java.util.*; class Example{ public static void main(String args[]){ Scanner input = new Scanner(System.in); int[] number = new int[5]; for(int i = 0 ; i < 5 ; i++){ System.out.print("Enter Number " + (i + 1) + " : "); number[i] = input.nextInt(); } System.out.println(); System.out.print("All Numbers : [ "); for(int i = 0 ; i < number.length ; i++){ System.out.print(number[i] + " "); } System.out.println("]"); } }
Output
Enter Number 1 : 56
Enter Number 2 : 78
Enter Number 3 : 45
Enter Number 4 : 67
Enter Number 5 : 89
All Numbers : [ 56 78 45 67 89 ]
import java.util.*; class Example{ public static void main(String args[]){ Scanner input = new Scanner(System.in); while(true){ System.out.print("Do you want to add new Student.? (Y/N) : "); String letter = input.next(); System.out.println(); switch(letter){ case "Y": System.out.print("Enter Student Name : "); String name = input.next(); System.out.print("Enter Student Address : "); String address = input.next(); System.out.print("Enter Number of Subjects : "); int number_of_subjects = input.nextInt(); System.out.println(); int[] marks = new int[number_of_subjects]; int max = 0; int total = 0; for(int i = 0 ; i < number_of_subjects ;){ System.out.print("Enter Subject " + (i + 1) + " Marks : "); marks[i] = input.nextInt(); if(marks[i] > 100){ System.out.println("Marks is Grater than 100."); System.out.println(); continue; } if(max < marks[i]){ max = marks[i]; } total = total + marks[i]; i++; } boolean flag = true; while(flag){ System.out.println(); System.out.println("A. Show Student Details."); System.out.println("B. Show All Marks."); System.out.println("C. Show Maximum Marks."); System.out.println("D. Show Total,Average & Result."); System.out.println("X. Exit."); System.out.println(); System.out.print("Please select one option : "); letter = input.next(); System.out.println(); switch(letter){ case "A": System.out.println("Student Name : " + name); System.out.println("Student Address : " + address); break; case "B": for(int i = 0 ; i < marks.length ; i++){ System.out.println("Subject " + (i + 1) + " Marks : " + marks[i]); } break; case "C": System.out.println("Maximum marks : " + max); break; case "D": double average = (double)total / number_of_subjects; String result = "pass"; if(average < 50){ result = "Fail"; } System.out.println("Total : " + total); System.out.println("Average : " + average); System.out.println("Result : " + result); break; case "X": System.out.println("Have a nice day..!"); flag = false; break; default : System.out.println("Please select valid option.."); } } break; case "N": System.out.println("Good bye...."); return; default : System.out.println("Please enter valid letter.."); } System.out.println(); } } }
Output
Do you want to add new Student.? (Y/N) : Y
Enter Student Name : John
Enter Student Address : India
Enter Number of Subjects : 3
Enter Subject 1 Marks : 78
Enter Subject 2 Marks : 89
Enter Subject 3 Marks : 54
A. Show Student Details.
B. Show All Marks.
C. Show Maximum Marks.
D. Show Total,Average & Result.
X. Exit.
Please select one option : A
Student Name : John
Student Address : India
A. Show Student Details.
B. Show All Marks.
C. Show Maximum Marks.
D. Show Total,Average & Result.
X. Exit.
Please select one option : A
Introduction In this section, we will guide you step by step in the development of an image upload registration system in Java using MySQL and JDBC. In the application, users register…
The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…
Creating a responsive login form is a crucial skill for any web developer. In this…
In this tutorial will teach Laravel 12 CRUD API by step. Laravel 10 CRUD Application …
In this lesson we talk about laravel 12 image uploading and display the image step…
In this tutorial will teach Laravel 12 CRUD Application step by step. Laravel 12 CRUD…