summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/if.html
diff options
context:
space:
mode:
Diffstat (limited to 'reference/C/SYNTAX/if.html')
-rw-r--r--reference/C/SYNTAX/if.html133
1 files changed, 133 insertions, 0 deletions
diff --git a/reference/C/SYNTAX/if.html b/reference/C/SYNTAX/if.html
new file mode 100644
index 0000000..8df9bb6
--- /dev/null
+++ b/reference/C/SYNTAX/if.html
@@ -0,0 +1,133 @@
+<title>if-else keywords.</title>
+
+<head>
+<script language="JavaScript">
+</script>
+</head>
+
+<body bgcolor="#ffffcc">
+<hr>
+<center>
+<h1>The 'if' and 'else' keywords</h1>
+</center>
+<hr>
+<p>
+
+The if-else statement is a two-way decision statement. It is written as
+
+<pre>
+ if ( expression ) statement1;
+ [else statement2;]
+</pre>
+
+The else portion is optional. If the expression evaluates to
+<a href="../CONCEPT/true_false.html">true</a>
+(anything other than 0) then statement1 is executed. If there is an <b>else</b>
+statement and the expression evaluates to
+<a href="../CONCEPT/true_false.html">false</a>
+statement2 is executed.
+
+For example
+
+<pre>
+(1)
+ int NumberOfUsers;
+ .
+ .
+ if( NumberOfUsers == 25 )
+ { /* No else part */
+ printf( "There are already enough users. Try later.\n" );
+ return ERROR;
+ }
+ .
+ .
+
+
+(2) if( a >= b ) larger = a; /* else part is present */
+ else larger = b;
+</pre>
+
+Consider this code fragment:
+<pre>
+ if( p == 1 )
+ if( q == 2 ) r = p * 2 + q;
+ else r = q * 2 + p;
+</pre>
+Because the <b>else</b> part is optional, there is an ambiguity when an <b>else</b> is
+omitted from a nested if sequence. In 'C', this is resolved by associating
+the <b>else</b> with the closest previous if that does not have an <b>else</b>.
+Therefore, in the above example, the <b>else</b> part belongs to the if(q==2)
+statement.
+
+The code can be made more readable by explicitly putting parentheses in
+the expression, like this
+
+<pre>
+ if( p == 1 )
+ {
+ if( q == 2 ) r = p * 2 + q;
+ }
+ else r = q * 2 + p;
+
+ OR
+
+ if( p == 1 )
+ {
+ if( q == 2 ) r = p * 2 + q;
+ else r = q * 2 + p;
+ }
+</pre>
+
+Because the statement in the <b>else</b> part can also be an if statement, a
+construct such as shown below is possible in 'C' to create a multiple
+choice construct.
+
+<pre>
+ if( expression1 )
+ statement1;
+ else if( expression2 )
+ statement2;
+ else if( expression3 )
+ statement3;
+ .
+ .
+ else
+ statementN;
+</pre>
+
+<hr>
+<h2>Example:</h2>
+<img src="../../GRAPHICS/computer.gif" align=center>
+<a href="../EXAMPLES/if.c"> Basic <b>if</b> example.</a>
+<br clear=left>
+<hr>
+<h2>See also:</h2>
+<ul>
+<li><a href="switch.html">switch</a> keyword.
+</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>