Jump to content

Syntax problem with Switch statement


smormando

Recommended Posts

I'm new to Javascript, so bear with me. Maybe this isn't even possible, but I'd like to be able to do the following:switch (whatever){case (whatever < 5): blah blah blah;case (whatever>10): blah blah blah;}Does the case have to be a specific value, or can it be an expression? If this is possible, what's the syntax here?Thanks!Steve

Link to comment
Share on other sites

The switch-case wouldn't find (whatever < 5) because that's the same as saying case false or case true.Switch-case only searches for certain values.The if() command is suitable for what you're trying to do.

Link to comment
Share on other sites

I agree with Ingolme that if-else is the way to go in your situation. If you want to stick with the switch, however, you could do it like this:

switch(whatever){	case 0:	case 1:	case 2:	case 3:	case 4:		// blah blah blah		break;	case 5: 	case 6:	case 7:	case 8:	case 9:		// blah blah blah		break;	default:		// blah blah blah		break;}

That's pretty long-winded (and would only work as expected for positive numbers) when the following would also work:

if(whatever < 5){	// blah blah blah}else if (whatever > 10){	// blah blah blah}else{	// blah blah blah}

Link to comment
Share on other sites

Your code will work, just make a small adjustmentvar whatever = 11switch (true){ case (whatever < 5): //test your expression here WScript.Echo("blah blah blah"); break; //don't forget to break case (whatever > 10): WScript.Echo("blah blah blah blah"); break; default: WScript.Echo("default"); break;}

Link to comment
Share on other sites

That's partially right. Using a value of true isn't going to force the switch statement to execute, it's always going to execute, the value that you switch is the value that you test all of your cases against. So switch(true) would test cases for the value true. It's the same as doing this:

switch (true){  case true:	echo "this will always be printed";	break;  case false:	echo "this will never be printed";	break;  default:	echo "there's not even a third boolean value, there's no need for default";}

Likewise,

switch (false){  case true:	echo "this will never be printed";	break;  case false:	echo "this will always be printed";	break;}

You could put a boolean expression inside each case, but it's probably going to be better if you're doing boolean checks to just use an if statement. Switch statements are typically used if you're checking if a single variable is one of a range of values. If you use a switch(true) block it's basically the same thing as an if/else block, it will just execute the first true case it finds. The only additional thing a switch gives over if/else is if you leave out the break statements the switch will fall through to later cases, an if/else structure won't do that.You can tell why the original code doesn't work if you replace the variables with their types.switch (number){case (boolean):blah blah blah;case (boolean):blah blah blah;}You're trying to compare a number with boolean values.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...