Using Nested If Statements

Using nested if

In some cases, if a condition is met, other conditions must be checked. For this, we can use another if structure in the relevant block of the if structure.

The most important thing when using nested if is not to mix up the parentheses. Opening and closing curly braces of each if block we open immediately and then filling them in will reduce the number of errors.

In addition, using line indentation properly will make our work easier.

In the example structure below, an if is opened in both the first block and the else block of the outer if structure. This structure can be longer or shorter depending on the need. Other if blocks can even be opened inside the inner if blocks.

Example nested if structure:

if ( condition-1 )

{

if (condition-A)
{
 // condition - 1 and actions to take when the condition to place
}
else if (condition-B)
{
 // condition 1 and condition-B processing to be performed when held
}
else
{
 // if-1 occurs and must Actions to take when -A and condition-B are not met
}

}
else if ( condition-2)
{

if ( condition-C )
{
 //operations when condition-2 and condition-C are met
}
else if (condition-D)
{
 //operations when condition-2 and condition-D are met
}
else
{
 //condition-2 is true and condition Actions to take when -C and condition-D are not met
}

}

 

visual c# nested if else tutorials, using if in if, if and else exercises

EXERCISES

Nested if exercise

Let the user enter their notes in the three text boxes. Calculate the average of these grades and write the result as "Passed" or "Failed".

If any of the boxes is empty, an error message will be given.

If a number less than 0 or greater than 100 is entered in any of the boxes, an error message will be given.

First we need to check if the boxes are empty. If all three boxes are full, we can take numbers into variables. Then we need to see if the numbers are between 0 and 100.

Solution:

if(textBox2.Text=="" || textBox3.Text=="" || textBox4.Text=="")
{
   MessageBox.Show("Missing information");
}
else
{
 
   int a = int.Parse(textBox2.Text);
   int b = int.Parse(textBox3.Text);
   int c = int.Parse(textBox4.Text);
 
if (a < 0 || a > 100 || b < 0 || b > 100 || c < 0 || c > 100)
{
   MessageBox.Show("Numbers entered incorrectly");
}
else
{
   float ort = (a + b + c) / 3;
 
if (ort < 50)
{
label2.Text = "You failed the class";
}
else
{
label2.Text = "You passed.";
}
}
}

 

Nested if tutorial

İç içe if örneği

Above is the image of the form in the 1st picture, and the combobox opened in the 2nd picture.
 
Accordingly, the entrance price will be calculated according to the information to be received from the user. The terms are as follows:
  • Normal: No discount, 5 TL
  • Student: 50% discount, 2.50 TL. No discount if age over 30.
  • Teacher: 30% discount, 3.50 TL. No discount if age younger than 20 and older than 65.
  • Soldier: 50% discount, 2.50 TL. No discount if age younger than 20 and older than 40.
  • Disabled: 75% discount, 1.25 TL
  • Elderly: 25% discount, 3.75 TL. No discount if under 60.
  • Veteran: 100% discount, 0 TL. No discount if under 20.

Let's write the program that calculates the fee according to the situations mentioned above. In the meantime, an error message will be given even if the selection is not made.

* The Turkish currency TL is used in the example.

private void button1_Click_1(object sender, EventArgs e)
        {
            int age= int.Parse(textBox2.Text);
 
            if (comboBox1.SelectedIndex == -1)
            {
                label1.Text = "Make selection.";
            }
            else if (comboBox1.SelectedIndex == 0)
            {
                label1.Text = "5,00 TL";
            }
            else if (comboBox1.SelectedIndex == 1)
            {
                if (age< 30)
                {
                    label1.Text = "2,50 TL";
                }
                else
                {
                    label1.Text = "5,00 TL";
                }
            }
            else if (comboBox1.SelectedIndex == 2)
            {
                if (age>= 20 && age<= 65)
                {
                    label1.Text = "3,50 TL";
                }
                else
                {
                    label1.Text = "5,00 TL";
                }
            }
            else if (comboBox1.SelectedIndex == 3)
            {
                if (age>= 20 && age<= 40)
                {
                    label1.Text = "2,50 TL";
                }
                else
                {
                    label1.Text = "5,00 TL";
                }
            }
            else if (comboBox1.SelectedIndex == 4)
            {
                if (age>= 60)
                {
                    label1.Text = "3,75 TL";
                }
                else
                {
                    label1.Text = "5,00 TL";
                }
            }
            else if (comboBox1.SelectedIndex == 5)
            {
                
                    label1.Text = "1,25 TL";
            }
            else if (comboBox1.SelectedIndex == 6)
            {
                if (age>= 20) label1.Text = "0,00 TL";
                else label1.Text = "5,00 TL";
            }
 
 
        }

 



COMMENTS




Read 851 times.

Online Users: 230



using-nested-if-statements