# The if statement

### Contents

- 1. In what cases is advisable to use the operator conditional jump?
- 2. What view has the full form of “
**if**” statement? - 3. What view has the shortened form of conditional statement?
- 4.
**Examples**of using the “**if**” statement, that takes the full form. - 5.
**Examples**of using the “if” statement with shortened form. - 6.
**Examples**of applying of nested if statements. - 7. What is the general view and working principle of “if-else-if“?
- 8.
**Example**of applying the construction “if-else-if“ **Related topics**

##### 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;elsestatement2;

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

**are given. Write the code fragment, that finds a maximum between these numbers.**

*y*double x, y, max;x = 5;y = -3;if (x > y)max = x;elsemax = y;// after performing max = 5

**Example**** 2.** The two real numbers ** x** and

**are given. Determine**

*y**x*/

*y*. Envisage the dividing by zero with the message “Division by zero“.

double x, y, res;x = 5;y = 0;// check division by zeroif (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***], where**

*y***,**

*x**y*– integers.

...double x, y, a;x = 3;y = 9;a = 8;if ((x<=a)&&(a<=y))label1.Text = "In the range";elselabel1.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 maximummax = x;if (max < y) max = y; // shortened form of if statementlabel1.Text = max.ToString();

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

- 3 + sin(
*x*) - 1 +
*x* - 4 –
*x*^{2}

The code snippet:

double x, r1, r2, r3;double max; // maximum valuex = 2;r1 = 3 + Math.Sin(x);r2 = 1 + x;r3 = 4 - x * x;max = r1;if (max < r2) max = r2; // the if statementif (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 ifif (x > y){if (x > z){if (y > z) // x - y - zlabel1.Text = x.ToString() + ";" + y.ToString() + "; " + z.ToString();else // x - z - ylabel1.Text = x.ToString() + ";" + z.ToString() + "; " + y.ToString();}else // z - x - ylabel1.Text = z.ToString() + "; " + x.ToString() + "; " + y.ToString();}else{if (y > z){if (x > z) // y - x - zlabel1.Text = y.ToString() + "; " + x.ToString() + "; " + z.ToString();else // y - z - xlabel1.Text = y.ToString() + "; " + z.ToString() + "; " + x.ToString();}else // z - y - xlabel1.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" statementif (x > y){if (y>z) // z - y - zlabel1.Text = x.ToString() + ";" + y.ToString() + "; " + z.ToString();if (y <= z){if (x>z) // x - z - ylabel1.Text = x.ToString() + ";" + z.ToString() + "; " + y.ToString();if (x<=z) // z - x - ylabel1.Text = z.ToString() + "; " + x.ToString() + "; " + y.ToString();}}else{if (y<z) // z - y - zlabel1.Text = z.ToString() + "; " + y.ToString() + "; " + x.ToString();if (y >= z){if (x>z) // y - x - zlabel1.Text = y.ToString() + "; " + x.ToString() + "; " + z.ToString();if (x<=z) // y - z - xlabel1.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;...elsestatementN;

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";elseif (n == 2)label1.Text = "Tuesday";elseif (n == 3)label1.Text = "Wednesday";elseif (n == 4)label1.Text = "Thursday";elseif (n == 5)label1.Text = "Friday";elseif (n == 6)label1.Text = "Saturday";elselabel1.Text = "Sunday";

### ⇑

### Related topics

### ⇑