Summary

   


This chapter explored the fundamental aspects of iteration and how to implement this essential programming tool by using C#'s three types of iteration statements while, do-while, and for loops.

Important points mentioned in this chapter are reviewed in this section.

An iteration statement repeats a statement or block of statements as long as its loop condition is true.

Iteration statements are often used to generate or analyze (traverse) a sequence of data elements.

The while loop statement consists of the while keyword, a loop condition, and a loop body (consisting of one or more statements) that is repeatedly executed until the loop condition is false. The while loop is an entry condition loop, so its loop body may never be executed even once.

The do-while loop statement is an exit condition loop. As a result, its loop body will be executed at least once. Entry condition loops are more frequently used than exit condition loops. However, do-while loops are particularly suited for receiving and processing repeated user input.

The for loop is, like the while loop, an entry condition loop, but as opposed to the while loop, it confines the important parts of a loop (loop condition, loop initialization, and loop update) to one place in the source code. This makes it easier to focus on the parts that need to be modified to make the for loop work correctly. The for loop is a flexible construct that allows you, with the use of the comma operator, to include more than one loop initialization and more than one loop update.

The break statement can be used to jump out of an iteration statement to continue execution at the statement following the iteration statement.

The continue statement jumps over the rest of the statements in the loop body and continues with the next loop.

Structured programming is focused on how to write the detailed parts of computer programs found inside individual methods. Its basic philosophy is simply stated as "Only use control constructs that have one entry and one exit point." This basic rule can easily be broken when using the error-prone goto statement.

The combination assignment operator plus equals (+=) allows you to write

 number += 10; 

instead of

 number = number + 10; 

The same ability runs parallel for the combinations assignment operators -=, *=, /=, %=.

Iteration statements can be nested inside each other to any required level. Two nested iteration statements are suited for generating and processing two-dimensional data. Three nested iteration statements are suited for three-dimensional data and so forth.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net