Decision Making Statements Or Selection Constructs In C#

  • Is a programming construct supported by C# that controls the flow of a program.
  • Executes a particular block of statements based on a boolean condition, which is an expression returning true or false.
  • Is referred to as a decision-making construct.
  • Allow you to take logical decisions about executing different blocks of a program to achieve the required logical output.

C# supports the following decision-making constructs:

  • if…else
  • if…else…if
  • Nested if
  • switch…case
  • Nested Switch Case

The If Statement

  • The if statement allows you to execute a block of statements after evaluating the specified logical condition.
  • The if statement starts with the if keyword and is followed by the condition.
  • If the condition evaluates to true, the block of statements following the if statement is executed.
  • If the condition evaluates to false, the block of statements following the if statement is ignored and the statement after the block is executed.

The following figure displays an example of the if construct:

The If-else Statement

  • In some situations, it is required to define an action for a false condition by using an if…else construct.
  • The if…else construct starts with the if block followed by an else block and the else block starts with the else keyword followed by a block of statements.
  • If the condition specified in the if statement evaluates to false, the statements in the else block are executed.

Example 1:

            int a = 3;

            if (a == 10)
            {
                Console.WriteLine("A is 10");
            }
            else
            {
                Console.WriteLine("A is not 10");
            }

Example 2:

            Console.WriteLine("Enter Username: ");
            string username = Console.ReadLine();

            Console.WriteLine("Enter Password: ");
            string password = Console.ReadLine();

      if (username == "adil123@gmail.com" && password == "Adil123@")
            {
                Console.WriteLine("Login Successfull..");
            }
            else
            {
                Console.WriteLine("Incorrect Username Or Password");
            }

The If-else-if Statement

  • The if…else…if construct allows you to check multiple conditions to execute a different block of code for each condition.
  • It is also referred to as if-else–if ladder.
  • The construct starts with the if statement followed by multiple else if statements followed by an optional else block.
  • The conditions specified in the if…else…if construct are evaluated sequentially.
  • The execution starts from the if statement. If a condition evaluates to false, the condition specified in the following else…if statement is evaluated.

Example:

            Console.WriteLine("Enter your Percentage: ");
            int percentage = int.Parse(Console.ReadLine());

            if (percentage >= 80)
            {
                Console.WriteLine("Grade: A-1");
            }
            else if (percentage >= 70)
            {
                Console.WriteLine("Grade: A");
            }
            else if (percentage >= 60)
            {
                Console.WriteLine("Grade: B");
            }
            else if (percentage >= 50)
            {
                Console.WriteLine("Grade: C");
            }
            else if (percentage >= 40)
            {
                Console.WriteLine("Grade: D");
            }
            else if (percentage >= 33)
            {
                Console.WriteLine("Grade: E");
            }
            else
            {
                Console.WriteLine("Grade: F (Fail)");
            }

Nested If Construct

  • The nested if construct consists of multiple if statements.
  • The nested if construct starts with the if statement, which is called the outer if statement, and contains multiple if statements, which are called inner if statements.
  • In the nested if construct, the outer if condition controls the execution of the inner if statements. The compiler executes the inner if statements only if the condition in the outer if statement is true.
  • In addition, each inner if statement is executed only if the condition in its previous inner if statement is true.

Example 1:

            int a = 3;
            if (a > 5)
            {
                if (a == 10)
                {
                    Console.WriteLine("A is 10");
                }
                else
                {
                    Console.WriteLine("A is not 10");
                }
            }
            else
            {
                if (a == 3)
                {
                    Console.WriteLine("A is 3");
                }
                else
                {
                    Console.WriteLine("A is not 3");
                }
            }

Example 2:

            Console.WriteLine("Enter your email: ");
            string email = Console.ReadLine();

            if (email == "a.ansari20@hotmail.com")
            {
                Console.WriteLine("Enter your password: ");
                string password = Console.ReadLine();
                if (password == "Adil123")
                {
                    Console.WriteLine("Login Successfull");
                }
                else
                {
                    Console.WriteLine("Incorrect Password..");
                }
            }
            else
            {
                Console.WriteLine("Email is invalid.");
            }

Switch… Case Construct

  • A program is difficult to comprehend when there are too many if statements representing multiple selection constructs.
  • To avoid using multiple if statements, in certain cases, the switch…case approach can be used as an alternative.
  • The switch…case statement is used when a variable needs to be compared against different values.
  • In C#, the flow of execution from one case statement is not allowed to continue to the next case statement and is referred to as the ‘no-fall-through’ rule of C#.

Example 1:

            Console.WriteLine("Enter your name: ");
            string name = Console.ReadLine();

            switch (name)
            {
                case "sameed":
                    Console.WriteLine("Hi Sameed");
                    break;
                case "ammad":
                    Console.WriteLine("Hi Ammad");
                    break;
                case "zubia":
                    Console.WriteLine("Hi Zubia");
                    break;
                default:
                    Console.WriteLine("Who are you ??");
                    break;
            }

            Console.WriteLine("Switch Exits..");

Example 2:

            int number = 8;

            switch (number)
            {
                case 1:
                    Console.WriteLine("Monday");
                    break;
                case 2:
                    Console.WriteLine("Tuesday");
                    break;
                case 3:
                    Console.WriteLine("Wednesday");
                    break;
                case 4:
                    Console.WriteLine("Thursday");
                    break;
                case 5:
                    Console.WriteLine("Friday");
                    break;
                case 6:
                    Console.WriteLine("Saturday");
                    break;
                case 7:
                    Console.WriteLine("Sunday");
                    break;
                default:
                    Console.WriteLine("Invalid Week Day");
                    break;
            }

Nested-Switch… Case Construct

  • C# allows the switch…case construct to be nested. That is, a case block of a switch…case construct can contain another switch…case construct.
  • Also, the case constants of the inner switch…case construct can have values that are identical to the case constants of the outer construct.

Example:

Console.WriteLine("Enter Your Pizza Flavour: ");
            string pizza_flavour = Console.ReadLine();

            switch(pizza_flavour)
            {
                case "Chicken Tikka":
                    Console.WriteLine("You have selected Chicken Tikka Pizza.");
                    break;
                case "Supreme":
                    Console.WriteLine("You have selected Supreme Pizza.");
                    break;
                case "Cheese Lover":
                    Console.WriteLine("You have selected Cheese Lover Pizza.");
                    break;
                case "Vegetable":
                    Console.WriteLine("Which Vegetable you want in your pizza ?");
                    string veg = Console.ReadLine();
                    switch(veg)
                    {
                        case "Olives":
                            Console.WriteLine("You have selected Olives in your vegetable Pizza.");
                            break;
                        case "Mushrooms":
                            Console.WriteLine("You have selected Mushrooms in your vegetable Pizza.");
                            break;
                        case "Onions":
                            Console.WriteLine("You have selected Onions in your vegetable Pizza.");
                            break;
                        default:
                            Console.WriteLine("Your selected vegetable is not available.");
                            break;
                    }
                    break;

                case "Fajita":
                    Console.WriteLine("You have selected Fajita Pizza.");
                    break;
                default:
                    Console.WriteLine("Your pizza Flavour is not available.");
                    break;
                
            }

Click Below Link to Download Notes Of This Blog

https://www.mediafire.com/file/rc332e7xj3tqxqa/DECISION+MAKING+STATEMENTS+CSHARP.pptx/file

Click Below Link to Download Visual Studio Project / Source Code Of This Blog

https://www.mediafire.com/file/rgm0qxzjjwksoee/SelectionStatements.rar/file

No responses yet

Leave a Reply

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