Delirium tremens
Delirium tremens

Reputation: 4739

Explain the following from Accelerated C++ please

I don't understand the following excerpt from Accelerated C++:

Starting at

Because || is left-associative, and because of the relative precedence of ||,== ,and -,

r == 0 || r == rows - 1 || c == 0 || c == cols - 1 means the same as it would if we were to place all of its subexpressions in parentheses:

((r == 0 || r == (rows - 1)) || c == 0) || c == (cols - 1)

and going until

Otherwise, it does something else, which we must now define.

I don't understand this. How would you tell me the same with your own words?

Upvotes: 0

Views: 387

Answers (1)

anon
anon

Reputation:

If you have a series of conditions you want to evaluate, let's say "if x is 1 or y is 2, then call function foo()" then there is no point in performing the second test (y is 2) if you already know that x is 1. The || operator works like that:

i( x == 1 || y == 2 ) {
     foo();
}

The expression y == 2 will not be evaluated if x == 1, because it is not necessary. This is called short circuited evaluation, and can save a lot of time if evaluation is expensive.

If this isn't what you are asking about, please make your question more explicit.

Upvotes: 5

Related Questions