Velocity Reviews > C conditional statement shorthand puzzle -- please help!!

# C conditional statement shorthand puzzle -- please help!!

almurph@altavista.com
Guest
Posts: n/a

 01-27-2009
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.

Keith Thompson
Guest
Posts: n/a

 01-27-2009
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Lew Pitcher
Guest
Posts: n/a

 01-27-2009
On January 27, 2009 12:12, in comp.lang.c, (E-Mail Removed)
((E-Mail Removed)) 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 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. ------

Barry Schwarz
Guest
Posts: n/a

 01-29-2009
On Jan 27, 9:42*am, Lew Pitcher <(E-Mail Removed)> wrote:
> On January 27, 2009 12:12, in comp.lang.c, (E-Mail Removed)
>
>
>
>
>
>
>
> ((E-Mail Removed)) 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 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.

James Kuyper
Guest
Posts: n/a

 01-29-2009
Barry Schwarz wrote:
> On Jan 27, 9:42 am, Lew Pitcher <(E-Mail Removed)> wrote:
>> On January 27, 2009 12:12, in comp.lang.c, (E-Mail Removed)

....
>> ((E-Mail Removed)) 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.

almurph@altavista.com
Guest
Posts: n/a

 01-29-2009
Thank you all for your comments.
al.

James Kuyper wrote:

> Barry Schwarz wrote:
> > On Jan 27, 9:42 am, Lew Pitcher <(E-Mail Removed)> wrote:
> >> On January 27, 2009 12:12, in comp.lang.c, (E-Mail Removed)

> ...
> >> ((E-Mail Removed)) 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.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post almurph@altavista.com C Programming 4 02-02-2009 04:42 PM almurph@altavista.com C Programming 12 01-17-2009 01:42 AM tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM Quean Computer Support 2 12-23-2004 07:08 AM sk A+ Certification 1 07-17-2004 05:19 PM

Advertisments