When the condition is not met, the flow of the program continues after the loop.
NOTE It is important to note the use of the less-than operator (<) in place
of the less-than-or-equal-to operator (<=), which allows the loop to proceed
one more time until i=10. This is an important concept that can lead to off-byone
errors. Also, note the count was started with 0. This is common in C and
worth getting used to.
PART III
Chapter 6: Programming Survival Skills
125
The while loop is used to iterate through a series of statements until a condition is
met. The format is as follows:
while(
){
;
}
It is important to realize that loops may be nested within each other.
if/else
The if/else construct is used to execute a series of statements if a certain condition is met;
otherwise, the optional else block of statements is executed. If there is no else block of
statements, the flow of the program will continue after the end of the closing if block
curly bracket (}). The format is as follows:
if() {
} {
;
}
The braces may be omitted for single statements.
Comments
To assist in the readability and sharing of source code, programmers include comments
in the code.
Pages:
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281