C# Tutorial Lesson 10: Flow Control (2): Jump and Selection Statements

The jump statements include

break
continue
goto
return (see lesson 13)
throw (see lesson 17)

break

The 'break' statement breaks out of the 'while' and 'for' loops covered in lesson 9, and the 'switch' statements covered later in this lesson. The following code gives an example - albeit a very inefficient one - of how it could be used. The output of the loop is the numbers from 0 to 4.

1.

int a = 0;

2.

while (true)

3.

{

4.

    System.Console.WriteLine(a);

5.

    a++;

6.

    if (a == 5)

7.

        break;

8.

}


continue

The 'continue' statement can be placed in any loop structure. When it executes, it moves the program counter immediately to the next iteration of the loop. The following code example uses the 'continue' statement to count the number of values between 1 and 100 inclusive that are not multiples of seven. At the end of the loop the variable y holds the required value.

1.

int y = 0;

2.

for (int x=1; x<101; x++)

3.

{

4.

    if ((x % 7) == 0)

5.

        continue;

6.

    y++;

7.

}


goto

The 'goto' statement is used to make a jump to a particular labelled part of the program code. It is also used in the 'switch' statement described below. We can use a 'goto' statement to construct a loop, as in the following example (but again, this usage is not recommended):

1.

int a = 0;

2.

start:

3.

System.Console.WriteLine(a);

4.

a++;

5.

if (a < 5)

6.

    goto start;


Selection Statements

C# offers two basic types of selection statement:

if - else
switch - default

if - else

'If-else' statements are used to run blocks of code conditionally upon a boolean expression evaluating to true. The 'else' clause, present in the following example, is optional.

1.

if (a == 5)

2.

    System.Console.WriteLine("A is 5");

3.

else

4.

    System.Console.WriteLine("A is not 5");


If statements can also be emulated by using the conditional operator. The conditional operator returns one of two values, depending upon the value of a boolean expression. To take a simple example, the line of code

int i = (myBoolean) ? 1 : 0 ;

sets i to 1 if myBoolean is true, and sets i to 0 if myBoolean is false. The 'if' statement in the previous code example could therefore be written like this:

1.

System.Console.WriteLine( a==5 ? "A is 5" : "A is not 5");


switch - default

'Switch' statements provide a clean way of writing multiple if - else statements. In the following example, the variable whose value is in question is 'a'. If a equals 1, then the output is 'a>0'; if a equals 2, then the output is 'a>1 and a>0'. Otherwise, it is reported that the variable is not set.

1.

switch(a)

2.

{

3.

    case 2:

4.

        Console.WriteLine("a>1 and ");

5.

        goto case 1;

6.

    case 1:

7.

        Console.WriteLine("a>0");

8.

        break;

9.

    default:

10.

        Console.WriteLine("a is not set");

11.

        break;

12.

}


Each case (where this is taken to include the 'default' case) will either have code specifying a conditional action, or no such code. Where a case does have such code, the code must (unless the case is the last one in the switch statement) end with one of the following statements:

break;
goto case k; (where k is one of the cases specified)
goto default;

From the above it can be seen that C# 'switch' statements lack the default 'fall through' behaviour found in C++ and Java. However, program control does fall through wherever a case fails to specify any action. The following example illustrates this point; the response "a>0" is given when a is either 1 or 2.

1.

switch(a)

2.

{

3.

    case 1:

4.

    case 2:

5.

        Console.WriteLine("a>0");

6.

        break;

7.

    default:

8.

        Console.WriteLine("a is not set");

9.

        break;

10.

}


Link Building Information