C conditional statement shorthand puzzle -- please help!!

Discussion in 'C Programming' started by almurph@altavista.com, Jan 27, 2009.

  1. Guest

    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.
    , Jan 27, 2009
    #1
    1. Advertising

  2. "" <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 27, 2009
    #2
    1. Advertising

  3. Lew Pitcher Guest

    On January 27, 2009 12:12, in comp.lang.c,
    () 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. ------
    Lew Pitcher, Jan 27, 2009
    #3
  4. On Jan 27, 9:42 am, Lew Pitcher <> wrote:
    > On January 27, 2009 12:12, in comp.lang.c,
    >
    >
    >
    >
    >
    >
    >
    > () 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.
    Barry Schwarz, Jan 29, 2009
    #4
  5. James Kuyper Guest

    Barry Schwarz wrote:
    > On Jan 27, 9:42 am, Lew Pitcher <> wrote:
    >> On January 27, 2009 12:12, in comp.lang.c,

    ....
    >> () 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.
    James Kuyper, Jan 29, 2009
    #5
  6. Guest

    Thank you all for your comments.
    al.

    James Kuyper wrote:

    > Barry Schwarz wrote:
    > > On Jan 27, 9:42 am, Lew Pitcher <> wrote:
    > >> On January 27, 2009 12:12, in comp.lang.c,

    > ...
    > >> () 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.
    , Jan 29, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    790
  2. foreach shorthand

    , Mar 25, 2005, in forum: C++
    Replies:
    2
    Views:
    580
    =?iso-8859-1?Q?Ali_=C7ehreli?=
    Mar 25, 2005
  3. David W
    Replies:
    0
    Views:
    868
    David W
    Jan 17, 2007
  4. Replies:
    12
    Views:
    563
    Richard
    Jan 17, 2009
  5. Replies:
    4
    Views:
    250
Loading...

Share This Page