The “if” statement

The if statement


Content

 


1. In what cases is advisable to use the operator conditional jump?

The conditional statement is applied in that cases, when the course of solving the problem depends on the fulfillment (failure to comply) of some condition. In the C# are specified the two forms of conditional jump statement:

  • the full form “if … else …“;
  • the shortened form “if …“.

2. What view has the full form of “if” statement?

The full form of if statement is following:

if (condition) statement1;
else statement2;

where condition – the some condition; statement1 – the statement that is executed when condition result is “true“; statement2 – the statement, that is executed when condition result is “false“. The “else” word is not mandatory.

After words “if” and “else” can follow several operators. In this case, they are taken in the operator brackets:

if (condition)
{
    // several operators
    ...
}
else
{
    // several operators
    ...
}

3. What view has the shortened form of conditional statement?

The shortened form of “if” statement is following:

if (condition) statement;

In this case the statement is executed, when the condition is true. If the condition is false, then nothing is done.

If you need execute the several operators after checking the condition, then these operators are taken in the braces:

if (condition)
{
    // several operators
}

4. Examples of using the “if” statement, that takes the full form.

Example 1. The two numbers x and y are given. Write the code fragment, that finds a maximum between these numbers.

double x, y, max;

x = 5;
y = -3;

if (x > y)
    max = x;
else
    max = y;
// after performing max = 5

Example 2. The two real numbers x and y are given. Determine x/y. Envisage the dividing by zero with the message “Division by zero“.

double x, y, res;

x = 5;
y = 0;

// check division by zero
if (y == 0)
    label1.Text = "Division by zero";
else
{
    res = x / y;
    label1.Text = res.ToString();
}

Example 3. Determine whether the integer value a in the interval [x; y], where x, y – integers.

...

double x, y, a;

x = 3;
y = 9;
a = 8;

if ((x<=a)&&(a<=y))
    label1.Text = "In the range";
else
    label1.Text = "It doesn't in the range";

...

5. Examples of using the “if” statement with shortened form.

Example 1. The task from paragraph 4. The two numbers x and y are given. Write the code snippet, that finds a maximum between these numbers.

double x, y, max;
x = 33;
y = 9;

// another way to find the maximum
max = x;
if (max < y) max = y; // shortened form of if statement
label1.Text = max.ToString();

Example 2. The real number x is given. You need calculate the maximum value:

  • 3 + sin(x)
  • 1 + x
  • 4 – x2

The code snippet:

double x, r1, r2, r3;
double max; // maximum value

x = 2;
r1 = 3 + Math.Sin(x);
r2 = 1 + x;
r3 = 4 - x * x;
max = r1;
if (max < r2) max = r2; // the if statement
if (max < r3) max = r3;
label1.Text = max.ToString();

6. Examples of applying of nested if statements.

The “if-else” statement can include other statement “if-else“.

Example 1. The use of nested operators in complete form of the operator if. Write the code snippet, that displays the values of three variables x, y, z in the descending order.

...
double x, y, z;
x = 22;
y = 88;
z = 2;

// nested operators if
if (x > y)
{
    if (x > z)
    {
        if (y > z) // x - y - z
            label1.Text = x.ToString() + ";" + y.ToString() + "; " + z.ToString();
        else // x - z - y
            label1.Text = x.ToString() + ";" + z.ToString() + "; " + y.ToString();
    }
    else // z - x - y
        label1.Text = z.ToString() + "; " + x.ToString() + "; " + y.ToString();
}
else
{
    if (y > z)
    {
        if (x > z) // y - x - z
            label1.Text = y.ToString() + "; " + x.ToString() + "; " + z.ToString();
        else // y - z - x
            label1.Text = y.ToString() + "; " + z.ToString() + "; " + x.ToString();
    }
    else // z - y - x
        label1.Text = z.ToString() + "; " + y.ToString() + "; " + x.ToString();
}

Example 2. Using of nested operators in the shortened form of the “if” operator. The decision of Example 1 in another way.

...
double x, y, z;
x = 7;
y = 6;
z = 8;

// full + shortened form of "if" statement
if (x > y)
{
    if (y>z) // z - y - z
        label1.Text = x.ToString() + ";" + y.ToString() + "; " + z.ToString();
    if (y <= z)
    {
        if (x>z) // x - z - y
            label1.Text = x.ToString() + ";" + z.ToString() + "; " + y.ToString();
        if (x<=z) // z - x - y
            label1.Text = z.ToString() + "; " + x.ToString() + "; " + y.ToString();
    }
}
else
{
    if (y<z) // z - y - z
        label1.Text = z.ToString() + "; " + y.ToString() + "; " + x.ToString();
    if (y >= z)
    {
        if (x>z) // y - x - z
            label1.Text = y.ToString() + "; " + x.ToString() + "; " + z.ToString();
        if (x<=z) // y - z - x
            label1.Text = y.ToString() + "; " + z.ToString() + "; " + x.ToString();
    }
}
...

7. What is the general view and working principle of “if-else-if“?

Often in the programs appropriate to have the use of a multi-stage structure

if - else - if

that consists nested operators “if“.

The general form of structure:

if (condition1)
    statement1;
else if (condition2)
    statement2;
else if (condition3)
    statement3;
    ...
else
    statementN;

In this structure the conditional expressions are calculated by principle of down.

Execution is carried out until one of the conditions is carried out. If the condition1 is “true“, then statement1 is executed. All others operators are missed. Analogically, if the condition2 is true, then the statement2 is executed. If none of the conditions is not executed, then the statementN is executed.


8. Example of applying the construction “if-else-if“.

The integer number n is given. This number specifies the number of week day from 1 to 7. By using this number n show the name of corresponding day of week.

The code snippet, that solves given task by using the construction “if-else-if“:

int n;
n = 3;

if (n == 1)
    label1.Text = "Monday";
else
if (n == 2)
    label1.Text = "Tuesday";
else
if (n == 3)
    label1.Text = "Wednesday";
else
if (n == 4)
    label1.Text = "Thursday";
else
if (n == 5)
    label1.Text = "Friday";
else
if (n == 6)
    label1.Text = "Saturday";
else
    label1.Text = "Sunday";