-1

For instance if I have an if statement as follows:

if(returnsFalse() && timeConsumingFunction()){
    //do whatever
}

Will the program run the time consuming function or will it realise that the if evaluates as false after the "returnsFalse()" function returns its value? How does this work in different languages? Mainly interested in java and c.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Kuranes
  • 62
  • 1
  • 7

1 Answers1

3

No if you use && it will not continue on if the first statement is false.(Java) If you use & it will evaluate all expressions.

brso05
  • 13,142
  • 2
  • 21
  • 40
  • 2
    And for completeness, if you use a single &, it will run both statements (where each will evaluate to true or false) and then it will and them. So don't mix those up! – Miquel Oct 28 '14 at 13:10
  • 2
    This is called *short-circuit evaluation* by the way – meskobalazs Oct 28 '14 at 13:10