简体   繁体   English

如何解决此逻辑运算符问题?

[英]How do I solve this logical operator problem?

My professor gave us a logical operator worksheet from my C++ clas and I got stumped on this problem. 我的教授从我的C ++用户那里给了我们一个逻辑运算符工作表,我为此感到困惑。 If x = -2, y = 5, z = 0, and t = -4, what is the value of each of the following logical expressions? 如果x = -2,y = 5,z = 0和t = -4,那么以下每个逻辑表达式的值是多少?

3 * y / 4 < 8 && y >= 4

I get stuck on this step after I plug everything in. 3 < 8 && 5 插入所有电源后,我会卡在此步骤中。3 <8 && 5

I know that on the left 3 * 5= 15, and 15 / 4 = 3. Now the other side is where I get stuck at. 我知道在左边3 * 5 = 15,而15/4 =3。现在另一边是我被卡住的地方。 I know 5 is true since it's greater than or equal to 4. But I don't know what to do with next when its 8 && 5. Can anyone help? 我知道5是正确的,因为它大于或等于4。但是当8 && 5时,我不知道下一步该怎么做。有人可以帮忙吗?

You can put parenthesis around the various sub-expressions in your expression by following the order of precedence of operators and their associativity . 您可以通过遵循运算符的优先级及其关联性,在表达式的各个子表达式中加上括号。

3 * y / 4 < 8 && y >= 4

is

(3 * y) / 4 < 8 && y >= 4

is

((3 * y) / 4) < 8 && y >= 4

is

(((3 * y) / 4) < 8) && y >= 4

is

(((3 * y) / 4) < 8) && (y >= 4)

That should give you a clear guideline of what the expression should evaluate to. 那应该给您一个明确的表达准则的指导。

This seems to be an exercise in operator precedence. 这似乎是运算符优先级的一种练习。 When precedence is taken into account, the statement 3 * y / 4 < 8 && y >= 4 is equivalent with 考虑优先级时,语句3 * y / 4 < 8 && y >= 4

(((3 * y) / 4) < 8) && (y >= 4)

Substituting the variables, we have 代入变量,我们有

(((3 * 5) / 4) < 8 && (5 >= 4)

After doing the math, we end up with 经过数学运算,我们最终得到

(3 < 8) && (5 >= 4)

3 is indeed lesser than 8, and 5 is indeed greater or equal than 4, so both sides of boolean and are true, and the whole expression is evaluated to true. 3的确小于8,而5的确大于或等于4,因此布尔and两边都为true,整个表达式的求值为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM