We will learn about the switch keyword in Java programming language
The switch is always used with case keyword.
the case is used with switch statement gives you the option to check a range of value for your variable. The switch statement invokes one statement from multiple conditions.
Syntax
Syntax of switch in Java
switch(expression){//switch keyword with expression case value1; // some code here break;//it is optional case value2; // some code here break;//it is optional ..................... ...................... default;// this statement is executed if all cases are not matched }
Example
Program
public class switch_keyword{ public static void main (String args[]){ String Days="Friday"; switch(Days){ case "Sunday": //case statement using case keyword System.out.println("Today Sunday"); break; case "Monday": System.out.println("Today Monday"); break; case "Tuesday": System.out.println("Today Tuesday"); break; case "Wednesday": System.out.println("Today Tuesday"); break; case "Thursday": System.out.println("Today Thursday"); break; case "Friday": System.out.println("Today Friday"); break; case "Saturday": System.out.println("Today Saturday"); break; default : System.out.println("This is not a day"); } } }
When the above code is compiled and executed, it will produce the following results
Today Friday
There are other Java language keywords that are similar to this keyword
static keyword in Java language
extends keyword in Java language
Suggested for you
Usage of final keyword in Java
Finally keyword in Java language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…