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

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

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.

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

Related

Python going through all if statements even if the if doesn’t match [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 5 months ago.
I have a calculator script, and I have a variable for the operator, number 1 and number 2, this is what it looks like
Operator = input(“operator: “)
Num1 = input(“num1 here: “)
Num2 = input(“num2 here: “)
If Operator == “x” or “*”:
#code
I have one of these for all four main math equations but it’s goes through all the ifs and elifs in order even if the ‘operator’ input doesn’t match. Note I have pyttsx3 installed and a text to speech is in the code. Any help would be appreciated.
Your comparison needs to check both conditions.
It should look like:
If Operator=='x' or Operator=='*'
Right now it's evaluating like:
If (Operator=='x') or ('*'==True)
In python a string (like '*') will evaluate as 'True', so your code is always being executed.
The or operator returns the first element if it is truthy, else the second element. Then if looks at what it gets. In this case (Operator == "x") or "*" evaluates to "*", which in turn is truthy. So the if block is entered.
What you would like to do is probably something like
if ( Operator == "x" ) or ( Operator == "*" ):
# code
To back my claim, you can find the definition of or here.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The string "*" is not empty, so it evaluates to true. See here.

How to interpret this discussion of Verilog relational operators

This discussion of Verilog relational operators at ASIC World clearly has at least one mistake:
The result is a scalar value (example a < b)
0 if the relation is false (a is bigger then b)
1 if the relation is true (a is smaller then b)
x if any of the operands has unknown x bits (if a or b contains X)
Note: If any operand is x or z, then the result of that test is
treated as false (0)
Clearly, "a is bigger than b" should be "a is bigger than or equal to b".
There is something else that looks wrong to me, but I don't know if it's just because I'm a Verilog novice. The last bullet point seems to contradict the subsequent note, unless there is a difference between an operand having all unknown bits (in which case the result of a relational operator will be x) and an operand being x (in which case the result will be 0).
Is there a difference between an operand being x and all of its bits being X? I know Verilog is case-sensitive.
verilog is known for its x-propagation pessimism.
From lrm 11.4.4
An expression using these relational operators shall yield the scalar value 0 if the specified relation is false
or the value 1 if it is true. If either operand of a relational operator contains an unknown (x) or highimpedance
(z) value, then the result shall be a 1-bit unknown value (x).
so, if any of the values contains 'x' bits the result will be 'x'.
Now in case, when the result is used as a conditional expression, the if statement will take true brunch if and only if the result is '1'. Otherwise it will take the false branch. Also, there are conversion rules in verilog, where x and z values are converted to 0 in binary operations, which conditional operation is.
so, the comment on the site is correct, it is talking of the results of a test (as in if statement)
If any operand is x or z, then the result of that test is treated as false (0)
I think you should take your comments to the author of that website.
I take the statement inside the ()'s to be an example
1 if the relation is true (if for example, a is smaller then b)
The subsequent note refers to a more general issue not specific to relational operators. When you have
if (expression) true_statement; else false_statement;
When expression evaluates to an X or 0, the false_statement branch is taken.
Also, Verilog is not case sensitive about numeric literals. 'habcxz and 'hABCXZ are equivalent.

how does && work when both the variables are the same?

I'm learning groovy to work on smartthings and found a relatively common command among the various examples and existing code (see below).
Reading the function of the && operator I would think the "&& cmd.previousMeterValue" is superfluous. Or is there some code shortcut I'm missing?
Thanks
John
if (cmd.previousMeterValue && cmd.previousMeterValue != cmd.meterValue) {
do something
}
Not knowing what type previousMeterValue has, this answer is somewhat generic.
Groovy follows common operator precedence, i.e. != is evaluated before &&.
To show it explicitly, the full expression is the same as:
(cmd.previousMeterValue) && (cmd.previousMeterValue != cmd.meterValue)
cmd.previousMeterValue is testing the value for the Groovy-Truth.
Depending on value type, the following might be applicable:
Non-null object references are coerced to true.
Non-zero numbers are true.
So if the value is null or 0, the expression is false.
If the first part of the expression evaluated to false, then the second part is skipped.
The logical && operator: if the left operand is false, it knows that the result will be false in any case, so it won’t evaluate the right operand. The right operand will be evaluated only if the left operand is true.
If the first part of the expression evaluated to true, then cmd.previousMeterValue != cmd.meterValue is evaluated, using the following rule:
In Groovy == translates to a.compareTo(b)==0, if they are Comparable, and a.equals(b) otherwise.
So if value is a number object, then it is evaluated as:
cmd.previousMeterValue.compareTo(cmd.meterValue) != 0
This means that BigDecimal values are compared by value, ignoring specific scale.

On use of parenthesis

So I was looking over some A-Levels Computer Science past papers, and stumbled into this:
Now, my first reaction was that there is no need for parenthesis on line 6. Reason being that algebraic operators take precedence over comparisons which take precedence over boolean ones.
As a small example from Java:
int a = 100;
int b = 100;
int c = 100;
int d = 100;
if( ((c+d) > 180) && ((a+b+c+d)) >= 320)
System.out.println("greater");
if(c+d > 180 && a+b+c+d >=320)
System.out.println("greateragain");
Both if statements are evaluated to true.
So, am I right in thinking parenthesis are only for human readability in this case or...?
You can say: "The use of parentheses makes the precendence of evaluation explicit, disregarding of the operator precendence of the language in use."
The comments above describe well that operator precendence is language specific. For example, in Pascal, logical operators such as AND seem to have higher precendence than math operators, and interpreted as binary AND. In contrast in C, the && has lower precedence, hence you can save some parentheses.
Therefore, it sounds like a wise idea to always use parentheses in case of a possible ambiguity, or at least until you're mastering the language in use.
BASIC was one of the early languages that took advantage of the fact that logical operators and bit-wise operators can share the same mnemonic.
In the example - c+d > 180 AND a+b+c+d >= 320 could (by a stretch of the imagination) have been interpreted as (c + d) > (180 AND a) + b + c + (d > 320). For this reason it is necessary to add brackets to eremove all ambiguity.

Nested conditional operator interpreted as String

In my Java EE 7 / JSF / EL 3.0 project, I need to define the style class of a tag depending of the condition:
if x < a, then the class is LT-A
else if x > b, then the class BT-B
else the class is BETWEEN-A-B
The only solution I could find is using a nested conditional operator:
styleClass="#{x<a ? 'LT-A' : (x>b ? 'BT-B' : 'BETWEEN-A-B')}"
However, my problem is that the (x>b ? 'BT-B' : 'BETWEEN-A-B') part gets interpreted as a String (and thus is rendered as-is instead of actually evaluated).
Is something wrong with the syntax?
The < and > are being misinterpreted. If you use lt and gt everything should work just fine.
Here's a full list of EL logical operators:
and - Logical operator alternative to (&&)
false - Boolean literal
le - Relation operator less than or equal to alternative to (<=)
not - Logical operator reverse alternative to (!)
div - Arithmetic operator division alternative to (/)
ge - Relational operator greater or equal to alternative to (>=)
lt - Relational operator less than alternative to (<)
null - Null literal
empty - The empty operator is a prefix operation that can be used to determine whether a value is null or empty.
gt - Relational operator greater than alternative to (>)
mod - Arithmetic operator modulo alternative to (%)
or - Logical operator alternative to (||)
eq - Logical operator alternative to (==)
instanceof - Java Keyword to do a Class comparison between Objects
ne - Relational operator not equal alternative to (!=)
true - Boolean literal
courtesy of javaevangelist.blogspot.co.at

Resources