home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18802 < prev    next >
Encoding:
Text File  |  1992-12-22  |  2.3 KB  |  72 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!spool.mu.edu!sgiblab!news.kpc.com!kpc!hollasch
  3. From: hollasch@kpc.com (Steve Hollasch)
  4. Subject: Re: QUESTION: TRICKS USING THE ? : OPERATOR
  5. Message-ID: <1992Dec23.001617.9462@kpc.com>
  6. Summary: Some comments
  7. Sender: usenet@kpc.com
  8. Organization: Kubota Pacific Computer, Inc.
  9. References: <dsembr01.725056046@starbase.spd.louisville.edu>
  10. Date: Wed, 23 Dec 1992 00:16:17 GMT
  11. Lines: 59
  12.  
  13. dsembr01@orion.spd.louisville.edu (Darren S. Embry) writes:
  14. | Okay, here are a couple of interesting and maybe useful tricks I discovered
  15. | that involve the trinary ? : operator:
  16.  
  17. | 1)    Using it as a rudimentary if-else statement:
  18. |               (2*3==6)
  19. |                       ? printf ("TRUE")
  20. |                       : printf ("FALSE");
  21. |               
  22. |       as a substitute for
  23. |       
  24. |               if(2*3=6)
  25. |                       printf("TRUE");
  26. |               else
  27. |                       printf("FALSE");
  28.  
  29.     This would be better written as:
  30.  
  31.         printf ((2*3==6) ? "TRUE" : "FALSE");
  32.  
  33. | 2)    Nesting the ? : operator, as in the example below:
  34. |       a?(b?c:d):e             can be written as       a?b?c:d:e
  35. |       a?b:(c?d:e)             can be written as       a?b:c?d:e
  36.  
  37.     This is pretty ugly.  Here's the style I use for multi-conditionals:
  38.  
  39.         printf
  40.         (   "Variable type is %s.\n",
  41.             (vartype == VAR_INTEGER) ? "integer"        :
  42.             (vartype == VAR_STRING)  ? "string"         :
  43.             (vartype == VAR_FLOAT)   ? "floating-point" :
  44.             (vartype == VAR_ZLORT)   ? "zlortskatster"  :
  45.             /* Other */                "unknown"        ;
  46.         );
  47.  
  48.  
  49. | Now, here are a couple of questions that you must answer:
  50.  
  51.     Nuh uh.
  52.  
  53. | I would appreciate lots of answers, and any objections to using this.
  54.  
  55.     You can also construct monsters like this:
  56.  
  57.         ((condition) ? function1 : function2) (arg1, arg2, arg3);
  58.  
  59.     which should be equivalent to:
  60.  
  61.         if (condition)
  62.             function1 (arg1, arg2, arg3);
  63.         else
  64.             function2 (arg1, arg2, arg3);
  65.  
  66. ______________________________________________________________________________
  67. Steve Hollasch                                   Kubota Pacific Computer, Inc.
  68. hollasch@kpc.com                                 Santa Clara, California
  69.