From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/SYNTAX/conditional.html | 120 ++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 reference/C/SYNTAX/conditional.html (limited to 'reference/C/SYNTAX/conditional.html') diff --git a/reference/C/SYNTAX/conditional.html b/reference/C/SYNTAX/conditional.html new file mode 100644 index 0000000..45b39af --- /dev/null +++ b/reference/C/SYNTAX/conditional.html @@ -0,0 +1,120 @@ +Conditional Expressions + + + + + + +
+
+

Conditional Expressions.

+
+
+

+ + +We have a short-hand construct for some if ... else ... constructs.

+Consider the following two examples. +

+

+ + + + + + + +
Example 1Example 2
+
+
+  if ( x == 1 ) 
+    y = 10;
+  else
+    y = 20;
+    
+
+
+
+
+  y = (x == 1) ? 10 : 20;  
+
+
+
+
+

+These examples both perform the same function. If x is 1 then y becomes 10 +else y becomes 20. The example on the right evaluates the first expression +'(x ==1 )' and if true evaluates the second '10'. If false the +third is evaluated. Here is another example. +

+

+ + + + + + + +
Example 1Example 2
+
+
+ if ( x == 1 ) 
+   puts("take car"); 
+ else
+   puts("take bike");  
+
+
+
+
+ (x == 1) ? puts("take car") : puts("take bike"); 
+                                                  
+    or
+
+ puts( (x == 1) ? "take car" : "take bike");
+
+
+
+

+It has been said that the compiler can create more efficent code from +a conditional expression possibly at the expence of readable code. +Unless you are writing time critical code (and lets face it, thats unlikely) +the more efficent code is not much of a reason to use this construct. +I feel that it has its uses, but should not be lost into some complex +statement, +but, since when did C programmers worry if anyone else could read their +code ;-) +

+


+

See also:

+ + +

+ +


+

+

+ + + + +
+ Top + + Master Index + + Keywords + + Functions +
+
+

+


+
Martin Leslie +

+ + -- cgit v1.2.3-54-g00ecf