Amit Tomar
Amit Tomar

Reputation: 4858

What do C/C++ expressions return?

What do the expressions in C++ return, the actual value obtained after applying the operators on the objects or true/false based on the calculated value ? Particularly :

In this code segment :

int i = 10, j = 5, k = 0;
i + j ;
k = i + j ;
while ( i + j )
{
    // Do something worth for the universe in this time
}

What will the expression written in line No. 2 return, 15 or true ? Will it return the same value in line No. 4 ? Is 15 always returned, but converted to true or false based on the context ?

I read this in C++ Primer :

A while has the form

while (condition)
     statement   

A while executes by (alternately) testing the condition and executing the associated statement until the condition is false. A condition is an expression that yields a result that is either true or false.

But expressions could be just plain objects as well right !? How are they supposed to mean a true or false ? For eg:

// Create an object `tempObject` of a class `SomeRandomClass`
while ( tempObject )
{

}

Can someone explain ?

Upvotes: 0

Views: 280

Answers (4)

Pete Becker
Pete Becker

Reputation: 76438

i + j has type int because both i and j are ints. The value of the expression is the sum of the two. In the statement

i + j;

the value is not used, so it is ignored. Chances are the compiler will omit the addition, since it has no side effects. In the statement

k = i + j;

the result is used; it is copied into k. In the statement

while (i + j) {
}

the value is used in a boolean context. Formally, it is "contextually converted to bool": zero is converted to false and any other value is converted to true.

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129464

When the "condition" in an if or while is not a direct boolean value (true or false) from the expression itself (== or > or <=, for example, would give a boolean value), the expression is seen as if it was a comparison as not equal to zero.

So:

int x = 25;

while(x)
 ... 

is the same as

while (x != 0)

This applies to all types, such as pointers, integers and floating point values.

Edit as per comments below:

An object of a class or struct must be possible to convert into a bool in some way: Either the object has direct operator bool() like this:

class X
{
  private:
    int x;
  public:
    X(int xx = 0) x(xx) {}
    void setx(int xx) { x = xx; }
    bool operator bool() const { return x != 0; }
};

Now we can do:

X a(1);

if (a) ... 
a.setx(0);
if (a) ...

The first if will be true, the second if will be false.

If there isn't an operator bool() available, then the compiler may use operator int() or operator void* to convert the type, and then making the implicit != 0 comparison to produce a bool value.

Upvotes: 4

Michael Martini
Michael Martini

Reputation: 61

To understand these code, please first go back to C:

Everthing which is 0 is false. Everthing which is not 0 is true.

In your case the while loop then means while ( 15 ) which is the same as while ( true )

An endless loop...

Giving an object into such a code the object must behave at least as an integer or an boolean giving the while loop any evaluable expression, e.g.

public:
    operator bool() const
    {
         return myVar;
    }

where you store the while condition in myVar ...

Upvotes: 1

SLaks
SLaks

Reputation: 887797

0 (and false, in versions of C++ where that exists) means false; any other value means true.

Upvotes: 2

Related Questions