Traversing, Analyzing, and Generating Sequences of Data

   


When a program visits each element of a list of elements, programmers say the list is being traversed. Traversal is one of the most fundamental uses of iteration statements. The general loop construct for analyzing a list of data can be depicted by a flow chart or pseudocode as in Figure 9.1. Notice how the depiction matches our definition of an iteration statement presented in the beginning of the previous chapter: "An iteration statement repeats a statement or a block of statements until a given termination condition is met."

Figure 9.1. An iteration construct used for analyzing a sequence of data elements.
graphics/09fig01.gif

Figure 9.2 illustrates the general framework for a loop generating a list of data elements. Notice how it is merely a variation of the same theme as that shown in Figure 9.1.

Figure 9.2. An iteration construct used for generating a sequence of data elements.
graphics/09fig02.gif

There are many variations of the iteration theme, apart from the few already presented. For example, only every second element might be visited, perhaps the algorithm works faster if the list is processed from the end and backwards, or perhaps the best way is to start in the middle and work towards the beginning and end simultaneously, and so on. Fortunately, C# contains three different iteration constructs that cater to any need you might encounter. Let's have a closer look at their syntax and general form before we explore their practical use.


   


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

Similar book on Amazon

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