0

While i'm learning basics of CS. i saw a if statment not use "else" more than one time in every if statment! when he have 3 options he write: if () {} else { if () {} else {} }!

why don't we write: if () {} else {} else {}?!

2
  • 2
    How would the engine know which else to follow?
    – Clive
    Oct 24, 2015 at 9:36
  • I think you want else if
    – Tushar
    Oct 24, 2015 at 9:39

2 Answers 2

0

First of all, that's a syntax error and secondly if you just have two elses anyway which you both want executing, you could just put them together.

2
  • syntax not error.. look her: clipular.com/c/… Oct 24, 2015 at 9:52
  • That's not the same thing, there's always an if and then an else not else else.
    – Olavi Sau
    Oct 24, 2015 at 9:53
0

The JavaScript language supports the following conditional constructs.

if, else if, else

var myName = "Darren";

if (myName === "Darren") {
   alert("My name is Darren");
}
else if (myName === "user3860868") {
   alert("My name is user3860868");
}
else {
   alert("My name is not Darren or user3860868");
}

The first statement checks if the myName variable is Darren, if it matches an alert box is shown with my name, if it does not, it then checks if the myName variable is equal to user3860868, if the name does not match again it finally falls into the else statement and displays the last alert box.

You can have multiple else if statements but only one else statement.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.