Sergio Tapia
Sergio Tapia

Reputation: 41128

Can someone explain this short line of code to me?

bool stop = false;
           int f1 = 1;
           int f2 = 2;
           int f3 = 0;
           int sum = 2;
           while (!stop)
           {
               f3 = f1 + f2;
               sum += f3 % 2 == 0 ? f3 : 0; //THIS ONE
               stop = f3 > 4000000 ? true : false;//AND THIS ONE.
               f1 = f2;
               f2 = f3;
           }

What is that conditional operator? This is the first time I've seen anything like this.

Upvotes: 1

Views: 344

Answers (9)

csharptest.net
csharptest.net

Reputation: 64218

As masterfully written as the original was, you could obfuscate it's intent further:

int sum = 2;
for(int f1 = 1, f2 = 2, f3 = 0; !((f3 = (f1 + f2)) > 4000000); f1 = f2, f2 = f3)
    sum += f3 * (~f3 & 1);

... or ... write it like a normal person would:

int f1 = 1;
int f2 = 2;
int f3 = 0;
int sum = 2;

while( f3 <= 4000000 )
{
    f3 = f1 + f2;
    bool even = (f3 & 1) == 0;
    if( even )
        sum += f3;
    f1 = f2;
    f2 = f3;
}

... or ... if you like it really simple:

int sum = 4613732;

Even after rewriting it twice I don't get what it does... what's the purpose of this anyway?

Upvotes: 0

eglasius
eglasius

Reputation: 36027

If the condition (stuff to the left of ? is true, then it uses the first (the one before :) if not it uses the second (stuff after :).

int res = someCondition ? valueIfTrue : valueIfFalse;

see: http://msdn.microsoft.com/en-us/library/ty67wk28%28VS.80%29.aspx

Another one u will probably see soon:

   SomeClass res = someVariable ?? valueIfSomeVariableIsNull;

Update: on the refactor route, you might want:

while (!isMoreThan4Million)
{
   f3 = f1 + f2;
   bool sumIsEven = f3 % 2 == 0;
   sum += sumIsEven ? f3 : 0;
   isMoreThan4Million = f3 > 4000000;
   f1 = f2;
   f2 = f3;
}

Upvotes: 2

cdiggins
cdiggins

Reputation: 18203

The line:

sum += f3 % 2 == 0 ? f3 : 0; //THIS ONE

is the same as:

if (f3 % 2 == 0)
  sum += f3;
else
  sum += 0;

which could of course be rewritten as

if (f3 % 2 == 0) sum += f3;

and the line

stop = f3 > 4000000 ? true : false;//AND THIS ONE.

is the same as

if (f3 > 4000000)
   stop = true;
else 
   stop = false;

Or better yet:

stop = f3 > 4000000; 

Upvotes: 14

mrduclaw
mrduclaw

Reputation: 4035

It's called the ternary operator.

Upvotes: 4

David R Tribble
David R Tribble

Reputation: 12204

The expression

x = c ? a : b;

is equivalent to

if (c)
    x = a;
else
    x = b;

Also, the statement

stop = f3 > 4000000 ? true : false;

is completely redundant, and can be simplified to

stop = (f3 > 4000000);

(Paretheses added for clarity.)

Upvotes: 5

Steve Willcock
Steve Willcock

Reputation: 26839

http://msdn.microsoft.com/en-us/library/ty67wk28%28VS.80%29.aspx

It's called the ternary operator. It will evaluate the value before the : if the expression on the left of the ? is true - otherwise it evaluates the value after the :

Upvotes: 1

JStriedl
JStriedl

Reputation: 1296

it's more or less a compact equivalent of an if then:

(condition) ? ifConditionIsTrueUseThisValue : ifConditionIsFalseUseThisValue ;

commonly used to do a conditional value assignment:

variableName = (condition) ? valueIfConditionIsTrue : valueIfConditionIsFalse ;

simple stupid example to assign a value of x which ignores values below zero:

x = (x < 0) ? 0 : x ;

Upvotes: 0

Andrew
Andrew

Reputation: 85

predicate ? then : else

Upvotes: -2

lomaxx
lomaxx

Reputation: 115763

http://msdn.microsoft.com/en-us/library/ty67wk28%28VS.80%29.aspx

it basically reads like this:

if this condition is true ? then do this : otherwise do this

Upvotes: 2

Related Questions