summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/if.html
blob: 8df9bb66c18c494dbee3b9c9e04755c8cbef5e11 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
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>