tags:

views:

235

answers:

9
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.

+2  A: 

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;
}
eglasius
+2  A: 

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

lomaxx
A: 

predicate ? then : else

Andrew
A: 

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 ;
JStriedl
+1  A: 

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 :

Steve Willcock
+5  A: 

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.)

Loadmaster
Which _could_ open up a way for an optimization/simplification on `do { ... } while (f3 <= 4000000);`
Tordek
+4  A: 

It's called the ternary operator.

mrduclaw
+13  A: 

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;
cdiggins
good answer; clear, concise, and unambiguous.
kloucks
`if (f3 % 2 == 0) sum += f3;` could also be rewritten as merely `if (!(f3 % 2)) sum += f3;` since 0 is a false value and non-zero is a true value.
Amber
@klouks: tyvm!@Dav: you are correct, but I have never been a fan of mixing boolean operations and integers. This is however purely a question of taste, and usually has to do with how much contact one has with old-school C programmers. :-)
cdiggins
Neither of you are correct. There's no conversion between int and bool in C#. You're thinking of C, not C#.
Eric Lippert
Thanks for correcting me Eric. I never do that, so I didn't know it wasn't even legal in C#.
cdiggins
A: 

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?

csharptest.net