C# Decision Statements

C# Code Snippets Decision Statements
Share:

About

In this code snippet, we will learn how to use the if, else if and switch decision statements in C#.

The if decision statement will evaluate the expression provided in the brackets. If the expression is true the first code block executes else the other code block executes.

The switch statement is useful when you have a lot of checks to perform. You pass the value to be compared into the switch statement. Then you can write multiple cases for different possible values. 

Now let’s see how to do it in the code below.

Code:

using System;

namespace DecisionStatements
{
    class Program
    {
        static void Main(string[] args)
        {
            //The if decison statement will evaluate the provided expression. 
            //If the expression is true the first code block executes else the other code block executes.

            //Here are some examples of the if descison statement.
            bool c = true;

            if (c)
            {
                Console.WriteLine("c is true");
            }


            int a = 5;
            int b = 10;

            if (a > b)
            {
                Console.WriteLine("a is bigger than b");
            }
            else
            {
                Console.WriteLine("b is bigger than a");
            }
            

            string str = "Hello World";

            //Multiple checks can be added with the else if statement.
            if (str == "Hello World")
            {
                Console.WriteLine("The string is \"Hello World\"");
            }
            else if (str == "Hello")
            {
                Console.WriteLine("The string is \"Hello\"");
            }
            else
            {
                Console.WriteLine("The string contains something else: " + str);
            }


            //The switch statement is useful when you have a lot of checks to perform.
            string strNumber = "1"; 

            switch (strNumber)
            {
                case "1" : Console.WriteLine("The passed in string is a number.");
                break;

                case "2": Console.WriteLine("The passed in string is a number.");
                break;

                case "3": Console.WriteLine("The passed in string is a number.");
                break;

                case "4": Console.WriteLine("The passed in string is a number.");
                break;

                //If the value passed into the switch statement doesn't match up with any of the cases above the default case will execute. 
                default: Console.WriteLine("The passed in string is not a number between 1 and 4.");
                break;
            }

            Console.ReadLine();
        }
    }
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger