Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
"switch" Statements
This section describes 'switch' statement, which is a decision-making statement that executes a portion of the contained statement block by passing control to a sub-statement based on label matching.
What Is "switch" Statement? - A "switch" statement is a decision-making statement that executes a portion of the contained statement block. The executed portion starts from a statement whose label matches a given expression value and ends with the last statement of the block.
Here is the syntax for a "switch" statement.
switch (test_expression) { case label_constant: sub-statement sub-statement ... case label_constant: sub-statement sub-statement ... default: sub-statement sub-statement ... }
When a "switch" statement is executed, the test_expression is evaluated first. The resulting value is then compared with all label_constants. When a match is found, control is passed to the matching label. when no match is found, control is passed to the "default" label (if provided).
Here is a sample program that shows you how to use nested "if-then-else" statements:
/* SwitchStatementTest.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.time.LocalTime; class SwitchStatementTest { public static void main(String[] arg) { java.io.PrintStream out = System.out; LocalTime now = LocalTime.now(); int hour = now.getHour(); // from 0 to 23 out.println("\"switch\" statement:"); int workingHours = 8; switch (hour) { case 9: workingHours--; // starts here when hour == 9 case 10: workingHours--; // starts here when hour == 10 case 11: workingHours--; // starts here when hour == 11 case 12: workingHours--; // starts here when hour == 12 case 13: workingHours--; // starts here when hour == 13 case 14: workingHours--; // starts here when hour == 14 case 15: workingHours--; // starts here when hour == 15 case 16: workingHours--; // starts here when hour == 16 } out.println(" Worked "+workingHours+" full hours with no breaks!"); out.println(" Current time is "+now); } }
If you compile and run the above program, you will see:
herong> java SwitchStatementTest.java "switch" statement: Worked 3 full hours with no breaks! Current time is 12:15:11.702155
As you can see from the output, the program executes the "switch" statement as below:
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
What Is Control Flow Statement
Nested "if-then-else" Statements
Fall-Through Behavior of "switch" Statements
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
Generic Classes and Parameterized Types
Generic Methods and Type Inference
Lambda Expressions and Method References
Java Modules - Java Package Aggregation
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs
Garbage Collection and the gc() Method
Assert Statements and -ea" Option