Flow Controls (conditional statements) in C#

We will go through the different flow control structs in C#. These statements are used to make decisions and execute different paths of code based on a specific conditions.

if…else Statement

One of the most important construct in any language is condition checking.

In C#, we can do this in the form of if..else statement.

Below is the structure of the statement:

if (boolean expression)
  Statement or block of statements
else
  Statement of block of statements

Example of if..else statement:

class Program
{
    static void Main(string[] args)
    {
        int grade = 12;
        if (grade > 75)
        {
            Console.WriteLine("Congratulations!");
        }
        else
        {
            Console.WriteLine("Sorry! Please try again.");
        }
    }
}

unassigned

if…elseif Statement

Example of if..elseif statement:

class Program
{
    static void Main(string[] args)
    {
        int grade = 78;
        if (grade > 85)
        {
            Console.WriteLine("Congratulations!");
        }
        else if (grade > 75)
        {
            Console.WriteLine("Nice Try!");
        }
        else
        {
            Console.WriteLine("Sorry! Please try again.");
        }
    }
}

unassigned

nested if…else Statement

Since if...else is also a statement, you can use it inside if..else or what we called nested if statements:

class Program
{
    static void Main(string[] args)
    {
        int grade = 90;
        if (grade > 75)
        {
            if (grade > 85)
            {
                Console.WriteLine("Great!");
            }
            else
            {
                Console.WriteLine("Congratulations!");
            }
        }
        else
        {
            Console.WriteLine("Sorry! Please try again.");
        }
    }
}

unassigned

To avoid confusions when using nested statements, here are some guidelines:

  • Habit of using { } brackets with every if and else

  • Indentation: aligning the code to enhance readability. Visual Studio will help with the auto indentation.

switch…case Statement

In performing a series of checks, switch..case can be used instead of an if..else.

The general structure of switch...case is below:

switch (integral or string expression)
{
    case constant-expression:
      statements
      breaking or jump statement
    // some other case blocks
    ...
    default:
      statements
      breaking or jump statement
}

Here is a sample code on how to use switch..case in C#:

class Program
{
    static void Main(string[] args)
    {
        int yearlevel = 2;
        switch (yearlevel)
        {
            case 1:
                Console.WriteLine("Freshman");
                break;
            case 2:
                Console.WriteLine("Sophomore");
                break;
            case 3:
                Console.WriteLine("Junior");
                break;
            case 4:
                Console.WriteLine("Senior");
                break;
            default:
                Console.WriteLine("invalid number");
                break;
        }
    }
}

unassigned


🔚 end of document 🔚

References