Home > Software engineering >  Would else if be possible in some cases even if it isn't explicitly defined?
Would else if be possible in some cases even if it isn't explicitly defined?

Time:10-04

While writing some code, I came across a question. If a hypothetical (C-style) programming language supported lossy curly braces, if and else but not else if (explicitly). Also it should it situations like

if(a)
if(b)
f();
else
g();

group statements like

if(a){
  if(b){
    f();
  }else{
    g();
  }
}

. So, (curly braces are unnecessary for this example)

if(a){
  f1();
}else if(b){
  f2();
}else if(c){
  f3();
}else{
  f4();
}

would be grouped like

if(a){
  f1();
}else{
  if(b){
    f2();
  }else{
    if(c){
      f3();
    }else{
      f4();
    }
  }
}

. I would like to check whether this would mean that in such hypothetical language else if would behave like in C. Thanks for reaching out.

Edit: To clarify, lossy curly brackets(I thought that it's spelled loosy) are something from a compiler warning from a while ago, when I wrote something like

if(a)
    if(b)
        f1();
    else
        f2();

.

CodePudding user response:

In the C programming language:

else if(condition){
  statement;
}

Is strictly equivalent to:

else{
  if(condition){
    statement;
  }
}
  • Related