1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
<title>Conditional Expressions</title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>Conditional Expressions.</h1>
</center>
<hr>
<p>
We have a short-hand construct for some <b>if ... else ...</b> constructs.<p>
Consider the following two examples.
<p>
<center>
<table border=2 bgcolor=ivory>
<th>Example 1</th>
<th>Example 2</th>
<tr>
<td>
<pre>
if ( x == 1 )
y = 10;
else
y = 20;
</pre>
</td>
<td>
<pre>
y = (x == 1) ? 10 : 20;
</pre>
</td>
</tr>
</table>
</center>
<p>
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 <b>true</b> evaluates the second '10'. If <b>false</b> the
third is evaluated. Here is another example.
<p>
<center>
<table border=2 bgcolor=ivory>
<th>Example 1</th>
<th>Example 2</th>
<tr>
<td>
<pre>
if ( x == 1 )
puts("take car");
else
puts("take bike");
</pre>
</td>
<td>
<pre>
(x == 1) ? puts("take car") : puts("take bike");
or
puts( (x == 1) ? "take car" : "take bike");
</pre>
</td>
</tr>
</table>
</center>
<p>
It has been said that the compiler can create more efficent code from
a <b>conditional expression</b> 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 ;-)
<p>
<hr>
<h2>See also:</h2>
<ul>
<li><a href="if.html">if</a> keyword.
<li><a href="switch.html">switch</a> keyword.
<li><a href="idioms.html#printf">A use within printf.</a>.
</ul>
<p>
<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html"> Top</a>
</td><td width="25%">
<a href="../master_index.html"> Master Index</a>
</td><td width="25%">
<a href="keywords.html"> Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm"> Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie
</address><p>
</body>
</html>
|