C# - Decisions And Branching

Decisions And Branching

Changing the flow of control in a program in response to some kind of input or calculated value is an essential part of a programming language. C# provides the ability to change the flow of control, either unconditionally, by jumping to a new location in the code, or conditionally, by performing a test.

Conditional Statements

Conditional statements allow you to branch your code depending on whether certain conditions are met or the value of an expression. C# has two constructs for branching code: the if statement, which allows you to test whether a specific condition is met; and the switch statement, which allows you to compare an expression with several different values.

if Statement :

It executes its block only if the condition is true

if ( condition )
{
    //statement;
}

if else Statement

It executes if block if condition is true; otherwise it will execute the else block

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

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 clause 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)

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 switch Statement

The second of C#’s selection statements is switch. The switch provides for a multiway branch. Thus, it enables a program to select among several alternatives. Although a series of nested if statements can perform multiway tests, for many situations the switch is a more efficient approach. It works like this: The value of an expression is successively tested against a list of constants. When a match is found, the statement sequence associated with that match is executed. The general form of the switch statement is

switch(expression) 
{
    case constant1:
        statement sequence
    break;
    case constant2:
        statement sequence
    break;
    case constant3:
        statement sequence
    break;
    .
    .
    .
    default:
        statement sequence
    break;
}

The switch expression must be of an integer type, such as char, byte, short, or int, of an enumeration type, or of type string.

Thus, floating-point expressions, for example, are not allowed.

Switch statements are typically faster than equivalent if-else-if statements due to compiler optimizations.

The compiler can build jump tables where applicable. For example, when you use the reflector to look at the code produced, you will see that for huge switches on strings, the compiler will actually generate code that uses a hash table to dispatch these. The hash table uses the strings as keys and delegates to the case codes as values.

This has asymptotic better runtime than lots of chained if tests and is actually faster even for relatively few strings.

Nested switch Statements

It is possible to have a switch as part of the statement sequence of an outer switch. This is called a nested switch. The case constants of the inner and outer switch can contain common values and no conflicts will arise. For example, the following code fragment is perfectly acceptable:

switch(ch1) 
{
    case 'A': Console.WriteLine("This A is part of outer switch.");
        switch(ch2) 
        {
        case 'A':
            Console.WriteLine("This A is part of inner switch");
        break;
        case 'B': // ...
        } // end of inner switch
    break;
    case 'B': // ...
}