For nested if constructs, some languages provide constructs similar to

if Bexp1 then C1
  elseif Bexp2 then C2
  elseif Bexpn then Cn
else Cn+1

All I have seen provide elseif. I wonder why not further provide thenif? Is there a need for thenif?


  • 9
    What exactly would this thenif accomplish? – 8bittree Sep 28 '16 at 14:50
  • 1
    More to the point, what would it accomplish that simply putting an if into the then block wouldn't? – Mason Wheeler Sep 28 '16 at 14:53
  • We have this already. It's called "and". Some languages call it "&&". – candied_orange Sep 29 '16 at 4:54

Is there a need for thenif?

Since no mainstream language has such a construct, the answer to your question must be "no".

There again, there's no need for elseif either, as C-like languages get by with else if. So just because it's not needed doesn't mean it couldn't exist.

So should it exist? Since it's purpose would be to assist with writing nested if's, it's worth remembering that the latter often add to both cyclomatic complexity and to poor readability. So if nested if's are to be avoided (as a rule of thumb), then there seems little benefit to adding thenif to any language.

  • 2
    I've never seen an elseif in a C-like language. I've seen them in languages like Python and Ruby where there is a need - you can't use else if because else does not apply on the next statement like in C - it applies until the a block ends with dedent in Python or end in Ruby. This measn that without elif/elsif you'd have to nest the following else ifs. – Idan Arye Sep 28 '16 at 16:47
  • @IdanArye I'm not sure elseif is needed for Python but, as you say, it does make sense for languages that use end etc. – David Arno Sep 28 '16 at 19:06
  • It's needed for python, because otherwise you'll have to indent more and more for each else-if. Compare this to this. – Idan Arye Sep 28 '16 at 19:21
  • @IdanArye, sorry I wasn't clear. Python chooses to have an elif, but it isn't needed for that style of language. This fake python example would work just fine if the language worked that way. – David Arno Sep 28 '16 at 20:52
  • This would require a special treatment to else if, which is not fundamentally different than the elif keyword. – Idan Arye Sep 28 '16 at 21:47

I think such a construct would be more confusing than useful.

Consider this piece of code:

if my_condition then
    if my_other_condition then

If we had a "thenif" keyword, we would be able to shorten that code like this:

if my_condition thenif my_other_condition then

The new version of the code makes it harder to answer important questions at a glance, such as:

  • Which branches might be taken if my_condition is true?
  • What happens if my_other_condition is evaluated and comes out as false?
  • Which occurrence of else corresponds to which occurrence of if?
  • 4
    The only path through that makes any sense is for thenif to act as and, in which case and already covers things. – David Arno Sep 28 '16 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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