Re: [NEWBIE] question

From: Daniel A. Koepke (dkoepke@circlemud.org)
Date: 04/03/01


On Tue, 3 Apr 2001, Edward Felch wrote:

> I hate to ask a basic c question, but how does the ? in: GET_POS(ch)
> == POS_SITTING ? "sitting" : "resting"  work? Thanks a lot... ive been
> wondering

This is the ternary operator mentioned in another thread about code
maintainability.  It is a basic C question, and I would recommend you buy
a book on C (even a pocket reference that covers the syntax of the
language would be sufficient).

Having said that, the operator is not the question mark -- the operator is
the question mark and the colon.  The expression

  (a ? b : c)

should be read

  if (a) then (b) else (c)

It's a way of doing a simple conditional selection of a subexpression.
Naturally you can combine these:

  #define PDESC(perc) (perc >= 100 ? "perfect"           \
                      : perc >= 75 ? "very good"         \
                      : perc >= 60 ? "good"              \
                      : perc >= 40 ? "average"           \
                      : perc >= 25 ? "bad" : "awful")

Such conditional expressions can be found throughout the code.  Naturally,
they can be misused.  You should avoid any construct that affords you
terseness at the expense of your readability.

-dak

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/05/01 PST