Why '&&' and not '&'?

前端 未结 16 1509
别跟我提以往
别跟我提以往 2020-11-27 12:11

Why is && preferable to & and || preferable to |?

I asked someone who\'s been programming for years a

16条回答
  •  自闭症患者
    2020-11-27 13:12

    Because && and || are used for flow control just like if/else are. It isn’t always about conditionals. It is perfectly reasonable to write as a statement, not as an if or a while conditional, the following:

     a() && b() && c() && d();
    

    or even

     w() || x() || y() || z();
    

    It’s not just that it those are easier to type than the equivalent if/else versions; they are also much easier to read and understand.

提交回复
热议问题