BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Program Flow Control: Conditional Processing

The thirty-second part of the C# Fundamentals tutorial concludes the examination of the program flow control commands available within C#. This article considers the conditional processing commands that allow code to be executed when tests are met.

The If Statement

The if statement is the most commonly used conditional processing statement. In its simplest format, the statement defines a predicate and only executes a piece of code if the condition is met. The syntax of the statement is as follows:

if (condition) command;

The condition defines a predicate that must be met. If the conditional is true, the command is executed. If false, the command is ignored and processing continues with the next statement. The command may be a single command or a group of commands in a code block.

int toTest = 5;

if (toTest >= 0) Console.WriteLine("Positive");     // Outputs "Positive"
if (toTest < 0) Console.WriteLine("Negative");      // Outputs nothing

The If-Else Statement

Often you will want to execute one piece of code if a condition is met and alternative code if not. To achieve this, the if statement can be extended with an else section:

if (condition) command; else alternative-command;

Using the above syntax, the condition section must still evaluate to a Boolean value. If the Boolean value is true when the if statement is executed, the command statement or code block is executed. If the Boolean value is false, the alternative-command is executed. In all cases, one of the two commands will be run.

int toTest = 5;

if (toTest >= 0)
    Console.WriteLine("Positive");      // Outputs "Positive"
else
    Console.WriteLine("Negative");

Nested If Statements

As a program increases in complexity, the number of conditions that must be processed may increase. Sometimes you need to nest if statements to achieve the desired results. Nesting may occur in the main body of the if statement, in the else section or both. When nesting if statements you should indent the code for readability.

int toTest = 5;

if (toTest >= 0)
{
    if (toTest == 0)
        Console.WriteLine("Zero");
    else
        Console.WriteLine("Positive");  // Outputs "Positive"
}
else
{
    Console.WriteLine("Negative");
}

The "If-Else-If" Ladder

The If-Else-If ladder is a combination of statements that is used to test a series of conditions. The first if statement performs a standard Boolean test. If the test is met, the code within the if executes. If not, control passes to the else statement, which contains a second "if" command. This continues as a series of if statements nested within the previous command's else until all conditions that require testing have been checked. A default command or code block may execute when no condition has evaluated to true by adding a final else. The syntax for the if-else-if ladder is as follows:

if (condition 1)
    command 1;
else if (condition 2)
    command 2;
else if (condition 3)
    command 3;
.
.
.
else if (condition X)
    command X;
else
    default command

Using a ladder, any number of conditions may be checked and only one condition's associated code will ever be executed. However, the number of checks should not be too high as to make the code difficult to read. Note that although the structure is simply a series of nested if commands, the indentation is minimal to show that a ladder structure is in operation and to improve readability.

The following example uses an if-else-if ladder structure to categorise a value according to the range it falls within:

int toTest = 5;

if (toTest < 0)
    Console.WriteLine("Negative");
else if (toTest == 0)
    Console.WriteLine("Zero");
else if (toTest < 5)
    Console.WriteLine("Small");
else if (toTest < 20)
    Console.WriteLine("Medium");        // Outputs "Medium"
else if (toTest < 50)
    Console.WriteLine("Large");
else
    Console.WriteLine("Huge");
11 March 2007