-3

W

enter image description here

CAN YOU PLEASE EXPLAIN IN DETAIL. Why are we not checking both condition in "and" of if statement , and in checking both condition in "or" of If condition?

Why in first question x= 11, y=20, is printed. While in second question x=11, y=21, is printed.

  • Not sure if this is a SO question, that is some basic programming stuff you can read in any basic book / prob blogposts, too. Look up short circuit evaluation – Leviathan Jul 15 '20 at 08:08
  • 2
    Btw. please, don't post code as images. (See [ask]: _**DO NOT post images of code, data, error messages, etc.**_ (emphasize not mine).) – Scheff's Cat Jul 15 '20 at 08:14
  • Ok. I will keep in mind for future doubt. – Anchal Ajay Jul 15 '20 at 12:17

1 Answers1

1

when you have an expression like A || B and A is true, there is no need to evaluate B. The outcome of the whole expression will be true. Something similar goes for A && B.

That means that you must not put assignments or the like in your expression in the way you do, since they can be short circuited. So the ++y is dangerous stuff: you cannot rely on it being executed.

ruud
  • 743
  • 13
  • 22