Nested conditional statement. Difficult conditions


The blocks “if” and “otherwise” may include any other statements, including other nested conditional statements; while the else statement refers to the nearest previous if

Example
if ( A > 10 )
  if ( A > 100 )
    cout << "You have a lot of money.";
  else
    cout << "You have enough money.";
else
    cout << "You have not enough money.";
To make it easier to understand the program, all the blocks “if” and “otherwise” (together with the brackets enclosing them) are shifted to the right by 2-3 characters — such a record is called a “ladder” record
Recording "ladder" is a good form for any programmer!

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.