John Cooper
John Cooper

Reputation: 7651

Switch case - else condition

<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?

Upvotes: 4

Views: 66679

Answers (6)

self-taught301
self-taught301

Reputation: 31

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

Use a default code block as seen below. Anything that doesn't match the cases will fallback to the default one.

switch(expression) {
      case x:
        // code block
        break;
      case y:
        // code block
        break;
      default:
        // code block
    }

How this works:

  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.
  • If there is no match, the default code block is executed.

Upvotes: 1

adarshr
adarshr

Reputation: 62603

You can do something like:

var something = 0;

switch (theDay) {
    case 5:
      document.write("Finally Friday");
      something = 15;
      break;
    case 6:
      document.write("Super Saturday");
      something = 16;
      break;
    case 0:
      document.write("Sleepy Sunday");
      something = 20;
      break;
    default:
      document.write("I'm looking forward to this weekend!");
}


switch (something) {
    case 15: document.write("Not Friday");   break;
    case 16: document.write("Not Saturday"); break;
    case 20: document.write("Not Sunday");   break;
    default: document.write("Nothing");      break;
}

Upvotes: 0

Mutation Person
Mutation Person

Reputation: 30520

A standard if/else would be the best way to achieve this. I wonder why you want to do it without.

That said, It's not very elegant, but you could try adding the following to the top of your switch statement:

case 0:
case 1:
case 2:
case 3:
case 4:
case 6:
   document.write("Finally Something");

Giving you:

switch (theDay)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 6:
   document.write("Finally Something");
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!");
}

Upvotes: 0

nnnnnn
nnnnnn

Reputation: 150080

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.

Upvotes: 5

Matthew Egan
Matthew Egan

Reputation: 87

Overly complicated answers.

Simplify.

var switchElse = true;
switch (CHECK_SOMETHING) 
{
    case "SOME_VALUE":
        ...DO SOMETHING...
        switchElse = false;
        break;
    default:
}

if (switchElse) 
{
        ...DO ELSE...
}

The only solution that can be formulated without a compare. USE "DEFAULT" PATTERN

var myValue = "Friday"
switch (CHECK_SOMETHING)
{
    case "SOME_VALUE": 
         myValue = "Some Other Day";
    default:
}

Upvotes: -1

techfoobar
techfoobar

Reputation: 66693

Its like switch does a jump to the matching case, if it doesn't match it will jump to what matches. The answer to your question "If the case 5 does not execute, can i do something else in that place?" is No, because it never reaches that case at all. It jumps to the next matching case or default.

Upvotes: 1

Related Questions