Changes

Jump to: navigation, search

C Sharp Flow Control with if and else

3,768 bytes added, 16:06, 15 January 2008
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''...
In the chapter titled [[C Sharp Operators and Expression|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 Sharp Essentials|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 Sharp Operators and Expression|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:

<pre>
int x = 10;

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

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'':

<pre>

</pre>

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:

<pre>
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
</pre>

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.

Navigation menu