Advertisement

Responsive Advertisement

II- Selection Structure

II- Selection Structure

Also known as a conditional structure, a selection structure is a programming feature that performs different processes based on whether a boolean condition is true or false. Selection structures use relational operators to test conditions. There are different types of selection structures that can be used to achieve different outcomes.

  • If you want your program to do something if a condition is true, but do nothing if that condition is false, then you should use an if-end structure.
  • If you want your program to do something if a condition is true and do something different if it is false, then you should use an if-else structure.
  • If you want to test multiple conditions, then you can include an elseif structure within an if-end or if-else structure.




II.1- If Statement

An if statement can be any of the following two forms:

  • An if statement with an else part selects one of the two statements to execute based on the value of a Boolean expression, as the following example shows:






II.2- Switch Case Statement

The switch Case statement selects a statement list to execute based on a pattern match with a match expression, as the following example shows:




 Source Code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace Switch_Case

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void txtName_TextChanged(object sender, EventArgs e)

        {

            switch (txtName.Text)

            {

                case "Lim sokros":

                    txtPosition.Text = "Programmer SBC Bank";

                    break;

                case "":

                    txtPosition.Text = "BBU Lecturer";

                    break;

                default:

                    txtPosition.Text = "Unknown";

                    break;

            }

        }

    }

}

Post a Comment

0 Comments