# Logical operations in C ++

We 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:

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 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:

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:

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:

Put it all considered in a code:

Result:

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:

Logical operations in C ++

## 7 thoughts on “Logical operations in C ++”

1. faster I would have to see how they are applied in IFah!!!

I promise in the coming days to talk ))

2. 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!

1. consideration of such, seemingly small nuances, big “+”!!!

3. All lucidly explained, Thank you. All three types of logic are clear: &&, ||, !.

4. Jury says:

cognitive site!Everything is simple and clear))

5. Anatoly says:

My program for tasks from video))

#include
using namespace std;

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

int a;
cout << "Выберите команду: " << endl;
cout << "1 – adding n" << "2 – Subtraction n" << "3 – multiplication n" << "4 – division" <> a;

switch (a) {
case 1:
cout << "Вы выбрали сложение. \n";
break;
case 2:
cout << "Вы выбрали вычитание. \n";
break;
case 3:
cout << "Вы выбрали умножение. \n";
break;
case 4:
cout << "Вы выбрали деление. \n";
break;
default:
cout << "Пожалуйста, enter the correct value (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;
}