C++. Conditional jump operator ‘if’




Conditional jump operator ‘if’


Contents


Search other websites:

1. What is the function of the conditional ‘if statement in C++ programs?

The conditional ‘if’ statement allows you to organize the selection of the progress of the program. The choice is made by means of certain condition. If the condition is true, then the program executes in one way. Otherwise, the program is executed in a different way.

The conditional branch operator performs branching in the program.

2. What representations does the conditional jump operator have in the C++ language?

A conditional branch operator can have the following representations:

  • the complete form ‘if … else’;
  • the short form ‘if’;
  • the representation ‘if … else … if’.

When solving tasks, any of the forms can be replaced by another one. The same task can be solved in several ways. In solving the task the choice of one or other form of conditional jump ‘if’ remains at the discretion of the programmer.






3. What representation does the full form of the ‘if’ statement have?

A general representation of the full form of the conditional branch operator ‘if’ is as follows:

if (expression)
{
    // several operators (instructions)
    // ...
}
else
{
    // several operators (instructions)
    // ...
}

Where expression is a conditional expression (condition) according to the C++ syntax.

The if statement works as follows. If the ‘expression’ element evaluates to true, then operators are executed, which follow immediately after the ‘if’ keyword.

If, after the word ‘if’ or after the word ‘else’ you need to perform only one operator (rather than several), then the curly braces ‘{ }’ can be omitted.

The general form of the operator, in which after the words ‘if’ and ‘else’ only one statement needs to be executed, can be the following:

if (expression)
    statement1;
else
    statement2;

4. Examples of using the full form of the ‘if’ statement.

Example 1. Write a code snippet that evaluates the value of the following expression:

C++ formula task

float f, x;

// input x
// ...

if ((-8<=x)&&(x<=5))
    f = x*x+2*x-4;
else
    f = x-5;

In the above example, the resulting value of f is computed based on the value of x.

Example 2. The value n = 1..7 is given, which is the day of the week number. By the value of n, determine whether this day is a day off or a work day. The result is written to the variable fDayOff of type bool.

A snippet of code that solves this problem.

int n;
bool fDayOff;

n = 5;

if ((n>=1) && (n<=5))
    fDayOff = false; // fDayOff = false
else
    fDayOff = true;

5. Which representation has the shortened form of the ‘if’ statement?

Sometimes in C++ programs, the full form of the ‘if’ statement should be replaced by a shortened form. This is necessary in cases where after the word ‘else’ you do not need to follow any instructions. In the shortened form of the ‘if’ statement, the keyword ‘else’ is omitted.

The general representation of the abbreviated form of the ‘if’ operator is:

if (expression)
{
    // several statements
    // ...
}

Where the expression is a conditional expression (condition) according to the C++ syntax.

If after the word ‘else’ only one statement is to be executed, then the curly braces { } can be omitted:

if (expression)
    statement;

6. Examples of using the shortened form of the ‘if’ statement

Example 1. Three integers a, b, c are given. Develop a program that finds the minimum value between these numbers.

A snippet of code that solves this task:

int a, b, c;
int min; // required minimum value

a = 8;
b = -5;
c = 12;

// minimum value search
min = a;
if (min > b) min = b;
if (min > c) min = c; // min = -5

Example 2. An integer n = 1..3 is given, which is the number of the function. From the value of variable n, calculate the value of the corresponding function:

1) x2+8; 2) -5x-1; 3) 10-x.

A snippet of code that solves this problem with the abbreviated form of the ‘if’ statement.

int n;
int x, f;

// input of the values n, x
// ...

if (n==1) f = x*x + 8;
if (n==2) f = -5*x - 1;
if (n==3) f = 10 - x;

7. The compound form of the conditional jump operator ‘if … else … if’

The ‘if’ operator can have a more complex form, which has the following general representation:

if (expression1)
{
    // statements
    // ...
}
else
if (expression2)
{
    // statements
    // ...
}
else

...

if (expressionN)
{
    // statements
    // ...
}
else
{
    // statements
    // ...
}

where expression1, expression2, …, expressionN are conditional expressions.

Conditional expressions are computed from the top down. If one of the expressions finds the true result (true), then the operators that are associated with this branch will be executed, and all other “branches” will be skipped.

8. Examples of using the composite form ‘if … else … if’

Example 1. Let there be given the number of the month of the year n. By the month number, determine how many days this month. Take that in February, 28 days.

A snippet of code that solves this task.

int n;
int days;

// input n
// ...

if ((n==4)||(n==6)||(n==9)||(n==11)) days = 30;
else
if (n==2) days = 28;
else
    days = 31;

Example 2. Given a real number x. Calculate the value of the function

C++ formula task

A snippet of code that solves this task.

float x;
float f;

x = 5;

if (x<-2)
    f = 3*x*x - 8;
else
if ((-2<=x)&&(x<=4))
    f = -9*x*x - 12;
else
    f = 32 + x; // f = 37

9. What are nested if statements?

The ‘if’ statements can be nested. This means that instead of operators (see items 2, 4, 6) there can be another ‘if’ statement. In C++, up to 256 attachments of the ‘if’ statements are allowed.

Here, there is a rule relative to the last else-statement. The last else-instruction refers to the closest if-statement that is inside the same program block (braces { }) but is not yet associated with any other else-statement. In order to avoid invisible logical errors, it is recommended to select the necessary “branches” of the if statement in curly braces { }.

The simplest general form of a nested ‘if’ statement is:

if (expression)
{
    // Nested statement if
    if (expression)
    {
        // statements
        // ...
    }
    else
    {
        // statements
        // ...
    }

    // ...
}

10. Example of nested statements if

Example. Fragment of the solution of the quadratic equation taking into account all possible values of the coefficients of the equations a, b, c.

float a, b, c; // coefficients of equation
float d;
float x1, x2; // The roots of equation

a = -1;
b = 1;
c = 5;

d = b*b - 4*a*c;

if (a==0)
{
    if (b!=0)
    {
        label1->Text = " The equation has 1 root ";
        x1 = -c/b;
        label2->Text = x1.ToString();
    }
    else
    {
        label1->Text = " Incorrect data ";
    }
}
else
{
    // a!=0
    if (d<0)
        label1->Text = " The equation has no roots ";
    else
    {
        if (d==0)
        {
            label1->Text = " The equation has 1 root ";
            x1 = -b/(2*a);
            label2->Text = "x = " + x1.ToString();
        }
        else
        {
            label1->Text = " The equation has 2 roots ";
            x1 = (-b - Math::Sqrt(d))/(2*a);
            x2 = (-b + Math::Sqrt(d))/(2*a);
            label2->Text = "x1 = " + x1.ToString();
            label3->Text = "x2 = " + x2.ToString();
        }
    }
}


Related topics