Last Updated:

Branching Construction in C++ - Lesson 3

There are situations when the program needs to choose which operation to perform, depending on a certain condition.

For example, we enter an integer from the keyboard. If this number is greater than ten, then the program must perform one action, otherwise - another. We implement this algorithm in C++ using the branching construct.

  • If statement
  • Example of branch design
#include <iostream>
using namespace std;

int main()
{
     setlocale(0, "");
     double num;

     cout << "Enter an arbitrary number: ";
     cin >> num;

     if (num < 10) { // If the entered number is less than 10.
         cout << "This number is less than 10." <<endl;
     } else { // otherwise
         cout << "This number is greater than or equal to 10." <<endl;
     }
     return 0;
}

If you run this program, a corresponding message will be displayed when you enter a number less than ten.

If the number you entered is greater or equal to ten, another message will be displayed.

If statement

The if statement is used to perform an operation when the condition is true. A conditional construct in C++ is always written in parentheses after the operator .if

Inside the braces, the body of the condition is indicated. If the condition is met, all commands that are between the braces will be executed.

Example of branch design

if (num < 10) { // If the entered number is less than 10.
     cout << "This number is less than 10." <<endl;
} else { // otherwise
     cout << "This number is greater than or equal to 10." <<endl;
}

It says: "If the variable is less than 10, display a corresponding message. Otherwise, bring out another message."num

Improve the program so that it displays a message that the variable is ten:num

if (num < 10) { // If the entered number is less than 10.
     cout << "This number is less than 10." <<endl;
} else if (num == 10) {
     cout << "This number is 10." <<endl;
} else { // otherwise
     cout << "This number is greater than 10." <<endl;
}

Here we check three conditions:

  • The first is when the entered number is less than 10
  • The second is when the number is 10
  • And the third is when the number is greater than ten

Note that in the second condition, when checking for equality, we use the equality operator, , and not the assignment operator, because we do not change the value of the variable when checking, but compare its current value with the number 10.==

  • If you put an assignment statement in a condition, then when you check the condition, the value of the variable changes, after which the condition is met.

Each if statement corresponds to only one else statement. The combination of these statements , else if means that if the previous condition is not met, then check this one. If none of the conditions is true, the body of the operator else is executed.

If only one command must be executed after the ifelse statement, or the else if binding statement, you do not have to put curly braces. The previous program can be recorded as follows:

#include <iostream>
using namespace std;

int main()
{
     setlocale(0, "");
     double num;

     cout << "Enter an arbitrary number: ";
     cin >> num;

     if (num < 10) // If the entered number is less than 10.
         cout << "This number is less than 10." <<endl;
     else if (num == 10)
         cout << "This number is 10." <<endl;
     else // otherwise
         cout << "This number is greater than 10." <<endl;

     return 0;
}

This method of recording looks more compact. If we need to execute more than one command when a condition is met, curly braces are required. For example:

#include <iostream>
using namespace std;

int main()
{
     setlocale(0, "");
     double num;
     int k;

     cout << "Enter an arbitrary number: ";
     cin >> num;

     if (num < 10) { // If the entered number is less than 10.
         cout << "This number is less than 10." <<endl;
         k = 1;
     } else if (num == 10) {
         cout << "This number is 10." <<endl;
         k = 2;
     } else { // otherwise
         cout << "This number is greater than 10." <<endl;
         k = 3;
     }

     cout << "k = " << k << endl;
     return 0;
}

This program checks the value of a variable If it is less than 10, then assigns the value of one to the variable. If the variable is ten, then assigns the value of two to the variable. Otherwise, the value of the triple. After branching is performed, the value of the variable is displayed.num.knumkk

Practice well, try to come up with your own example with branching. When you understand that you understand this topic, you can safely proceed to the next lesson.