Re: ? : Operator
From: marbac (marbac_at_chello.at)
Date: 03/24/05
- Next message: marbac: "Re: ctor, operator ="
- Previous message: OJ: "Re: ? : Operator"
- In reply to: OJ: "? : Operator"
- Next in thread: Mark P: "Re: ? : Operator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 24 Mar 2005 07:42:16 GMT
OJ schrieb:
> Hi,
> as a newbie, could someone explain the "? :" operator to me...
> I wrote this as part of a small routine to fill an array with the first
> 20 items of the Fibonacci sequence, but it produces strange results...
>
> int arrFib[20];
> int i,k;
>
> for(i=0;i<20;i++);
> {(i<2 ? arrFib[i] = 1 : arrFib[i] =(arrFib[i-1] + arrFib[i-2]));}
>
That is the conditional expression operator. It behaves like
if (i<2) arrFib[i] = 1; else arrFib[i] =(arrFib[i-1] + arrFib[i-2]);
with the difference, that it also returns the value.
I often use it to decide for a value within an operation chain
e.g.:
for (1=0;i<20;i++)
arrFib[i]=(i<2)?1:(arrFib[i-1]+arrFib[i-2]); // gives the same result.
regards marbac
- Next message: marbac: "Re: ctor, operator ="
- Previous message: OJ: "Re: ? : Operator"
- In reply to: OJ: "? : Operator"
- Next in thread: Mark P: "Re: ? : Operator"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|