9. Flow of Control Part II: Iteration Statements

   


Chapter 9. FLOW OF CONTROL PART II: ITERATION STATEMENTS

You will learn about the following in this chapter:

  • The essential nature of iteration statements and the typical situations where they are called for

  • The while, do-while, and for-loops and how to work with these iteration statements

  • How the jump statements break and continue can be applied to exert additional control over the iteration statements

  • The combination assignment operators

  • How iteration statements can be positioned inside each other to form nested loops and the usefulness of these types of constructs

  • The concept of structured programming and its advantages

Without the ability to repeatedly perform the same set of operations, as facilitated by the iteration statements, the time it would take a program to run from its first statement to its last statement would be directly proportional to the length of the program. Often, computer users work on their computer for many hours (days), letting the computer solve the same tasks repeatedly, but typically involving different data during each repeated execution. Writing a document using a word processor is an example. Each time a character is typed the program repeats some actions. This would require immensely sized programs that no programmer would ever want to program were it not for the presence of the iteration statements. Consequently, iteration is one of the cornerstones in computing without which programmers would find it hard to create programs of much use.

The imperative nature of the iteration statements is reflected by their early introduction in this book and casual use in many examples prior to their thorough discussion in this chapter. In Chapter 2, "Your First C# Program," you were presented (using pseudocode) with an average-calculating algorithm based on iteration to help our alien friend from planet Blipos. Chapter 6, "Types Part I: The Simple Types" provided you with an informal introduction to the do-while loop as a means to create a Blipos clock, and Chapter 7, "Types Part II: Operators, Enumerators and Strings" utilized the while-loop to: create a day counter program, analyze text, order words in alphabetical order and to extract words of a certain length from a string.

It is interesting to note that in each program the iteration algorithm was employed to handle data of a repetitive nature. For example, the list of numbers analyzed by the average calculating algorithm in Chapter 2 could be written as

 {number1, number2, number3, number4...} 

and the series of times generated by the Blipos clock could, in a general form, be written as

 {time1, time2, time3, time4...} 

The remaining examples are listed in Table 9.1.

Table 9.1. The Repetitive Nature of Data Related to Iterative Statements
Name of example Type of sequence processed Analyzing or Generating
Average-calculating algorithm A list of numbers Analyzing
Blipos clock A series of seconds and minutes Generating
Day counter program A sequence of days Generating
Text analyzer A string of characters Analyzing
Word extractor A string of characters (and ultimately a list of words) Analyzing

Also note, as indicated in Table 9.1, that each example either uses the iteration statement to analyze a sequence of given data or to generate a sequence of data. Analyzing and generating data are important uses for the loop statements.


   


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