Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jade's syntax support a switch statement?

I tried this in jade served by express but got "unexpected identifier" as an error.

- switch(myvar)
    - case: "0"
        span First Case
            break
    - case: "2"
        span Second Case
            break
    - case: "3"
        span Third Case
            break
    - case: "4"
        span Fourth Case
            break

I was curious as to what is the syntax for a switch statement, if there is one.

Update: Jade, not express.

like image 762
Chris Abrams Avatar asked Apr 01 '12 23:04

Chris Abrams


People also ask

What is the syntax of switch statement?

A typical syntax involves: the first select , followed by an expression which is often referred to as the control expression or control variable of the switch statement. subsequent lines defining the actual cases (the values), with corresponding sequences of statements for execution when a match occurs.

Which of the following expressions use switch statement?

The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

Which of the following types Cannot appear as a switch expression type?

1) The expression used in switch must be integral type ( int, char and enum). Any other type of expression is not allowed.

Which type of statement is switch?

Switch case statements follow a selection-control mechanism and allow a value to change control of execution. They are a substitute for long if statements that compare a variable to several integral values. The switch statement is a multiway branch statement.


1 Answers

EDIT

This question is apparently about Jade instead.

But the answer is still yes :).

But it's called case:

From the docs

case friends
    when 0
        p you have no friends
    when 1 
        p you have a friend
    default
        p you have #{friends} friends

Javascript has a switch statement.

switch(variable){
    case 1:
        // do something
        break;
    case 2:
        // do something else
        break;
    // and so forth
    default: 
        // do something if nothing
        break;
 }

Being that Express.js runs in Node.js which is just JavaScript -- yes. Express has a switch statement since JavaScript has a switch statement. (Even coffeescript has a switch that "compiles" down to a JavaScript switch statement.)

MDN reference: switch statement

It looks like your syntax is messed up there -- what are those "-" characters? You're also missing the : from the end of each case statement, and you're not breaking after each case which means the code for ALL cases will ALWAYS run regardless of the condition.

like image 116
tkone Avatar answered Nov 01 '22 13:11

tkone