Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case - else condition

Tags:

javascript

<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.

var d = new Date();
var theDay = d.getDay();
switch (theDay)
{
case 5:
  document.write("Finally Friday");
  break;
case 6:
  document.write("Super Saturday");
  break;
case 0:
  document.write("Sleepy Sunday");
  break;
default:
  document.write("I'm looking forward to this weekend!");
}
</script>

If the theDay = 5, then we display Finally Friday. I want if theDay !=5, then display 'Finally Something'.. similarly for others too...

Is it possible without an If/else condition. If the case 5 does not execute, can i do something else in that place?

like image 442
John Cooper Avatar asked Sep 11 '25 20:09

John Cooper


1 Answers

Is it possible without an If/else condition. If the case 5 does not execute, can i do something else in that place?

No.

The switch statement will execute the first matching case, and then keep going (ignoring all further case labels) until it gets to either a break statement or the end of the switch block - but even though you can "fall through" to subsequent cases by omitting the break statement the switch does not provide any mechanism to say "do something when this case isn't matched/executed, but also keep trying to look for a matching case".

What you are describing would normally be done with a series of if/else statements:

var d = new Date(),
    theDay=d.getDay(),
    matched = false;

if (theDay === 5) {
  matched = true;
  document.write("Finally Friday");
} else {
  // your != 5 case here
}
if (theDay === 6) {
  matched = true;
  document.write("Super Saturday");
} else {
  // your != 6 case here
}
if (theDay === 0) {
  matched = true;
  document.write("Sleepy Sunday");
} else {
  // your != 0 case here
}

// default when none matched:
if (!matched) {
  document.write("I'm looking forward to this weekend!");
}

Note that I've added a matched flag to allow the default to work. And note that there are no else if statements because you need every if/else pair to execute.

If you are really determined to use a switch statement you could do something silly like the following:

var d = new Date(),
    theDay = d.getDay(),
    c,
    cases = { // pre-list all the "not" cases
             "!5" : true,
             "!6" : true,
             "!0" : true
            };

// add case for theDay and remove the "not" case for theDay (if there is one)
cases[theDay] = true;
if (cases["!" + theDay])
   delete cases["!" + theDay];

for (c in cases) {
   switch(c) {  
      case "5":
         document.write("Finally Friday");
         break;
      case "!5":
         document.write("Finally Something");
         break;
      case "6":
         document.write("Super Saturday");
         break;
      case "!6":
         document.write("Finally Something - but not 6");
         break;
      case "0":
         document.write("Sleepy Sunday");
         break;
      case "!0":
         document.write("Finally Something - but not 0");
         break;
      default:
         document.write("I'm looking forward to this weekend!");
   }
}

If you need the cases to execute in a specific order use an array rather than an object.

like image 107
nnnnnn Avatar answered Sep 13 '25 08:09

nnnnnn