Break Statement
The Java break statement is used to exit a loop immediately and transfer the control to the next statement after the loop. It has two main uses: when encountered inside a loop, it instantly terminates the loop, and in a switch statement, it is used to exit a specific case.
Syntax:
Flow
Diagram
Source Code:
import java.util.Scanner;
public class BreakStatement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
// Display menu
System.out.println("\n==== MATH CALCULATOR ====");
System.out.println("1. Addition (+)");
System.out.println("2. Subtraction (-)");
System.out.println("3. Multiplication (*)");
System.out.println("4. Division (/)");
System.out.println("5. Exit");
System.out.print("Choose an operation: ");
int choice = scanner.nextInt();
if (choice == 5) {
System.out.println(" Exiting... Goodbye!");
break; // Exit the loop
}
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
double result = 0;
boolean validOperation = true;
switch (choice) {
case 1:
result = num1 + num2;
System.out.println(" + Result: " + result);
break;
case 2:
result = num1 - num2;
System.out.println(" - Result: " + result);
break;
case 3:
result = num1 * num2;
System.out.println(" * Result: " + result);
break;
case 4:
if (num2 == 0) {
System.out.println(" Error: Division by zero is not allowed!");
validOperation = false;
} else {
result = num1 / num2;
System.out.println(" / Result: " + result);
}
break;
default:
System.out.println(" Invalid choice! Please choose a valid option.");
validOperation = false;
}
if (validOperation) {
System.out.println(" Calculation complete!");
}
}
scanner.close();
}
}Result:
Watch the video:
Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

