x>y && z==5 - how are parts of this expression called?

孤人 提交于 2019-12-06 04:08:24

What are the parts called?

>, &&, and == are all operators. Operands are the values passed to the operators. x, y, and z are the initial operands. Once x > y and z == 5 are evaluated, those boolean results are used as the operands to the && operator which means the expressions themselves are not the operands to &&, the results of evaluation those expressions are the operands.

When you put operands and an operator together, you get an expression (i.e. x > y, z == 5, boolResult == boolResult)

How are they evaluated?

In most (if not all) languages x > y will be evaluated first.

In languages that support short circuiting, evaluation will stop if x > y is false. Otherwise, z == 5 is next.

Again, in languages that support short circuiting, evaluation will stop if z == 5 is false. Otherwise, the && will come last.

>, &&, and == are all operators. Operands are the values passed to the operators. x, y, and z are the initial operands. Once x > y and z == 5 are evaluated, those boolean results are used as the operands to the && operator.

One alternative would be to turn to the grammar of C#

It states the following:

conditional-and-expression   &&   inclusive-or-expression

Just generalizing it as "expressions" is probably accurate enough :)

If your question is really what the parts left and right of the && are called, I’d say “expression”, maybe “boolean expression”.

Conditions, or in case of ||: Alternatives

In c# the && is an operator and the left and right are expressions. In an if statement, if the left evaluates to true the right will never be evaluated.

Kilanash

It's a boolean comparison expression that's comprised of two separate boolean comparison expressions.

Depending on the language, how this is interpreted depends on operator precedence. Since it looks like a C-like dialect, I'll assume && is short-circuit AND. (More explanation here).

Order of operations would be left to right as the equality testers (>, >=, <=, ==, !=) have equal precedence to boolean operations (&&, ||).

x > y would be evaulated, and if true, z == 5 would be evaluated, and then the first and second results would be ANDed together. However, if x > y was false, the expression would immediately return false, due to short-circuiting.

You're correct that x>y and z==5 are operands, and && is an operator. In addition, both of these operands in turn contain their own operands and operators. These are called complex operands

So:

  • x>y and z==5 are operands to the operator &&
  • x and y are operands to the operator >
  • z and 5 are operands to the operator ==

Regarding the individual component parts and how to name them:

  • Both == and > are comparison operators, which compare the values of two operands.
  • == is an equality operator, and evaluates to true if the left operand is equal to the right operand.
  • > is a greater than operator, and evaluates to true if the left operand is greater than the right operand.
  • && is a logical operator, specifically logical AND. It evaluates to true if both the left and the right operand are true.

When referring to each operand, it's standard to refer to them by their position, i.e. the left operand and right operand - although there's no "official" name - first and second operand work equally well. Note that some operators such as ! have only one operand, and some even have 3 (the ternary operator, which takes the form condition ? true_value : false_value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!