Chapter 5: Program Control Statements


This chapter discusses C#’s program control statements. There are three categories of program control statements: selection statements, which are the if and the switch; iteration statements, which consist of the for, while, do-while, and foreach loops; and jump statements, which include break, continue, goto, return, and throw. Except for throw, which is part of C#’s exception-handling mechanism and is discussed in Chapter 13, the others are examined here.

The if Statement

Chapter 2 introduced the if statement. It is examined in detail here. The complete form of the if statement is

 if(condition) statement; else statement;

where the targets of the if and else are single statements. The else clause is optional. The targets of both the if and else can be blocks of statements. The general form of the if using blocks of statements is

 if(condition) {   statement sequence } else {   statement sequence } 

If the conditional expression is true, the target of the if will be executed; otherwise, if it exists, the target of the else will be executed. At no time will both of them be executed. The conditional expression controlling the if must produce a bool result.

Here is a simple example that uses an if and else statement to report if a number is positive or negative:

 // Determine if a value is positive or negative. using System; class PosNeg {   public static void Main() {     int i;     for(i=-5; i <= 5; i++) {       Console.Write("Testing " + i + ": ");       if(i < 0) Console.WriteLine("negative");       else Console.WriteLine("positive");     }   } }

The output is shown here:

 Testing -5: negative Testing -4: negative Testing -3: negative Testing -2: negative Testing -1: negative Testing 0: positive Testing 1: positive Testing 2: positive Testing 3: positive Testing 4: positive Testing 5: positive

In this example, if i is less than zero, then the target of the if is executed. Otherwise, the target of the else is executed. In no case are both executed.

Nested ifs

A nested if is an if statement that is the target of another if or else. Nested ifs are very common in programming. The main thing to remember about nested ifs in C# is that an else statement always refers to the nearest if statement that is within the same block as the else and not already associated with an else. Here is an example:

 if(i == 10) {   if(j < 20) a = b;   if(k > 100) c = d;   else a = c; // this else refers to if(k > 100) } else a = d; // this else refers to if(i == 10)

As the comments indicate, the final else is not associated with if(j<20), because it is not in the same block (even though it is the nearest if without an else). Rather, the final else is associated with if(i==10). The inner else refers to if(k>100), because it is the closest if within the same block.

The following program demonstrates a nested if. In the positive/negative program shown earlier, zero is reported as positive. However, for some applications, zero is considered signless. The following version of the program reports zero as being neither positive nor negative:

 // Determine if a value is positive, negative, or zero. using System; class PosNegZero {   public static void Main() {     int i;     for(i=-5; i <= 5; i++) {       Console.Write("Testing " + i + ": ");       if(i < 0) Console.WriteLine("negative");       else if(i == 0) Console.WriteLine("no sign");         else Console.WriteLine("positive");     }   } }

Here is the output:

 Testing -5: negative Testing -4: negative Testing -3: negative Testing -2: negative Testing -1: negative Testing 0: no sign Testing 1: positive Testing 2: positive Testing 3: positive Testing 4: positive Testing 5: positive

The if-else-if Ladder

A common programming construct that is based upon the nested if is the if-else-if ladder. It looks like this:

 if(condition)   statement; else if(condition)   statement; else if(condition)   statement; . . . else   statement;

The conditional expressions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. The final else often acts as a default condition. That is, if all other conditional tests fail, then the last else statement is performed. If there is no final else and all other conditions are false, then no action will take place.

The following program demonstrates the if-else-if ladder. It finds the smallest singledigit factor for a given value.

 // Determine smallest single-digit factor. using System; class Ladder {   public static void Main() {     int num;     for(num = 2; num < 12; num++) {       if((num % 2) == 0)         Console.WriteLine("Smallest factor of " + num + " is 2.");       else if((num % 3) == 0)         Console.WriteLine("Smallest factor of " + num + " is 3.");       else if((num % 5) == 0)         Console.WriteLine("Smallest factor of " + num + " is 5.");       else if((num % 7) == 0)         Console.WriteLine("Smallest factor of " + num + " is 7.");       else         Console.WriteLine(num + " is not divisible by 2, 3, 5, or 7.");     }   } }

The program produces the following output:

 Smallest factor of 2 is 2. Smallest factor of 3 is 3. Smallest factor of 4 is 2. Smallest factor of 5 is 5. Smallest factor of 6 is 2. Smallest factor of 7 is 7. Smallest factor of 8 is 2. Smallest factor of 9 is 3. Smallest factor of 10 is 2. 11 is not divisible by 2, 3, 5, or 7.

As you can see, the last else is executed only if none of the preceding if statements succeeds.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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