detraveller
detraveller

Reputation: 295

How is this if condition working?

I have Following IF statement and I can't figure out what it means:

if (data->tokens.size()) 
{..
  //reads each token in a for loop and assigns each token to a variable...
}

Tokens have six values in it so tokens.size() should return 6, right?

I don't understand why we need an if condition there? From what i understand, if condition will be true if tokens.size() returns 1 and false if it returns 0.

In my case, it returns 6(or whatever the amount of tokens which can be any number and not just 0 or 1). So how is this if condition working?

In other words, I want to know how does if handle when it gets the values other than 0 and 1.

Upvotes: 1

Views: 173

Answers (6)

qPCR4vir
qPCR4vir

Reputation: 3571

To make it more precise: the standard said:

6.4 Selection statements

selection-statement:

if ( condition ) statement

… The rules for conditions apply both to selection-statements and to the for and while statements (6.5). … The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable contextually converted to bool (Clause 4). If that conversion is ill-formed, the program is ill-formed. … The value of a condition that is an expression is the value of the expression, contextually converted to bool; if that conversion is ill-formed, the program is ill-formed.

Where conversion to bool mean:

4 Standard conversions

Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (8.5).

This is important. That is why you can do this sort of “magic”:

while(getline(cin,str))) cout<<str;

with will apply bool(cin)(in this case getline return cin))

In your example the conversion will be:

4.12 Boolean conversions [conv.bool]

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. For direct-initialization (8.5), a prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

Just as an aside comment, chances are that the if in your example is almost useless or even just obfuscating the code. Consider:

for (int i=0; i< data->tokens.size(); ++i) 
{..
  //reads a token and assigns to a variable...
}

or:

for( const auto &token : data->tokens)
    {..
      // assigns token to a variable...
    }

In both case the the for will enter only if size()>0 making redundant the if.

Upvotes: 0

fatihk
fatihk

Reputation: 7919

if(data->tokens.size()) ~ if(data->tokens.size()!=0) 

Upvotes: 0

Robert
Robert

Reputation: 20286

It is conditional not a loop. If it is true then you enter conditional block. If it's zero then it's false and it doesn't enter block. If there's any other value than 0 then it's true and it enters block.

Upvotes: 0

siritinga
siritinga

Reputation: 4231

if takes a true or false bool expression, not a number. Fortunately for us, if you give a number, it will be converted to a bool. False if the number is 0, true otherwise, so it will work.

Upvotes: 1

lucasg
lucasg

Reputation: 11002

I think it is just a test of non-emptyness to ensure we don't iterate over an empty vector

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129364

Conditional statemens in C and C++ are implicitly comparing with "not equal to zero" if nothing else is given. E.g. if (data->tokens.size()) is the same as if (data->tokens.size() != 0).

Upvotes: 5

Related Questions