(C++) Nested conditional statement. Difficult conditions


Nested Conditional Statement

What if we need to check several conditions and if they are met, perform certain actions?
In this case we can use a nested conditional statement.

A nested conditional statement is a conditional statement placed inside an if or else block. The else block always refers to the nearest preceding if block.
 
Problem
For the available amount of money (in conventional units) it is necessary to output the phrase:
  1. if the amount is more than 10 units, print "You have enough money";
  2. if the amount is more than 100 units, print "You have a lot of money";
  3. if the amount is less than 10 units, output "You don't have much money".
From the condition of the problem you can see that if you have more than 10 conditional units of money, you need to check the condition if it is more than 100 conditional units, and if it is more, then output the phrase "You have a lot of money". If the money is more than 10, but less than or equal to 100, then the phrase should be "You have enough money". It turns out that before you output this phrase, you need to check one more condition. An example implementation of a program with nested conditional statement is described below.
 
Program
#include <iostream>
using namespace std;
int main()
{
    int money;
    cin >> money;
    if ( money > 10 ) # If the money is more than 10
    {
         # Nested conditional money
        if ( money > 100 ) # and the money is more than 100
        {
            cout << "You have a lot of money" << endl;   # Output 1 phrase
        }
        else
        {
            cout << "You have enough money" << endl;  # If the money is more than 10, 
                                                # but not more than 100,
                                                # then output phrase 2
        }
    }
    else
    {   # If the money is not more than 10, then output phrase 3.
        cout << "You don't have much money" << endl; 
    }
    return 0;
}
In this program
  • We take an integer from the user as input and store it in the variable money.
  • Then we use the if...else statement to check if the value of money is greater than 10.
    • If it is, we execute the inner if...else statement.
    • If false, then the code in the outer else statement is executed, which outputs the phrase "You don't have enough money."
  • The inner if...else statement checks if the value of money is greater than 100.
    • If true, it outputs the phrase "You have a lot of money."
    • If false, it outputs the phrase "You have enough money."

Don't forget to staircase the program, for better readability and better understanding!
 

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.