0

This might not be a problem but just for peace of mind and I think it good to know how c++ mechanic deal with this keyword. Consider this,

if (condition1)statement1;
else if (condition2)statement2;

we can interprete as,

if (condition1)statement1;
else statement3;

where "statement3" is "if (condition2)statement2;" Which not violate the c++ syntax.

In another case, if we added curly-bracket

if (condition1){
    statement1;
}
else if (condition2){
    statement2;
}

Which equivalent to

if (condition1){
    statement1;
}
else {
    if (condition2){
        statement2;
    }
}

Or, if we add more "else if" condition as following.

if (condition1){
    statement1;
}
else if (condition2){
    statement2;
}
else if (condition3){
    statement3;
}
else{
        statement4;
}

We got

if (condition1){
    statement1;
}
else {
    if (condition2){
        statement2;
    }
    else {
        if (condition3){
            statement3;
        }
        else{
            statement4;
        }
    }
}
M lab
  • 224
  • 1
  • 10
  • There are statements and code, but I am oblivious to the intended purpose. – sweenish Mar 02 '22 at 21:46
  • 7
    `else if` is always an `else`, followed by an `if`. If that's your question - it's hard to understand what "[does] _else if_ exist in c++" means. They exist. – Drew Dormann Mar 02 '22 at 21:46
  • 1
    @DrewDormann but it is interesting that some languages have 'if' 'else' and 'elseif' , python for example. – pm100 Mar 02 '22 at 21:49
  • @pm100 Indeed, many languages have some `elif` concept. I'm not smart enough to know why it's necessary in those languages. – Drew Dormann Mar 02 '22 at 21:50
  • 5
    Python has `elif` because, being indentation sensitive, the alternative would involve unwanted indentation (in C++, you simply don't indent the inside of the `else` and it looks fine). Perl has `elsif` because the `{}` on an `else` block are mandatory in Perl, so you can't simply chain the keywords together. Ruby has `elsif` because, erm, I guess they were copying Perl? I don't see any immediate reason why `else if` *wouldn't* work in Ruby. – Silvio Mayolo Mar 02 '22 at 22:00
  • For the extreme OCD, each subsequent `else if` would be indented another level. But in my experience, most OCD folks (myself included) forgive not indenting those in if/else if/else ladder-logic constructs. – Eljay Mar 02 '22 at 23:11
  • 1
    I just found similar post in stack overfloaw https://stackoverflow.com/questions/24373076/is-else-if-a-single-keyword – M lab Mar 03 '22 at 12:09

1 Answers1

2

To answer your question as asked

Do "else if" exist in c++ or it just only "if" and "else"?

No, else if is not a c++ keyword. See https://en.cppreference.com/w/cpp/language/if

Note the else statement-false part. Then your following if just becomes that statement-false

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
  • Where it becomes a bit interesting for parsers is that the `statement-false` production encompasses both the `if (condition2) statement-true2; else statement-false2;` and not just the `if`. And gives a sometimes surprising meaning to the tricky sequence `if (condition1) if (condition2) statement-true2; else statement-false2;` Use braces! – Ben Voigt Mar 02 '22 at 22:12