views:

108

answers:

7

Hi, I know && is the logical operator here, also conditions on the left and on the right are operands, right? Like:

1+1 is an expression where + is the operator and the numbers are operands. I just do not know whether the condition itself is called the operand as well because it get compared by an operator. I guess so.+

Thanks

+6  A: 

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.

Justin Niessner
Interesting how many upvotes this answer gets. None of you has read the question. :)
Scytale
That's because it's not clear exactly what the question is. I added more information to cover both of the questions I see (one in the title, another in the body of the question).
Justin Niessner
+1  A: 

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

Scytale
A: 

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.

Alex Mendez
Are you sure about that? I do not think they are called expressions as expression is the whole that will result to false or true. I think that operator has to operate with operands.
Bliznak
You are right to say that the whole is an expression. However, if you were to use the left an right individually in an if statement they would be an expression. So, you might say that you have two sub expression between an operator.
Alex Mendez
A: 

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.

Kilanash
A: 

Conditions, or in case of ||: Alternatives

Dario
A: 

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

Ryan Brunner
Thanks. Anyway, is it correct to say "the condition on the left of the operator"? Or I need to say the operand on the left of the operator. Do not mind the grammar please, just the meaning :)
Bliznak
+2  A: 

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 :)

Jakob