dmkathayat
dmkathayat

Reputation: 483

How is a conditional implemented in C/C++?

I am curious to know how a conditional statement such as an "if" is implemented in C/C++ or other programming languages.

Accordingly, how will it make difference in terms of computation cost if I write an if statement in the following two ways:

Way I:

if(statement1)
    return true;
if(statement2)
    return true;
if(statement3)
    return true;

Way II:

if(statement1 || statement2 || statement3)
    return true;

In the second way, will all the statements be first evaluated and then the OR operation applied upon their results or will the OR operation be first taken into cognizance and then as soon as any statement evaluates to true, the true value shall be returned? The latter saves the computation cost/time by not having to necessarily evaluate each and every statement in the conditional, since as soon as it gets the first true, the job is done. But this is dependent on the operations inside the conditionals. If its an AND, it becomes a different case altogether. And things get tricker with combined operations(AND+OR+XOR..)

So how is it really done behind? Is it possible for the implementation to be case-dependent on the boolean operations inside the conditional?

Upvotes: 2

Views: 1005

Answers (6)

user2448027
user2448027

Reputation: 1638

The implementation depends on the compiler and optimization settings. In some cases, Way II (the one with OR conditions) is faster due to "short-circuiting": if the operand on the left side of the || operator is true, the rest of the condition isn't checked. In Way I, on the other hand, the conditions are ecexuted sequentially - unless some kind of optimization (or a smart compiler) is used to convert it to Way II (in which case both Ways are equally fast).

Upvotes: 2

Mike Seymour
Mike Seymour

Reputation: 254471

In the second way, will all the statements be first evaluated and then the OR operation applied upon their results or will the OR operation be first taken into cognizance and then as soon as any statement evaluates to true, the true value shall be returned?

Logic operators always short-circuit in C++. In the case of ||, if the first operand is true, then the second is not evaluated. This means that both of your examples should be equivalent; the code will evaluate the expressions until one is true, then return true.

Not only is this potentially more efficient that always evaluating both; it also allows something like

if (!p || *p == whatever)

in which it would be an error to evaluate the second if the first were true.

If its an AND, it becomes a different case altogether.

In that case, the second operand is only evaluated if the first is false.

And things get tricker with combined operations(AND+OR+XOR..)

There is no logical XOR in C++; only && and ||. Combining them is reasonably straightforward; && has a higher priority, and within each subexpression the operands are evaluated from left to right, short-circuiting as required.

Upvotes: 2

Boris
Boris

Reputation: 622

According to the standard:


5.15 Logical OR operator

   logical-or-expression:
     logical-and-expression
     logical-or-expression || logical-and-expression

1 The || operator groups left-to-right. The operands are both contextually converted to bool (Clause 4). It returns true if either of its operands is true, and false otherwise. Unlike |, || guarantees left-to-right evaluation; moreover, the second operand is not evaluated if the first operand evaluates to true.

2 The result is a bool. If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression.


Other fact - compiler abides rules or not.

Upvotes: 1

md5
md5

Reputation: 23699

I am curious to know how a conditional statement such as an "if" is implemented in C/C++ or other programming languages.

It depends on the assembly language of your computer. For instance, with x86 instruction listings, the instruction CMP will set a flag, which is read by instructions such as J** (JNE, JA, ...).

This is typically how a condition is "implemented".

In the second way, will all the statements be first evaluated and then the OR operation applied upon their results or will the OR operation be first taken into cognizance and then as soon as any statement evaluates to true, the true value shall be returned?

In C, && and || binary operators use short-circuit evaluation. That is, in E1 || E2, if E1 is true, E2 is not evaluated.

Upvotes: 4

Chris G.
Chris G.

Reputation: 3981

Smart compilers will convert Way I into Way II.

Way II checks each statement and returns true immediately upon encountering truth.

Upvotes: 1

KrisSodroski
KrisSodroski

Reputation: 2842

Depends on the compiler. You can see what happens by using gcc -o which will generate an object file. Open up the object file, and you'll see hwo the compiler optimizes it. Usually, it creates a jump table (for long conditionals), but other times it just uses conditional processor instructions.

Upvotes: 1

Related Questions