summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/while.html
blob: ef083f9ccf683fe9a722be14710f927e8e33e07d (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<html>
<head>
<title>The WHILE keyword.</title>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center><h1>The WHILE keyword.</h1></center>
<hr>
The <b>while</b> keyword is related to <a href=do.html>do</a>
and <a href=for.html>for</a>. Its purpose is to repeatedly execute
a <a href="statements.html#blocks">block</a> of statements. Here is 
an example :

<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr>
<td>
<pre>

        main()
        {
            int i=5;

            while(--i)
            {
                printf(" i is %d\n", i);
            }
        }
</pre>
</td>
</tr>
</table>
</center>
<p>

The expression <b>i--</b> is evaluated and if its 
<a href="../CONCEPT/true_false.html">true</a> the statements in the 
<a href="statements.html#blocks">block</a> are executed. 
The loop continues until the expression is false (zero).
The result will look like this:

<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr>
<td>
<pre>
                
                i is 4
                i is 3
                i is 2
                i is 1
</pre>
</td>
</tr>
</table>
</center>
<p>

It is important to note that the statements 
on a <b>while</b> will not get executed if the first evaluation of the
expression is FALSE. If you do not want this to happen you may prefer
to use the <a href="do.html">do</a> statement.

<hr>
<p>
Now consider the next example.

<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr>
<td>
<pre>

        main()
        {
            int i=5;

            while(--i);
            {
                printf(" i is %d\n", i);
            }
        }
</pre>
</td>
</tr>
</table>
</center>


The result will look like this:


<p>
<center>
<table border=1 width="80%" bgcolor="ivory">
<tr>
<td>
<pre>

                i is 0
</pre>
</td>
</tr>
</table>
</center>

<p>
This is because of the <b>;</b> on the end of the <b>while</b>
statement which means the <b>while</b> will loop (executing NULL statements)
until <b>i</b> is zero. Execution will then continue down the program 
(to the printf).
<p>

<hr>
<h2>Examples:</h2>

<img src="../../GRAPHICS/computer.gif" align=left alt="o">
<a href="../EXAMPLES/while.c">Basic <b>while</b></a>.
<br clear=left>

<hr>
<h2>See also:</h2>
<ul>
<li><a href="while.html">for</a> keyword.
<li><a href="do.html">do</a> keyword.
<li><a href="break.html">break</a> keyword.
<li><a href="continue.html">continue</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>