Waqas Shah
Waqas Shah

Reputation: 115

Please check if I get and write the switch statement right?

Switch statement is also a conditional statement like if and else if statement but in more reliable way. The structure:   

switch (conditional variable) {    
case condition 1:    
     conditional code    
     break;    
case condition 2:    
     conditional code    
     break;    
case condition 3:    
     conditional code    
     break;    
default:    
     conditional code 

real code:

var baby = "crying"
switch (baby) {    
    case "sleeping":    
        alert ("Green light")    
        break;    
    case "playing":    
        alert ("Yellow light")    
        break;    
    case "crying":    
        alert ("Red light")    
        break;    
    default:    
        ("no lights")    
}

Upvotes: 0

Views: 36

Answers (1)

Ravi Kumar
Ravi Kumar

Reputation: 36

You missed the semicolon after alert and also alert missing on last statement :

var baby = "crying"
switch (baby) {    
case "sleeping":    
    alert ("Green light");   
    break;    
case "playing":    
    alert ("Yellow light");    
    break;    
case "crying":    
    alert ("Red light");   
    break;    
default:    
    alert ("no lights");    
}

Upvotes: 1

Related Questions