Site icon Codeforcoding

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

try keyword in Java

Catch keyword in Java

implements keyword in java

public Key words in Java

static in Java language

Volatile  in Java

Transient  in Java

Package  in Java

class  in Java

boolean  in Java

break  in Java

do  in Java

double  in Java

This  in Java

final  in Java

switch  in Java

interface  in java

extends  in Java language

 

Suggested for you

 Java language Keywords

C language Keywords

 Python language Keywords

keywords in C language

Usage of final  in Java

Usage of this  in Java

Finally  in Java language

 

 

The switch keyword in Java programming language
The continue keyword in Java programming language
Exit mobile version