tags:

views:

148

answers:

5

I do not understand the termination parameter of this for loop. What does it mean? Specifically, what do the ?, ->, and : 0 represent?

for( i = 0; i < (sequence ? sequence->total : 0); i++ )
+8  A: 

This: (sequence ? sequence->total : 0) (it's called a "ternary if", since it takes three inputs) is like saying:

if (sequence)
    replaceEntireExpressionWith(sequence->total);
else
    replaceEntireExpressionWith(0);

-> is a dereferencer, just like *, but it makes user data-types like structs easy to use.

sequence->total means sequence is a pointer to a one of those data types, and you are accessing the total property of what it is pointing to. It's exactly the same as:

(*sequence).total;

So the loop:

for( i = 0; i < (sequence ? sequence->total : 0); i++ )

exits when sequence evaluates to false, since 0 == false.

The ternary if construction is used to make sure they aren't dereferencing (->) a null pointer, because if they just put sequence->total as the condition, they would be dereferencing it every time. Unhappy! =(

Chris Cooper
is -> the same as a pointer? Can -> and * be used interchangeably?
typoknig
Yes, it's just more convenient. It's the same thing as (*sequence).total.
Chris Cooper
Minor nitpick: "-> is a dereferencer, just like *, but it makes structs easy to use." -- -> works for all user data types, including classes and unions, not just structs.
Billy ONeal
@Billy: I'll edit that. Thanks.
Chris Cooper
+2  A: 

if sequence not valid, or is the member called "total" is reached by i.

The ? says if sequence is not valid the loop ends when i = 0. (so it wont run)

Chris H
+1 for actually explaining the purpose rather than saying only what can be found in every C++ reference.
Billy ONeal
+2  A: 

You're seeing two different operators.

  1. The conditional operator

    condition ? truePart : falsePart
    

    This operator check whether condition is true, and evaluates to either truePart or falsePart.

  2. The deferenced member operator

    pointer->member
    

    This is equivalent to (*pointer).member.

Your expression will evaluate to 0 if sequence is null, and the total property of the structure that sequence points to if it isn't null.

SLaks
+1 for actually explaining the purpose rather than saying only what can be found in every C++ reference.
Billy ONeal
+1  A: 

there are two things at work here:

the ? and : syntax are the syntax for a ternary operation

http://en.wikipedia.org/wiki/Ternary_operation

other answers here explain ternary operators concisely

the -> syntax is to access a member from a pointer as opposed to a reference or value

this code is equivalent to:

int total = 0;
if(sequence){
    total = sequence->total;
}

for( i=0; i < total; ++i){
    ...
}
eipxen
it isn't if sequence or sequence->total is modified in the loop.
KillianDS
Thanks for the link, I didn't know the termonlogy to look for.
typoknig
right, sorry, forgot about the repeated reevaluation in the loop
eipxen
A: 

? : is the ternary operator. It evaluates the expression before the ?, and the result is the expression after the ? if true, or the expression after the : if false.

-> is what I'd call "operator arrow", but what it essentially does is dereference the pointer sequence, and then access a member called total.

Two things here:

  1. The purpose of this is to ensure sequence is not a NULL pointer before attempting to dereference it with ->,
  2. The loop should be rewritten like this:
    for( i = 0; sequence && i < sequence->total; i++ )
    That removes the confusion regarding what is going on.
Billy ONeal
Chris Cooper
Billy ONeal
@Billy: Is that the idiom? In that case, ok. I accept your premise =D. But about the short circuit... Why doesn't the left side just evaluate to `false`, which is `0` and then get compared the sequence's total?
Chris Cooper
Actually, perhaps I'll post that as an "official question" in the morning, since I believe it deserves and "official answer". ;PI'll link here when I ask it, for anyone reading this who is interested. Unless there are similar questions out there...
Chris Cooper
Billy ONeal
Ahhhhhhh. Thanks! =)
Chris Cooper