C# Control Structures

Most control structures in C# are similar to those in C or C++, but there are some specific differences and extensions.

Also see the page on exception handling.


Decision Structures

if and else

Format:

     if (logical condition)
      {
       ...
      }
     else
      {
       ...
      }

There are few surprises here. As usual, the else section is optional. We can have nested if structures. If the body of an "if" or and "else" is just one line of code, the braces are not needed.

Bear in mind that logical conditions must evaluate to true or false, not numeric values. We have various operators for forming compound conditions:

switch, case and default

This resembles the C++ switch, but:


Iteration Structures

while

There are few surprises here. The format is:

     while (logical condition)
      {
       ...
      }

This is a top-tested loop which continues until the condition becomes false or until the execution of a break, goto, return or throw statement.

Again, here and in the other loop constructs, if the body of the loop is just one line, we do not need the braces.

do

This is used to create a bottom-tested loop. The format is:

     do
      {
       ...
      }
     while (logical condition);

for

There are few surprises here. The format is:

     for (initializer; condition; iterator)
      {
       ...
      }

This is a top-tested loop. Each of the three sections (initializer, condition, iterator) is optional. (Without the condition, we have an infinite loop.)

The initializer and iterator may contain more than one instruction (separated by commas), but usually each is just one instruction or expression.

foreach and in

This is used to iterate through an array or a suitable enumerated container. The format is:

     foreach (type Name1 in Name2)
      {
       ...
      }

Here Name2 is an array or a container of items of the indicated type.

For example, suppose Tab is an array of integers. We might have

     int Total = 0;
     foreach (int X in Tab)
     {
      Total = Total + X;
     }

Notice that "in" has another meaning as well, involving parameters.

continue

If a continue statement is executed in a loop (for, foreach or while), control returns to the top of the loop, skipping whatever may follow.

break

The break statement terminates the closest enclosing loop (do, while, for or foreach) or switch statement in which is occurs. (Notice that we can have nested structures. We could use break to end an inner loop and still be executing an outer loop starting at the line after the end of the inner loop.)


Miscellaneous

goto

The goto statement transfers control directly to a labeled statement such as:

It is worth noticing that we can have labels on statements:

     label:  statement;

A label exists in some scope and can be referenced only within that scope.

The goto concept is considered rather old-fashioned by some people but it is sometimes convenient, as in:

     for (...) 
     {
      for (...) 
      {
       ...
       if (something)
        goto end_of_loop;
      }
     }
     end_of_loop;

Here goto allows us to break out of nested loops.