C Sharp Flow Control with if and else

From Techotopia
Revision as of 16:06, 15 January 2008 by Neil (Talk | contribs) (New page: In the chapter titled C# Operators and Expressions we looked at how to use logical expressions in C# to determine is something is ''true'' or ''false''...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

In the chapter titled C# Operators and Expressions we looked at how to use logical expressions in C# to determine is something is true or false. Since programming is largely an exercise in applying logic, much of the art of programming involves writing code that makes decisions based on one or more criteria. Such decisions define which code gets executed and, conversely, which code gets by-passed when the program is executing. This is often referred to as flow control since it controls the flow of program execution.

In previous chapters the if statement has been used in some examples. In this chapter of C# Essentials we are going to look at the if statements in a little more details.

Using if to Make Decisions

The if statement is perhaps the most basic of flow control options available to the C# programmer. Programmers who are familiar with C, C++ or Java will immediately be comfortable using C# if statements.

The basic syntax of C# if statement is as follows:

if (boolean expression) {

      // C# code to be performed when expression evaluates to true here

}

Essentially if the boolean expression evaluates to true (see C# Operators and Expressions for more details of this type of logic) then the code in the body of the statement is executed. The body of the statement is enclosed in braces ({}). If, on the other hand, the expression evaluates to false the code in the body of the statement is skipped.

For example, if a decision needs to be made depending on whether one value is greater than another:

      int x = 10;

      if ( x > 9 )
      {
               System.Console.WriteLine ("x is greater than 9!");
      }

Clearly, x is indeed greater than 9 causing the message to appear in the console window.

Using if ... else .. to Make Decisions

The next variation of if statement allows us to also specify some code to perform if the expression in the if statement evaluates to false. The syntax for this construct is as follows:

If (boolean expression) {

    // Code to be executed if expression is true 

} else {

    // Code to be executed if expression is false

}

Using the above syntax, we can now extend our previous example to display a different message if the comparison expression evaluates to be false:


In this case, the strings to do not match, so the second MessageBox will display.


Using If ... Then .. ElseIf to Make Decisions

The final variation of the If .. Then structure is the If .. Then .. ElseIf construct. This allows multiple expressions to be evaluated in a single structure. The syntax of If .. Then .. ElseIf is as follows:

If expression Then

.. Code to be Executed is expression is True ..

ElseIf expression Then

.. Code to be executed is expression is True

ElseIf expression Then

.. Code to be executed is expression is True

Endif

For example, the following code construct uses the ElseIf approach to find a match to a string:

        Dim myName As String = "John"

        If myName = "Fred" Then
            MessageBox.Show("Hello Fred")
        ElseIf myName = "John" Then
            MessageBox.Show("Hello John")
        ElseIf myName = "Paul" Then
            MessageBox.Show("Hello Paul")
        End If

This approach works well for a moderate number of comparisons, but can become cumbersome for a larger volume of values or expression evaluations. For such situations, the Select Case construct provides a more flexible and efficient solution.