Categories: Keyword in Java

The switch keyword in Java programming language

The switch keyword in Java programming language

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

try keyword in Java

Catch keyword in Java

implements keyword in java

public Key words in Java

static keyword in Java language

Volatile Keyword in Java

Transient keyword in Java

Package keyword in Java

class keyword in Java

boolean keyword in Java

break keyword in Java

do keyword in Java

double keyword in Java

This keyword in Java

final keyword in Java

interface keyword in java

extends keyword in Java language

 

Suggested for you

 Java language Keywords

C language Keywords

 Python language Keywords

keywords in C language

Usage of final keyword in Java

Usage of this keyword in Java

Finally keyword in Java language

 

 

 

The else keyword in Java programming language
The case keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago