switch condition checking

switch is used when it is expected that one of the several condition is going to be true.

Syntax

switch ( variable ){
	case  value :
		// code block 
	case  value : 
		// code block
	case value :
		// code block	
}
There is a list of food to be served based on different weekdays in a hostel canteen. It is clear that one of the food type is going to return for any input weekday, so it is better to use switch condition to match the current day with the weekday and return the food type of the day.

Example

We are matching month with first three months ( Jan, Feb , March ) of any year.
int month=3;	
	switch ( month) {
		case 1:
			System.out.println("Jan : " + month);
			break;
		case 2:	
			System.out.println("Feb : " + month);
			break;
		case 3:	
			System.out.println("March: " + month);
			break;	
		
	}
If our month is not within these three months then a different message has to be returned. For this we will add default command.
int month=5;	
switch ( month) {
	case 1:
		System.out.println("Jan : " + month);
		break;
	case 2:	
		System.out.println("Feb : " + month);
		break;
	case 3:	
		System.out.println("March: " + month);
		break;	
	default:	
		System.out.println("No matching months");
	}

Break and default

switch with break and default
Use of break; is optional. Once the break; statement is encountered the execution comes out of the switch block without further checking of other case conditions.

Default :

When none of the case condition returns true, the code within the default is executed.

Should we use break; inside default code block ?

Since the last code block is within default and after that the switch statement will end, so there is no point in keeping a break statement inside default code block. It is not going to make any difference in our sequence of execution.

switch with string variable

We have three employees. One of the names will be stored in string variable and based on their name output saying the job details will be shown. If name is not found then default messaging saying No employee found will be displayed.
var emp_name="Rama";	
switch ( emp_name) {
	case "Rabi":
		System.out.println("Rabi is a Manager");
		break;
	case "Raju":	
		System.out.println("Raju is a Driver");
		break;
	case "Rama":	
		System.out.println("Rama is an officer");
		break;	
	default:	
		System.out.println("No Employee found");
		}
Output is here
Rama is an officer
Key points
  1. Case values must be unique and duplicate case values will generate error.
  2. Data type of switch and case must be same.
  3. case expressions must be constant expressions, we can’t use any variable.

switch with multiple condition matching

We can’t use any OR or AND logical operators within case.

If Month is between April and Jun then output should be First Quarter. If Month is between July and Sep then 2nd Quarter. April ( 04) – Jun ( 06): First Quarter
July(07) - Sep (09) : Second Quatter
Oct (10) - Dec (12) : Third Quater
Jan (01) - March (03) : Fourth Quarter
int month=8;
		
switch ( month) {
	case 4:
	case 5:
	case 6:	
		System.out.println("First Quarter");
		break;
	case 7:
	case 8:
	case 9:	
		System.out.println("Second Quarter");
		break;
	case 10:
	case 11:
	case 12:	
		System.out.println("Third Quarter");
		break;	
	case 1:
	case 2:
	case 3:	
		System.out.println("Fourth Quarter");
		break;	
	default:	
		System.out.println("Wrong Month Number");
	}
Output
Second Quarter

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer