Programming Fundamentals · Interview question

In an if / else if / else if / else chain, can multiple branches run?

A strong answer

No. Once any branch's condition matches, its body runs and the rest of the chain is skipped, even if a later condition would also be true. This makes chains efficient (you stop testing as soon as you find a match) and makes the branches mutually exclusive by design. If you wrote four independent ifs with no else, each would test independently and you could fall into multiple bodies. That's a different shape entirely, and it's almost always a mistake when the intent is "classify this value into one bucket."

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Conditionals

How a program makes decisions. if, else if, and else, with the dangling-else trap and the ternary shortcut for one-line choices.

More Conditionals questions

Browse all 472 interview questions