Module: (C++) Nested conditional statement. Difficult conditions


Problem

2/14

Complex Conditions

Theory Click to read/hide

The previous problem can be solved in a shorter way using difficult conditions.

Let's understand what is COMPLEX CONDITIONS

The simplest conditions consist of one relationship (more, less, etc.) But sometimes it is necessary to combine simple conditions into more complex ones, for example: it is cold outside and it rains. Two simple conditions (it’s cold outside), (it’s raining outside) are connected by a bunch AND.

DIFFICULT CONDITION - consists of two or more simple relations (conditions), which are combined using logical operations
AND - logical multiplication - in the C++ language it is written as && (or and)
OR - logical multiplication - written in C++ as || (or or)
NOT - logical multiplication - in the C++ language it is written like !


Operator AND - requires the simultaneous fulfillment of two conditions

condition 1 && condition 2 - will take true value only if both simple conditions are true at the same time
moreover, in the C++ programming language - if condition 1 is false, then condition 2 will not be checked


Operator OR - requires at least one of the conditions to be met

condition 1 || condition 2 - will take a false value only if both simple conditions are false at the same time
moreover, in the C programming language - if condition 1 is true, then condition 2 will not be checked

Operation NOT
! condition 1 - will take a false value, condition 1 is true and vice versa
For example, the following two conditions are equivalent: A> B and! (A <= B)

PRIORITY FOR PERFORMING LOGIC OPERATIONS AND RELATIONS
1 operation in parentheses
2 operation NOT
3 logical relationships>, <,> =, <=, ==,! =
4 operation AND
5 operation OR
Parentheses are used to change the order of actions.


BOOLEAN VARIABLES
In many programming languages, it is possible to use variables that store logical values ("true" / "false"). In C ++, such variables can take the values true (true) or false (false). For example, a program fragment

bool a, b;
a = true;
b = false;
cout << a || b;
Displays 1 (which corresponds to true, false corresponds to 0).
Boolean variables are of type bool, named after the English mathematician George Boole, the creator of the algebra of logic.

Problem

In the previous task we examined, it is necessary to check the condition in which the number must be greater than or equal to 20 and less than or equal to 40.
You can reduce the previous task using complex conditions.

In the 6th line of the program, instead of the underscore (_), put the desired logical connective.