In this tutorial, we will learn about the case keyword in java programming language
The case is used to label each branch in a switch statement, to execute a block of code in the case statement, which needs to an argument passed in switch statements.
A case block does not have an internal ending point. A break statement is typically used at the end of each case block. This helps to exit the switch statement.
Syntax
The syntax of the case within the switch
int age=value; switch(arg) { case 1: //case statement using case keyword <statements> break; case 2: <statements> break; default: <statements> }
Program
public class switch_keyword{ public static void main (String args[]){ String Days="Sunday"; 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"); } } }
In the above program, the value passed in switch statements, that is compared with every case statements sequence running from initial statements to end statements. Once case statements matched with a switch value the value of branch statement executed.
When the above code is compiled and executed, it will produce the following results
Today is Friday
There are other Java language keywords that are similar to this keyword
Suggested for you
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…
Java program to check odd or even using recursion In this tutorial, we discuss a…