Lesson 5 – Conditional Statements in C#

Conditional statements are part of almost every programming language and C# is no exception. Conditional statements are used for decision making in a software application. They help you execute a particular piece of code depending upon a certain condition. The conditional statements always return a boolean value. For instance, in a login system, the conditional statements are used to check if the name and password entered by the user are correct. There four main types of conditional statements in C#.

  1. IF Statement
  2. IF/Else Statement
  3. Switch Statement

If Statement

The “if statement” is used to evaluate a single condition. If the condition returns true, the code block for the if statement will execute. For instance, in the following example, we check if the value of the “password” variable is “abc123”. If the condition returns true, a welcome message will be printed on the console:

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            string password = "abc123";
            if (password == "abc123")
            {
                Console.WriteLine("Login Successful, Welcome!");
            }
        }
    }
}

Since in the above script the value of “password” variable is actually “abc123”, the code block that follows the if statement will execute and you will see the following message on the console:

Login Successful, Welcome!

If you change the value of the “password” variable to something other than “abc123”, you will see nothing printed on the console.

What if you also want to display a message to the user that login failed, in case the “password” variable contains a value other than “abc123”? This is where IF/Else statements come to play.

IF/Else Statement

If you want to execute an alternate piece of code in case the If condition returns false, you can use the Else block. In the following example, the if condition checks if the value of the “password” variable is “abc123”. If the condition returns false, the else block will print a message that login failed.

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {
            string password = "abc13";
            if (password == "abc123")
            {
                Console.WriteLine("Login Successful, Welcome!");
            }
            else
            {
                Console.WriteLine("Login failed, Please try again!");
            }
        }
    }
}

The output of the above script is as follows:

Login failed, Please try again!

Switch Statement

If you have to evaluate multiple conditions, using if/else statements can result in a cluttered code. A better way is to use switch statements as follows:

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {

            string password = "abc123";
            switch (password)
            {
                case "abc123":
                    Console.WriteLine("Login successful");
                    break;
                case "abc":
                    Console.WriteLine("The password is incomplete");
                    break;
                default:
                    Console.WriteLine("The password is not correct");
                    break;
            }
        }
    }
}

The output of the above script is as follows:

Login successful

What’s Next?

In this lesson, you studied conditional statements, in the next lesson we will start our discussion about loops.