Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about switch statement 1

Status
Not open for further replies.

ttomasko

Programmer
Aug 6, 2008
28
Hello,
Is it possible to use the switch statement with inequalities?

For instance, the following does not work.

Tom

Code:
var x = 4;
switch(x){
		case <5:
		alert("do something");
		break;
		case 5:
		alert("do something more");		
		break;
		case >5:
		alert("do something really big");
		break;
	}//end switch
 
No, it's not possible. Why not using the if statement?
 
[tt]var x = 4;
switch(true){
case x<5:
alert("do something");
break;
case x==5:
alert("do something more");
break;
case x>5:
alert("do something really big");
break;
}//end switch
[/tt]
ps: In .net, constant expression is enforced in the case conditional to avoid possible side-effect and this kind of construction will no longer allowed there. Just a note.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top