The case keyword in Java programming language
- Home
- Keyword in Java
- The case keyword in Java programming language
- On
- By
- 0 Comment
- Categories: Keyword in Java
The case keyword in Java programming language
The case keyword in Java programming language
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.
Declaration
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> }
Example
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