C Sharp Flow Control with if and else

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
C# Operators and ExpressionsThe C# switch Statement


Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99

Buy eBook


In the chapter titled C# Operators and Expressions we looked at how to use logical expressions in C# to determine whether 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 if statements in a little more detail.




Using the if Statement

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 .. Statements

The next variation of the 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:

      int x = 10;

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

In this case, the second WriteLine statement would execute if the value of x was less than 9.


Using if ... else if .. Statements

So far we have looked at if statements which make decisions based on the result of a single logical expression. Sometimes it becomes necessary to make decisions based on a number of different criteria. For this purpose we can use the if ... else if ... construct, the syntax for which is as follows:

                int x = 9;

                if (x == 10)
                {
                     System.Console.WriteLine ("x is 10");
                }
                else if (x == 9)
                {
                     System.Console.WriteLine ("x is 9");
                }
                else if (x == 8)
                {
                     System.Console.WriteLine ("x is 8");
                }

This approach works well for a moderate number of comparisons, but can become cumbersome for a larger volume of expression evaluations. For such situations, the C# switch statement provides a more flexible and efficient solution. For more details on using the switch statement read the chapter entitled The C# switch Statement.

Summary

In this chapter we looked at the use of C# if statements including if, if ... else and if ... else if ... constructs. In the next chapter of this book we will look at using the C# switch statement as an alternative to more complex if statements.


Purchase and download the full PDF and ePub versions of this Visual C# eBook for only $9.99

Buy eBook



PreviousTable of ContentsNext
C# Operators and ExpressionsThe C# switch Statement