What is happening in an “If(..||..)” and “If(…&&…)” construct internally?

后端 未结 6 1988
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 18:29

I was just wondering what happens inside of an \"if OR\" and \"if AND\". I have a feeling that it\'s just syntactic sugar to use &

6条回答
  •  轮回少年
    2020-12-15 19:00

    For those who read C# better than assembly, the real internal forms are closer to:

    if(a) goto yes;
    if(b) goto yes;
    if(c) goto yes;
    goto no;
    yes:  DoSomething();
    goto done;
    no:   /* if there were an else it would go here */;
    done: ;
    

    for

    if(a || b || c)
      DoSomething();
    

    and

    if(!a) goto no;
    if(!b) goto no;
    if(!c) goto no;
    yes:  DoSomething();
    goto done;
    no:   /* if there were an else it would go here */;
    done: ;
    

    for

    if(a && b && c)
      DoSomething();
    

    That's because the actual instructions are conditional branches -- in the internal form it is not possible for an if to be associated with a block, a nested if, or actually anything except a goto.

提交回复
热议问题