The cornerstone of any software code is the control flow logic which decides which code should be executed and which should not. In this chapter, we cover the conditional control flow constructs provided by C#.
Looping vs. conditional control flow
Regardless of the programming language used, application development is essentially 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, how often, and which code gets bypassed when the program runs. This is often referred to as control flow since it controls the flow of program execution. Control flow typically falls into the categories of looping control flow (how often code is executed) and conditional control flow (whether code is executed). The lessons in this section are intended to provide an introductory overview of both types of control flow in C#.
Earlier in the book, we used logical expressions in C# to determine whether something is true or false. Since programming is essentially 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 which gets bypassed when the program is running. This is often referred to as control flow since it controls the flow of program execution.
In previous chapters, the if
statement has been used in some examples. In this chapter, we will look at if
statements in more detail.
Using the if
statement
The if
statement is perhaps the most basic of control flow options available to the C# programmer. Programmers familiar with C, C++, or Java will immediately be comfortable using C# if statements.
The basic syntax of the C# if statement is as follows:
if (boolean expression) { // C# code to be performed when expression evaluates to true here }
If the boolean expression evaluates to true
then, the code in the statement’s body 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.
Note: If the body contains only one statement, the enclosing braces ({}
) may be omitted. These must be added once more than one statement is included in the body.
Using if
… else
… statements
The next variation of the if
statement allows us also to specify some code to execute 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 = 8; 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.
As with the standalone if
statement, the braces ({}
) are optional if the body only contains one statement. The following is, therefore, valid code:
int x = 8; if ( x > 9 ) { System.Console.WriteLine ("x is greater than 9!"); } else { System.Console.WriteLine ("x is less than 9!"); }
Using if
… else if
… statements
So far, we have looked at if statements that make decisions based on the result of a single logical expression. Sometimes it becomes necessary to make decisions based on several different criteria. For this purpose, we can use the if
… else if
… construct, the syntax for which is as follows:
if (boolean expression) { // Code to be executed if the expression is true } else if (boolean expression) { // Code to be executed if the expression is true } else if ..... } else { // Code to be executed if previous expressions are all false }
Note: The final else
can be omitted if no action is to be taken if all the boolean expressions evaluate to a false
result.
The following code excerpt demonstrates the use of the if
… else if
… construct:
int x = 11; 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"); } else { System.Console.WriteLine ("x is some other number"); }
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. The switch
statement will be covered in the next chapter.