? : Operator

O

OJ

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 = 1 : arrFib =(arrFib[i-1] + arrFib[i-2]));}

If I write it like this it is correct...

int i;
int fib[20];

fib[0] = 1;
fib[1] = 1;

for (i = 2; i < 20; i++)
{
fib = fib[i-1] + fib[i-2];}

I don't see the difference...

Thanks,
OJ
 
A

Alf P. Steinbach

* OJ:
as a newbie, could someone explain the "? :" operator to me...

(q? a1 : a2) is an expression that if q yields result a1, otherwise a2.

See also sections 1 and 2 of chapter 1.4 in

<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.html>.


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++);

Always use braces around the loop body: here you have an empty loop body.

{(i<2 ? arrFib = 1 : arrFib =(arrFib[i-1] + arrFib[i-2]));}


Never use side-effects in expressions.

If I write it like this it is correct...

int i;
int fib[20];

fib[0] = 1;
fib[1] = 1;

for (i = 2; i < 20; i++)

As a matter of style use '++i' rather than 'i++', see
{
fib = fib[i-1] + fib[i-2];}

I don't see the difference...


See above.
 
O

OJ

Hi Alf,
Thanks for the response....I'm afraid I've exposed myself as a total
beginner here!!....I shall be reading the website you posted!

Just one thing, what do you mean by...
Never use side-effects in expressions.

Thanks,
O
 
M

marbac

OJ said:
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 = 1 : arrFib =(arrFib[i-1] + arrFib[i-2]));}


That is the conditional expression operator. It behaves like
if (i<2) arrFib = 1; else arrFib =(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<2)?1:(arrFib[i-1]+arrFib[i-2]); // gives the same result.


regards marbac
 
M

Mark P

OJ said:
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++);

The problem is not in your use of ?: but in your for loop. The trailing
; is treated as an empty statement so your have twenty iterations of
nothing, and then one execution of the line below (with i still set to 20).
{(i<2 ? arrFib = 1 : arrFib =(arrFib[i-1] + arrFib[i-2]));}


The more conventional (and IMHO readable) way to write this is:

arrFib = (i<2 ? 1 : arrFib[i-1] + arrFib[i-2]);

where the parentheses are in fact optional.
If I write it like this it is correct...

int i;
int fib[20];

fib[0] = 1;
fib[1] = 1;

for (i = 2; i < 20; i++)
{
fib = fib[i-1] + fib[i-2];}

I don't see the difference...

Thanks,
OJ
 
O

OJ

Thanks everyone...I'm used to VB and am trying to teach myself C via
the internet...any resource suggestions would be most appreciated,
especially on best practice.
 
A

Alf P. Steinbach

* OJ:
Hi Alf,
Thanks for the response....I'm afraid I've exposed myself as a total
beginner here!!....I shall be reading the website you posted!

Just one thing, what do you mean by...
Never use side-effects in expressions.

Where you wrote

(i<2 ? arrFib = 1 : arrFib =(arrFib[i-1] + arrFib[i-2]))

you have side-effects, that during the evaluation of this expression
variables change values.

That will in most cases (but fortunately not in this special case)
yield Undefined Behavior, which in practice means e.g. different
results with different compilers.

Instead write clean expressions like the one on the right hand side
of '=' here:

arrFib = (i<2 ? 1 : (arrFib[i-1] + arrFib[i-2]))

where the assignment has been moved out of the expression.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top