web123456

C++ write multiple conditions in brackets

In C++ language, if() can write multiple conditions
&&:and
||: Or
!:negative

&&,|| and ! The priority is from high to low:
! > && > ||

for example:
if(a>1 && a<100) means a is greater than 1 and less than 100;
if(a<1 || a>100) means a is less than 1 or a is greater than 100;
if(a%4 == 0 && a%100 !=0 || a%400 == 0) The year represented by table a is a leap year (the number of years is a multiple of 4 and is not a multiple of 100 or the number of years is a multiple of 400).

The following content is modified from: /view/

&& operator

The && operator is called the logic and operator. It takes two expressions as operands and creates an expression that is true only if both subexpressions are true.
Here is an example of an if statement using the && operator:

if ((temperature < 20) && (minutes > 12))
cout << "The temperature is in the danger zone.";

Note that these two expressions logically concatenated with operators are complete expressions, and their values ​​can be evaluated as true or false. First evaluate temperature < 20 to produce results with true or false , then evaluate minutes>12 to produce results with true or false , and finally, these two results are juxtaposed by AND to get the final result of the entire expression.
The cout statement is executed only if temperature is less than 20 and minutes greater than 12. If the result of one expression is evaluated as false, the entire expression is false and the cout statement is not executed.

Note that if the subexpression to the left of the && operator is false, the expression on the right is not checked. Because as long as there is a subexpression that is false, the entire expression is false, checking the remaining expressions will waste CPU time. This is calledShort circuit evaluation

|| Operator

The || operator is called a logical or operator. It takes two expressions as operands and creates an expression that is true when any subexpression is true.
Here is an example of an if statement using the || operator:

if ((temperature < 20) || (temperature > 100))
cout << "The temperature is in the danger zone.";

If temperature is less than 20 or greater than 100, the cout statement will be executed. As long as one of these two subexpressions is true, the entire expression is true, and the cout statement is executed.
Note that the two things that are logical or connected together should be logical expressions, their values ​​are true or false, and the if condition written in the following form is incorrect:

if (temperature < 20 || >100)

As long as one of the subexpressions is true, the overall expression is true, and it doesn't matter whether the other subexpressions are false or true.
It should be noted that the || operator will also be performedShort circuit evaluation. If the subexpression to the left of the || operator is true, the subexpression to the right will not be checked because as long as one subexpression is true, the overall expression can be evaluated as true.

! operator

The ! operator is called a logical non-operator and performs logical NOT operations. It can invert the true or false value of an operand. In other words, if the expression is true, the ! operator returns false, and if the expression is false, it returns true.
Here is an example of an if statement using the ! operator:
if (!(temperature > 100))
cout << “You are below the maximum temperature. \n”;
First, the expression (temperature > 100) will be tested as true or false, and then the ! operator is applied to the value. If the expression (temperature > 100) is true, the ! operator returns false. If false, the ! operator returns true. In this example, it is equivalent to asking “Is the temperature not greater than 100?” or “Is it false if the temperature greater than 100?”

Boolean variables and ! operators
An interesting feature of a Boolean variable is that its value can be tested simply by naming. Assume moreData is a Boolean variable, and its test statement is as follows:
if (moreData == true)
It can be abbreviated as:
if (moreData)
Let’s look at the following statements:
if (moreData == false)
It can be abbreviated using logical non-operators:
if (!moreData)

&& , || and ! Priority

&& , || and ! The priority is:
!> && > ||
! operators have higher priority than many C++ operators. Therefore, to avoid errors, its operands should always be enclosed in parentheses unless it is intended to be applied to variables or simple expressions without other operators. For example, look at the following expression:
!(x > 2)
!x > 2
The first expression will! The operator is applied to the expression x > 2, which is asking "whether x is not greater than 2", however, the second expression is to ! Operators are applied to x. It is asking "whether the logic of x is greater than 2". Assume x is set to 5, since 5 is a non-zero value, it is considered true, and ! The operator inverts it to false, i.e. 0, and the > operator then determines whether 0 is greater than 2. To avoid this error, it is wise to always use brackets.
The && and || operators have lower priority than the relational operators, which means that the relational expression is calculated first, and then evaluated by the && and || operators, so there is:
a > b&& x < y is equivalent to (a > b) && (x < y)
a > b || x < y is equivalent to (a > b) | | (x < y)
Therefore, when the relational operator is mixed with && and ||, brackets are usually not required. Of course, using brackets is a good idea anyway, as they can make the program more readable.
When using && and || at the same time, brackets are strongly recommended because&& has higher priority than ||. If there are no brackets indicating the execution order, then && will always be done before ||, which may not be in line with the writer's original intention.
For example, suppose there are 3 boolean variables recentGrad, employed, and goodCredit, then the following expression:
recentGrad || employed && goodCredit
Equivalent to the following expression:
recentGrad || (employ && goodCredit)
But it is not the same as the following expression:
(recentGrad || employed) && goodCredit