Question:
Which of the following will be .true. when the variable x is between 3.7 and 4.8?
Jay
2009-02-23 17:31:38 UTC
a. 3.7 < x & x < 4.8
b. 3.7 < x .and. x < 4.8
c. 3.7 < x < 4.8
d. x < 3.7 .or. 4.8 < x

i say C. but this is a computer programming class, is there a twist?
Four answers:
codrguy
2009-02-23 17:43:08 UTC
it depends on the programming language your class is thought in.

most languages have different syntax but for C, C++ and Java, for example, the way to do it is :

3.7 < x && x < 4.8

so it really depends on the language you are using, however B would be the best guess here, for me.



you want x > 3.7 to be true AND x < 4.8 . so you should use whatever the correct way to do 'logical and' in the language you are using. if a single '&' is the way to do it, then A is correct, but '&' is usually a bitwise operator, so i doubt it. C is not used in any language i know of and D is totally wrong.
Pat M
2009-02-24 02:22:51 UTC
a, b and c are logically equal so all 3 are actually technically correct.



EDIT: after reading navid's response I would have to agree with most of what he said but I think A is most likely syntactically right (as he said, it depends on the language). Let me just respond to his post by saying 1) I have never seen a language that b would work in and 2) a would work in Java.



In Java, when used with booleans, the conditional and bitwise operators yield the exact same results. The only difference between the two is & can be used with booleans and primitive integral types while && can only be used with booleans and && uses short circuit evaluation. In the question both 3.7 < x and x < 4.8 result in booleans so the result of using & vs && will be the same. (To quote the Java Language Specification, Section 15.23: "&& computes the same result as & on boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always."
Derek Ellingwood
2009-02-24 01:37:19 UTC
The answer is b:



&& means .and.

|| means .or.

It would make sense to say c, however, a comparison is made with a vs b not a vs b vs c vs...
C B
2009-02-24 01:41:01 UTC
The answer is d. B is not right because it uses and and both conditions are not met.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...