0% found this document useful (0 votes)
7 views10 pages

Java Switch Case Explained with Examples

Uploaded by

kiranverma2127
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Java Switch Case Explained with Examples

Uploaded by

kiranverma2127
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Switch Case in Java

Q.1 What is a Switch Case?

Ans A switch case in Java is a decision-making statement. It is used when you have to
choose one option from many possible options. It works like a series of if–else statements
but is cleaner and easier to read.

Syntax of switch case

switch(expression)

​ case value1:

// statements

break;

​ case value2:

// statements

break;

​ case value3:

// statements

break;

default:

// statements

}
Explanation of each part

switch(expression):

The expression is evaluated once. Its value is compared with each case value.

case value:

If the expression matches this value, the statements inside this case run.

break:

It stops the switch statement after one case is executed.

(If you don’t write break, the next cases will also run — this is called fall-through.)

default:

It runs when no case matches.

(It’s like the else part of if–else.)

Important Points to Remember

1. The expression in a switch must be of byte, short, int, char, or String type.

2. Break is used to stop the execution after a case is matched.

3. Default runs if no case matches.

4. We can have multiple cases without break to run together.


5. It makes the code easier to read than multiple if–else statements.

[Link] to print weekdays on the basis of user’s choice.

// program to print weekdays on the basis of user’s choice.

class WeekDay

public static void main(int day)

switch(day)

case 1:

[Link]("Monday");

break;

case 2:

[Link]("Tuesday");

break;

case 3:

[Link]("Wednesday");

break;

case 4:

[Link]("Thursday");

break;

case 5:
[Link]("Friday");

break;

case 6:

[Link]("Saturday");

break;

case 7:

[Link]("Sunday");

break;

default:

[Link]("Invalid day!");

}// switch closed

​ } // main closed

} // class closed

[Link] to print month name on the basis of user’s choice that displays the name of
the month, based on the value of month, using the switch statement.

class mth

​ public static void main(int month)

​ String nm;

​ switch (month) {

​ case 1: nm = "January";

​ break;
​ case 2: nm = "February";

​ break;

​ case 3: nm = "March";

​ break;

​ case 4: nm = "April";

​ break;

​ case 5: nm = "May";

​ break;

​ case 6: nm = "June";

​ break;

​ case 7: nm = "July";

​ break;

​ case 8: nm = "August";

​ break;

​ case 9: nm = "September";

​ break;

​ case 10: nm = "October";

​ break;

​ case 11: nm = "November";

​ break;

​ case 12: nm = "December";

​ break;

​ default: nm = "Invalid month";

​ break;

​ }// switch closed

​ [Link](nm);
​ }// main closed

}// class closed

[Link] to print the grade as per the table given below:

CASE GRADE

A Excellent

B Distinction

C Well Done

F Better try again

Default Invalid Grade

class test

public static void main(String grade)

switch(grade)

​ {

case "A" :

[Link]("Excellent!");
break;

case "B" :

[Link]("Distinction");

break;

case "C" :

[Link]("Well done");

break;

case "D" :

[Link]("You passed");

case "F" :

[Link]("Better try again");

break;

default :

[Link]("Invalid grade");

}//switch closed

[Link]("Your grade is " + grade);

}//main closed

}//class closed

[Link] to print the vowel as entered by the user.

// program to print the vowel as entered by the user.

class vowel

public static void main(char code)


{

switch(code)

​ {

case 'A' :

case 'a' :

[Link]("Vowel entered is " +code);

break;

case 'E' :

case 'e' :

[Link]("Vowel entered is " +code);

break;

case 'I' :

case 'i' :

[Link]("Vowel entered is " +code);

break;

case 'O' :

case 'o' :

[Link]("Vowel entered is " +code);

break;

case 'U' :

case 'u' :

[Link]("Vowel entered is " +code);

break;

default :

[Link](code + "is not a Vowel");


}//switch closed

}//main closed

}//class closed

[Link] to print the rainbow colour name.

// program to print rainbow colour

class rainbow

public static void main(char code)

switch(code)

​ {

case 'V' :

case 'v' :

[Link]("Voilet");

break;

case 'I' :

case 'i' :

[Link]("Indigo");

break;

case 'B' :

case 'b' :

[Link]("Blue");

break;
case 'G' :

case 'g' :

[Link]("Green");

break;

case 'Y' :

case 'y' :

[Link]("Yellow");

break;

case 'O' :

case 'o' :

[Link]("Orange");

break;

case 'R' :

case 'r' :

[Link]("Red");

break;

default :

[Link]("Not a Rainbow colour");

}//switch closed

}//main closed

}//class closed

You might also like