0

Which is better to use && or ||? For example if I need to check if input is between 5 and 27 and divisible by 3, is it better to check as

if((num < 5) || (num > 27) || (num%3 != 0))
{
//skip
}
else
{
//operations
}

OR

if((num >= 5) && (num <= 27) && (num%3 == 0)
{
//operations
}
Kevin
  • 566
  • 2
  • 5
  • 13
  • 4
    Don't know why you're concerned about `||` and `&&` when you've got a `%` in there. – yamafontes Dec 18 '13 at 05:57
  • 1
    There is no performance difference. Choose the option that reads better for you. – Michael Petrotta Dec 18 '13 at 05:57
  • Unrelated to your specific question, but those two aren't equivalent. One checks for 5-27 inclusive, while the other checks for exclusive. – Dusty Dec 18 '13 at 05:58
  • no use. what you trying to accomplish? – Indranil.Bharambe Dec 18 '13 at 05:58
  • @MichaelPetrotta :Thank you. It was out of curiosity if checking for positive condition is better or for a negative condition. – Kevin Dec 18 '13 at 06:00
  • Both seem to have about the same number of operations.. so either way it doesn't seem to make any difference . To see a more efficient way to check whether an integer is between 2 integers, see [this question](http://stackoverflow.com/questions/17095324/fastest-way-in-c-to-determine-if-an-integer-is-between-two-integers-inclusive) – nedR Dec 18 '13 at 06:12

2 Answers2

0

Unless you are doing this in an extremely tight loop, you will never notice any difference whatsoever.

Most C compilers will rewrite the expression to take advantage of short-circuit evaluation anyhow, so for those compilers the order is irrelevant (for C++ there are exceptions).

If your compiler cannot optimize the Boolean math (very unlikely in a modern compiler), the second form is better because it allows for some terms to be skipped.

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • “the second form is better because it allows for some terms to be skipped.” You mean as opposed to the first form? What are the effects of `1 || printf("executed\n")` ? – Pascal Cuoq Dec 18 '13 at 06:45
  • @PascalCuoq: If the two forms are not compiled to identical code, the compiler is not optimizing. If it is not optimizing, each term of the || will be evaluated. Your `1 || printf("executed\n")` is not the same because `printf()` can have side effects (indeed it does). Short-circuit evaluation is only done for primitive types (per the spec) because they are known not to have side effects. – Eric J. Dec 18 '13 at 08:02
0

From the CPU's perspective, || and && will more likely than not take exactly the same amount of time to complete. From the compiler's perspective, transforming between the two will be a trivial operation -- this is certainly a choice it will make correctly for you. Use whichever is clearest to you.

It is far more important for you to make good use of C's short circuiting to ensure that the branch is determined in as few executed instructions as possible.

Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • Wouldn't C 's short circuiting be more effective if i used ||? That way || would be a better option right? Considering my intention is it to check if it lies within the range? Am i missing something? – Kevin Dec 18 '13 at 06:06
  • @Kevin you are missing the simple solution of compiling both versions to assembly with basic optimizations enabled and observing - how they basically translate to the same thing even when they are not already transformed one into the other - how you have not said anything about the expected result when you chose to write one or the other. In particular, both versions short-circuit at the same point for the same values. – Pascal Cuoq Dec 18 '13 at 06:49