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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

switch case

Status
Not open for further replies.

Nanda

Programmer
Nov 14, 2000
104
US
How do you specify "OR" condition using switch case..means

switch obj{
case "Project" || "Library":
alert("do something");
default:
alert("do nothing");
}

thanks
 
hiya,

You should use something like this:

Code:
switch (obj) {
    case "Project":
         alert("do something");
         break;
    case "Library":
         alert("do something");
         break;
    default:
         alert("do nothing");
}

Hope this helps?

Gtz,

Kristof
 
Hello Nanda!

Unfortunately you can't do that. The idea of swith to compare only one value in time. You may do it like this:
switch obj{
case "Project"
alert("do something");
case "Library":
alert("do something");
default:
alert("do nothing");
}


But in your example I would use simple if statement:
if ("Project" || "Library") { alert("do something"); }
else {alert("do nothing");}

Good Luck!
 
thanks everybody.

I have multiple cases, so have to use Switch instead of IF...I was expecting to do "OR" in switch case :-(. anyway.

thanks again.
 
i think this wuld also do the job:

switch (obj) {
case "Project":
case "Library":
case "Whatever":{
alert("do something");
break;}
default:{
alert("do nothing");}
}

didnt tryed, but it works in C++.. so, i thought it might be workin here.. Victor
 
yeah, it works;
check this:
<html><head><title>test</title>
<script>
function aa(obj){
switch (obj) {
case &quot;Project&quot;:
case &quot;Library&quot;:
case &quot;Whatever&quot;:{
alert(&quot;do something&quot;);
break;}
default:{
alert(&quot;do nothing&quot;);}
}
}
</script>
</head>
<body bgcolor=&quot;#FFFFFF&quot;>
<form name=&quot;myne&quot;>
<input type=text name=txt>
<input type=button value=run onclick=&quot;aa(document.forms.myne.txt.value)&quot;>
</form>
</body>
</html>

enjoy.. :) Victor
 
Vituz,
is it really necessary to use { (brackets) in a switch case?? as far as I know it will do to write like this:
Code:
switch (obj) {
    case &quot;Project&quot;:
    case &quot;Library&quot;:
    case &quot;Whatever&quot;:
         alert(&quot;do something&quot;);
         break;
    default:
         alert(&quot;do nothing&quot;);
         break;
}
instead of:
Code:
&quot;switch (obj) {
    case &quot;Project&quot;:
    case &quot;Library&quot;:
    case &quot;Whatever&quot;:{
         alert(&quot;do something&quot;);
         break;}
    default:{
         alert(&quot;do nothing&quot;);
         break;}
}&quot;

As far as I know, a switch case is broken with break; and that { (brackets) are not necessary!
My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
yeah, but i was learned to do that that way, dont remember when: may be on 1st year of university, may be on the second, may be in school..
& i'll keep doin that that way.. :)

Victor
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top