Flow Control And Iteration


Most of the flow control statements are conceptually and syntactically very similar to Java's. Here's a brief summary:

 if...else  if...else if(option == 1) { //do something } else if(option == 2) { //do something else } else { //do this if none of other options are selected } switch switch(option) { case 1: //do something break; case 2: //do something else break; default: break; } 

You should note that the C# version of switch (unlike Java's) all but prohibits fall-through. All case clauses must end with a break, unless the case clause is empty. To jump from one case clause to another you must use a goto statement.

 for for (int i = 0; i <10; i++) { // iterates 10 times } while bool condition = false; while (!condition) { // do something that may alter the value of the condition Boolean } do...while bool condition; do { // do something that may alter the value of the condition Boolean // at least one iteration occurs whatever the initial value of condition } while (condition); foreach 

C# introduces a foreach statement, used specifically to iterate through, and not change collection or array entries to get the desired information. Changing the contents might have unpredictable side effects. The foreach statement usually takes the following form:

 foreach (ItemType item in TargetCollection) 

ItemType represents the data type stored in the collection or array and TargetCollection represents the actual array or collection. A collection you want to iterate through using the foreach statement must meet two sets of requirements. The first set has to do with the composition of the collection itself. They are as follows:

  • The collection type must be an interface, class, or struct.

  • The collection type must include a GetEnumerator() method for returning an enumerator type. An enumerator type is basically an object that allows you to step through a collection item by item.

The second set of requirements deal with the composition of the enumerator type returned by the GetEnumerator() method just mentioned. Here is the list of requirements:

  • The enumerator should provide a Boolean method MoveNext().

  • MoveNext() should return true if there are more items in the collection.

  • MoveNext() should step to the next item in the collection at each invocation.

  • The enumerator type must provide a property named Current that returns an ItemType (or a type that can be converted to ItemType).

  • The property accessor Current should return the current element of the collection.

The following snippet of C# code uses foreach to iterate through a Hashtable collection:

 Hashtable t = new System.Collections.Hashtable(); t["a"] = "hello"; t["b"] = "world"; t["c"] = "of"; t["d"] = "c-sharp"; foreach(System.Collections.DictionaryEntry b in t) { Console.WriteLine( b.Value ); } 

You learned about the break statement in the discussion of switch; this statement can be used to exit from any flow control or iterative statement. The continue statement forces the end of the current iteration of an iterative statement, whereas return is used in a method to return control to the caller ofthe method.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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