short circuit evaluation can shorten the compile time, so i learned that C, C++ is using that way. But are there any situations that short-circuit evaluation ruining the code?
Short circuiting does not shorten the compile time of the code. (by any meaningful amounts, at least)
It might be shortening the runtime, but it's not its intended purpose.
The purpose of short circuiting is to do the minimal amount of work in order to check a certain condition.
For example:
When using &&
(as opposed to a single &
), the right operand won't be evaluated if the left one is false. This is due to the nature of a logical and
operation: if at least one of the operands is false, the whole expression is false.
Technically, it will shorten the runtime if the condition fails early, but the amount of saved runtime is dependent on the expressions inside each operand.
Anyway, it's incorrect to use &&
because it's "faster" than &
. You should use either when appropriate.
&
is used for bitwise operations.