C conditional statement shorthand puzzle -- please help!!

A

almurph

Hi,


I am new to C and have come accross the following line of code:


A = ( B ? A / B : 1E6 );


where:
"A" is defined to be of type float and is initially assigned the value
0, subject to change
"B" is defined to be of type short and is initially assigned the value
0, subject to change


does the above equate to:

if(B > 0)
{
A = A / B;
}
else
{
A = 1E6;
}


My first questions is - is this correct?

My second question is - can you do this B as type short. i though
this would have to be a boolean type? I'm confused.

Would appreciate any comments/suggestions/thoughts/code-sample that
you may want to offer.

Thanking you,
Al.
 
K

Keith Thompson

I am new to C and have come accross the following line of code:


A = ( B ? A / B : 1E6 );


where:
"A" is defined to be of type float and is initially assigned the value
0, subject to change
"B" is defined to be of type short and is initially assigned the value
0, subject to change


does the above equate to:

if(B > 0)
{
A = A / B;
}
else
{
A = 1E6;
}


My first questions is - is this correct?

Not quite. There is no ">" comparison in the original line.

You understand (mostly) how the conditional operator "?:" works, so I
won't go into that. The code is essentially equivalent to:

if (B) {
A = A / B;
}
else {
A = 1E6;
}

where "if (B)" is, by definition, equivalent to "if (B != 0)".

Any scalar (i.e., either arithmetic or pointer) expression can be used
as a condition. Here, "B" is being used as the condition in the "?:"
operator in the original code, and in an if statement in the
equivalent. When a value is used as a condition, it's always compared
for inequality to zero. (For a pointer, this means that it checks
that it's not a null pointer.)
My second question is - can you do this B as type short. i though
this would have to be a boolean type? I'm confused.

Yes, see above. Note that, prior to the C99 standard, C didn't even
have a boolean type. C99 added one, but comparisons such as "==" or
">" still yield values of type int, not _Bool.

Personally, unless B is being used just as a Boolean value, I'd write
"B != 0" rather than just "B" (or "B != NULL" if B is a pointer), but
either is perfectly legal. And plenty of C programmers do not share
my preference, and like to write "if (B)" even if B isn't just being
used as a Boolean.
 
L

Lew Pitcher

Hi,


I am new to C and have come accross the following line of code:


A = ( B ? A / B : 1E6 );


where:
"A" is defined to be of type float and is initially assigned the value
0, subject to change
"B" is defined to be of type short and is initially assigned the value
0, subject to change


does the above equate to:

if(B > 0) [snip]
}


My first questions is - is this correct?

No. You could re-express the statement as

if (B != 0)
A = A/ B;
else
A = 1E6;

Note the difference on the condition.
My second question is - can you do this B as type short. i though
this would have to be a boolean type? I'm confused.

B could have any scalar type. Scalar types consist of all the standard
arithmetic types (char, int, float, etc.) and pointer types.
Would appreciate any comments/suggestions/thoughts/code-sample that
you may want to offer.

Thanking you,
Al.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
B

Barry Schwarz

I am new to C and have come accross the following line of code:
A  = ( B ? A / B : 1E6 );
where:
"A" is defined to be of type float and is initially assigned the value
0, subject to change
"B" is defined to be of type short and is initially assigned the value
0, subject to change
does the above equate to:
if(B > 0) [snip]

My first questions is - is this correct?

No. You could re-express the statement as

  if (B != 0)
    A = A/ B;
  else
    A = 1E6;

Note the difference on the condition.
My second question is - can you do this B as type short. i though
this would have to be a boolean type? I'm confused.

B could have any scalar type. Scalar types consist of all the standard
arithmetic types (char, int, float, etc.) and pointer types.

B can be any arithmetic type. It cannot be a pointer type because
division by a pointer makes no sense.
 
J

James Kuyper

Barry said:
Hi,
I am new to C and have come accross the following line of code:
A = ( B ? A / B : 1E6 );
where:
"A" is defined to be of type float and is initially assigned the value
0, subject to change
"B" is defined to be of type short and is initially assigned the value
0, subject to change
does the above equate to:
if(B > 0) [snip]
} ....
My second question is - can you do this B as type short. i though
this would have to be a boolean type? I'm confused.
B could have any scalar type. Scalar types consist of all the standard
arithmetic types (char, int, float, etc.) and pointer types.

B can be any arithmetic type. It cannot be a pointer type because
division by a pointer makes no sense.

It did not seem to me that the question "can you do this" was referring
to the entire expression, but only specifically to the use of an
expression that is not of boolean type as the left operand of a
conditional expression. Lew's answer seems to be based upon that same
understanding.
 
A

almurph

Thank you all for your comments.
al.

James said:
Barry said:
On January 27, 2009 12:12, in comp.lang.c, (e-mail address removed) ...
([email protected]) wrote:
Hi,
I am new to C and have come accross the following line of code:
A = ( B ? A / B : 1E6 );
where:
"A" is defined to be of type float and is initially assigned the value
0, subject to change
"B" is defined to be of type short and is initially assigned the value
0, subject to change
does the above equate to:
if(B > 0)
[snip]
} ...
My second question is - can you do this B as type short. i though
this would have to be a boolean type? I'm confused.
B could have any scalar type. Scalar types consist of all the standard
arithmetic types (char, int, float, etc.) and pointer types.

B can be any arithmetic type. It cannot be a pointer type because
division by a pointer makes no sense.

It did not seem to me that the question "can you do this" was referring
to the entire expression, but only specifically to the use of an
expression that is not of boolean type as the left operand of a
conditional expression. Lew's answer seems to be based upon that same
understanding.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top