Two Control Statements


Inside a method, execution proceeds from one statement to the next, top to bottom. It is possible to alter this flow through the use of the various program control statements supported by C#. Although we will look closely at control statements later, two are briefly introduced here because we will be using them to write sample programs.

The if Statement

You can selectively execute part of a program through the use of C#’s conditional statement: the if. The if statement works in C# much like the IF statement in any other language. For example, it is syntactically identical to the if statements in C, C++, and Java. Its simplest form is shown here:

 if(condition) statement;

Here, condition is a Boolean (that is, true or false) expression. If condition is true, then the statement is executed. If condition is false, then the statement is bypassed. Here is an example:

 if(10 < 11) Console.WriteLine("10 is less than 11");

In this case, since 10 is less than 11, the conditional expression is true, and WriteLine( ) will execute. However, consider the following:

 if(10 < 9) Console.WriteLine("this won't be displayed");

In this case, 10 is not less than 9. Thus, the call to WriteLine( ) will not take place.

C# defines a full complement of relational operators that can be used in a conditional expression. They are shown here:

Operator

Meaning

<

Less than

<=

Less than or equal

>

Greater than

>=

Greater than or equal

= =

Equal to

!=

Not equal

Here is a program that illustrates the if statement:

 // Demonstrate the if. using System; class IfDemo {   public static void Main() {     int a, b, c;     a = 2;     b = 3;     if(a < b) Console.WriteLine("a is less than b");     // this won't display anything     if(a == b) Console.WriteLine("you won't see this");     Console.WriteLine();          c = a - b; // c contains -1     Console.WriteLine("c contains -1");     if(c >= 0) Console.WriteLine("c is non-negative");     if(c < 0) Console.WriteLine("c is negative");     Console.WriteLine();     c = b - a; // c now contains 1     Console.WriteLine("c contains 1");     if(c >= 0) Console.WriteLine("c is non-negative");     if(c < 0) Console.WriteLine("c is negative");   } }

The output generated by this program is shown here:

 a is less than b c contains -1 c is negative c contains 1 c is non-negative

Notice one other thing in this program. The line

 int a, b, c;

declares three variables, a, b, and c, by use of a comma-separated list. As mentioned earlier, when you need two or more variables of the same type, they can be declared in one statement. Just separate the variable names by commas.

The for Loop

You can repeatedly execute a sequence of code by creating a loop. C# supplies a powerful assortment of loop constructs. The one we will look at here is the for loop. Like the if statement, the C# for loop is similar to its counterpart in C, C++, and Java. The simplest form of the for loop is shown here:

 for(initialization; condition; iteration) statement;

In its most common form, the initialization portion of the loop sets a loop control variable to an initial value. The condition is a Boolean expression that tests the loop control variable. If the outcome of that test is true, the for loop continues to iterate. If it is false, the loop terminates. The iteration expression determines how the loop control variable is changed each time the loop iterates. Here is a short program that illustrates the for loop:

 // Demonstrate the for loop. using System; class ForDemo {   public static void Main() {     int count;     for(count = 0; count < 5; count = count+1)       Console.WriteLine("This is count: " + count);     Console.WriteLine("Done!");   } }

The output generated by the program is shown here:

 This is count: 0 This is count: 1 This is count: 2 This is count: 3 This is count: 4 Done!

In this example, count is the loop control variable. It is set to zero in the initialization portion of the for. At the start of each iteration (including the first one), the conditional test count < 5 is performed. If the outcome of this test is true, the WriteLine( ) statement is executed. Next, the iteration portion of the loop is executed, which adds 1 to count. This process continues until count reaches 5. At this point, the conditional test becomes false, causing v the loop to terminate. Execution picks up at the bottom of the loop.

As a point of interest, in professionally written C# programs, you will almost never see the iteration portion of the loop written as shown in the preceding program. That is, you will seldom see statements like this:

 count = count + 1;

The reason is that C# includes a special increment operator that performs this operation more efficiently. The increment operator is ++ (that is, two consecutive plus signs). The increment operator increases its operand by one. By use of the increment operator, the preceding statement can be written like this:

 count++;

Thus, the for in the preceding program will usually be written like this:

 for(count = 0; count < 5; count++)

You might want to try this. As you will see, the loop still runs exactly the same as it did before.

C# also provides a decrement operator, which is specified as – –. This operator decreases its operand by one.




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