Logical operations in C ++

logical operation with ++, c ++ beginnersWe continue to learn the basics of programming. We have already met with arithmetic operations (including arithmetic operations combined – While assigning a value to a variable). Considered as unary operators increment and decrement. But in programming, we have to not only deal with the calculation of, as well as to compare the values ​​of variables with each other. In this lesson we will deal with the logical operations of language C ++. When comparing between any variables (values), we get the result – lie (false) or truth (true). For example, we compare two values: 3 < 8 – it is truth, as 3 really less 8. And here 10 > 50 – It's a lie.

Consider the operators, that apply to logical operations:

logical operation with
Boolean operations – tab. 1

All operators of table 1 – and relational operators and equality operators They are binary. In programming, working with them is, we compare two operands (two values) between themselves and see their relationship to each other: one more, and a second smaller, or vice versa, or one absolutely equal to the second, or is… As a result of this test, as discussed above, the program returns one of two possible values – false (0) or true (1). By the way, the truth in C ++ refers to any value, both positive and negative, Besides 0. Important – do not confuse operators = (variable assignment) and == (logical equality operator). These are two completely different operator.

logical operation with
Logic operations -tab. 2

Logical operators association && and || also have a binary form. They are used in programming in order, to be able to compare not only two values, but also to create certain conditions, or combine multiple comparisons of conditions or equal to one. For example we have a variable variable value entered by the user. And we want to know – whether it is in the range from 10 to 20? This is true (true) or not so (false)? verify, using a Boolean AND (&&): cout << ((variable > 10) && (variable < 20)); I.e, on the screen will appear 1(true), only in case that, If a value is at the same time more 10 And(&&) less 20. If the user enters 15 – We see on the screen 1. If enters 21 – on the screen 0, as 21 range is already.

The results of the, which returns combined with the condition logical AND (&&), can be represented in a table:

logical operation with
Boolean operations – tab. 3

With reference to the above example,: 15 both >10 and <20. So run and Conditions 1 and Conditions 2. result – true. If the user entered value 50, It is performed and gives true only Condition 1 (>10), and Conditions 2 (<20) – this is false, since it is not satisfied. The result of the combined conditions – false.

What if we need to prove the contrary, that the entered number does not belong to the specified range, a is in the range of either <10, or >20? Then we will have to make a combined condition, Using OR logical operator ( || ): cout << ((variable < 10) || (variable > 20)); Then, if the user is 15 – We see on the screen 0, as 15 It does not occur in a range from 9 до -∞, or in a range from 21 до ∞. And if you introduce 22 – We see on the screen 1, as 22 It falls under the second condition and is in the range of 21 до ∞.

The results of the, which returns combined with the condition logical OR ( || ), It can also be represented in table:

logical operation with
Boolean operations – tab. 4

As for the negative inversion – logical NOT ( ! ) – This operator is a unary. He always “twists” return value terms. For example, when displaying the comparison result cout << (3 < 8); – see 1, because it is the truth. But before using a logical condition is NOT: cout << !(3 < 8); truth conditions (3 < 8) converted into a lie. The table looks like this:

logical operation with
Boolean operations – tab. 5

Put it all considered in a code:

logical operation with

We covered the basic logical operations in C ++. Perhaps not yet entirely clear, they are used in programming. But it normal. All they need for you to see, when we study design logical choice if and if else.

We invite you to watch the video, where logic operations are considered and not only:

Newsletter of programming:

Logical operations in C ++
4.4 (87.5%) 8 votes

7 thoughts on “Logical operations in C ++

  1. It is especially important not to get confused with the type of action !(a < b), since it is easy to get confused with the result… It was once a, confused and did not understand why 0 turned, but it turns out it's simple… and in this article it is spoken about! thanks to the author! informative!

  2. Моя программка для задачки из видео))

    using namespace std;

    int main()
    setlocale(LC_ALL, “rus”);

    int a;
    cout << "Выберите команду: " << endl;
    cout << "1сложение \n" << "2вычитание \n" << "3умножение \n" << "4 – division" <> a;

    switch (a) {
    case 1:
    cout << "Вы выбрали сложение. \n";
    case 2:
    cout << "Вы выбрали вычитание. \n";
    case 3:
    cout << "Вы выбрали умножение. \n";
    case 4:
    cout << "Вы выбрали деление. \n";
    cout << "Пожалуйста, введите корректное значение (1-4) \n";
    return a;
    int b;
    cout <> b;
    int c;
    cout <> c;
    int d;
    if (a == 1)
    d = b + c;
    cout << "Сумма введенных чисел равна: " << d << endl;
    if (a == 2)
    d = b – c;
    cout << "Разность введенных чисел равна: " << d << endl;
    if (a == 3)
    d = b * c;
    cout << "Произведение введенных чисел равно: " << d << endl;
    if (a == 4)
    d = b / c;
    cout << "Частное введенных чисел равно: " << d << endl;
    return 0;

Leave a Reply

Your email address will not be published. Required fields are marked *