home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!spool.mu.edu!sgiblab!news.kpc.com!kpc!hollasch
- From: hollasch@kpc.com (Steve Hollasch)
- Subject: Re: QUESTION: TRICKS USING THE ? : OPERATOR
- Message-ID: <1992Dec23.001617.9462@kpc.com>
- Summary: Some comments
- Sender: usenet@kpc.com
- Organization: Kubota Pacific Computer, Inc.
- References: <dsembr01.725056046@starbase.spd.louisville.edu>
- Date: Wed, 23 Dec 1992 00:16:17 GMT
- Lines: 59
-
- dsembr01@orion.spd.louisville.edu (Darren S. Embry) writes:
- |
- | Okay, here are a couple of interesting and maybe useful tricks I discovered
- | that involve the trinary ? : operator:
-
- | 1) Using it as a rudimentary if-else statement:
- |
- | (2*3==6)
- | ? printf ("TRUE")
- | : printf ("FALSE");
- |
- | as a substitute for
- |
- | if(2*3=6)
- | printf("TRUE");
- | else
- | printf("FALSE");
-
- This would be better written as:
-
- printf ((2*3==6) ? "TRUE" : "FALSE");
-
- | 2) Nesting the ? : operator, as in the example below:
- |
- | a?(b?c:d):e can be written as a?b?c:d:e
- | a?b:(c?d:e) can be written as a?b:c?d:e
-
- This is pretty ugly. Here's the style I use for multi-conditionals:
-
- printf
- ( "Variable type is %s.\n",
- (vartype == VAR_INTEGER) ? "integer" :
- (vartype == VAR_STRING) ? "string" :
- (vartype == VAR_FLOAT) ? "floating-point" :
- (vartype == VAR_ZLORT) ? "zlortskatster" :
- /* Other */ "unknown" ;
- );
-
-
- | Now, here are a couple of questions that you must answer:
-
- Nuh uh.
-
- | I would appreciate lots of answers, and any objections to using this.
-
- You can also construct monsters like this:
-
- ((condition) ? function1 : function2) (arg1, arg2, arg3);
-
- which should be equivalent to:
-
- if (condition)
- function1 (arg1, arg2, arg3);
- else
- function2 (arg1, arg2, arg3);
-
- ______________________________________________________________________________
- Steve Hollasch Kubota Pacific Computer, Inc.
- hollasch@kpc.com Santa Clara, California
-