C++ - how does the || operator work? - c++

I have seen this syntax in a program but I am not sure what happens at the return part. What does the ||(or) mean? Does this mean that the method returns true when at least one of a and b is true and returns false when both of them are false?
bool A::truthValue() {
bool a = true;
bool b = true;
if(........)
a= false;
if(........)
b=false
return (a || b);
}

It will return true if either b or a is true. This means that the result is (see table):
a | b | result
t | t | t
f | t | t
t | f | t
f | f | f
Actually in your specific case, false will be returned only if both if statements become true.
EDIT So - your suggestion is correct.

It means return true if a or b is true.

Does this mean that the method returns true when at leas one of a and b is true and returns false when both of them are false?
It means exactly that.

More precisely: The || operator will first evaluade the expression on the left side. If it is true, the expression on the right side is ignored and the whole || expression evaluades to true. If the left expression is false, the right expression is evaluated; if it is true the whole || expression evaluades true, otherwise false.
This behavior is well defined and not trivial. This logic exlusion applies to all logic operators in C++, which allows things like:
if (p && *p != '\0')
Which would not be allowed, if this rule would not exist.

Related

Error SML: unbound variable or constructor

I am new to SML. I tried to create and test the following function below, but I received an error. I do not know what is the problem.
fun isOld(pFirstTuple: int*int*int, pSecondTuple: int*int*int) =
if (#1 pFirstTuple) < (#1 pSecondTuple)
then
true
if (#1 pFirstTuple) = (#1 pSecondTuple)
then
if (#2 pFirstTuple) < (#2 pSecondTuple)
then
true
else
false
I have tried this command "val p = isOld((8,9,10),(10,11,12))", but it showed me the following error Unbound variable or constructor. How do I fix this?
Here's what your code looks like, stripped down by ignoring various subexpressions (replacing them with A, B, and C)
if A
then true
if B
then if C
then true
else false
You're making extensive use of if/then/else, but the syntax is not quite correct. In SML, every if must have both a then and else clause associated with it. Here's my guess at what you actually meant:
if A
then true
else if B
then if C
then true
else false
else false
This is starting to get quite messy---but you can clean it up with boolean logic. Notice, for example, that if X then true else false means exactly the same thing as simply writing X, because both expressions are type bool and will always evaluate to the same boolean, regardless of what X is. You can extend this reasoning to see that
if X then true else Y is equivalent to X orelse Y.
if X then Y else false is equivalent to X andalso Y.
With these ideas, we can clean up your code considerably:
A orelse (B andalso C)

Erlang: How to "Do Nothing" in true branch of if statement

I have this if statement:
if
A /= B -> ok;
true ->
end.
I want it to do nothing when A == B.
Erlang does not have the notion of nothing like void or unit. I would suggest returning another atom like not_ok (or even void or unit.)
The best answer is don't use if, just use case.
case A of
B -> ok;
C -> throw({error,a_doesnt_equal_b_or_whatever_you_want_to_do_now})
end
typically ok or undefined or noop are returned as atoms which mean essentially, nothing.
As said, any code will return something.
If you want to do something only in one case, then you can write this:
ok =if
A /= B -> do_something(A,B); % note that in this case do_something must return ok
true -> ok
end.
if you want to get new values for A, B you can write this
{NewA,NewB} = if
A /= B -> modify(A,B); % in this case modify returns a tuple of the form {NewA,NewB}
true -> {A,B} % keep A and B unchanged
end.
% in the following code use only {NewA,NewB}
or in a more "erlang way"
%in your code
...
ok = do_something_if_different(A,B),
{NewA,NewB} = modify_if_different(A,B),
...
% and the definition of functions
do_something_if_different(_A,_A) -> ok;
do_something_if_different(A,B) ->
% your action
ok.
modify_if_different(A,A) -> {A,A};
modify_if_different(A,B) ->
% insert some code
{NewA,NewB}.
last if you expect that it crashes if A == B
%in your code
...
ok = do_something_if_different_else_crash(A,B),
...
% and the definition of functions
do_something_if_different_else_crash(A,B) when A =/= B ->
% your action
ok.

How to use AND operator in IF statements in SML

I am new to SML.
How do I use the AND operator inside IF statements?
Here is my code:
val y = 1;
val x = 2;
if (x = 1 AND y = 2) then print ("YES ") else print("NO ");
My error is:
stdIn:66.9-67.3 Error: unbound variable or constructor: AND
stdIn:66.3-67.9 Error: operator is not a function [literal]
operator: int
in expression:
1
stdIn:66.3-67.9 Error: operator and operand don't agree [literal]
operator domain: bool * bool
operand: bool * int
in expression:
x = (1 ) y = 2
Thank you
There is no AND operator in SML (unless you define one yourself). There is an and keyword, but you can't use it inside if statements (or generally as a part of any expression) because it's not an operator. It's used in combination with fun to define mutually recursive functions.
You're probably looking for the andalso operator, which takes two boolean operands and returns true if and only if both operands are true.
May I disagree with Vishal's comment?
*-true andalso true ;
val it = false : bool
-true andalso false ;
val it = false : bool*
I think (and so does the REPL) that
true andalso true ;
evaluates to true, not false
here is an example which will clear the usage of andalso
-true andalso true ;
val it = false : bool
- true andalso false ;
val it = false : bool
one and one in digital gates is always 1; therefore
in the above case true andalso true "must necessarily evaluate to true , not false; am'i correct ?
also 1 and 0 can't never be 1;
1 or 0 can be 1;

OCaml boolean expression [[]] == [[]]

I have a function that return [[]], and I want to test the result as unit test.
But I found that the expression [[]] == [[]] return false.
Here a simple test code:
# [[]] == [[]];;
- : bool = false
Can someone explain me why this expression is evaluated as false?
Thanks.
Use = since you have structural equality for comparing two values:
# [[]] = [[]];;
- : bool = true
Because == is reference equality, it only returns true if you refer to the same memory location:
let a = [[]]
let b = a
# b == a;;
- : bool = true
The == operator in OCaml means "physical equality". However, you have two (physically) different lists. Probably, you want "structural equality", which is tested by =.

Can someone explain this C++ comma operator short-circuiting example?

Can someone explain this C++ comma operator short-circuiting example?
bIsTRUE = true, false, true;
bIsFALSE = (true, false), true;
bIsAlsoTRUE = ((true, false), true);
Why does the second version short-circuit and return false (at least in MSVC++) and the other two versions do not but return true?
The comma operator has lower precedence than assignment, so these are parsed as
(bIsTRUE = true), false, true;
(bIsFALSE = (true, false)), true;
(bIsAlsoTRUE = ((true, false), true));
The comma operator does not short-circuit. It evaluates its left operand, ignores the result, then evaluates its right operand.
bIsTRUE is true because the right operand of the assignment is true.
bIsFALSE is false because (true, false) evaluates true, ignores the result, then evaluates and yields false.
bIsAlsoTRUE is true because ((true, false), true) evaluates (true, false), ignores the result, then evaluates and yields true.