Is there a way to represent logical not with && or || - algorithm

Is there a way to represent logical not, with && or || or some statement like if or ?:
I want to implement this in some other way:
boolean isRunning = true;
startButton.setEnabled(!isRunning); // <<== ???

Assuming that you are doing this as an exercise, ternary operator lets you replace ! in a simple and straightforward way:
startButton.setEnabled(isRunning ? false : true);
As far as using && and || by themselves goes, this pair of operators is not functionally complete, i.e. there are operations that cannot be implemented by using a sequence of &&s and ||s alone; not ! operation is among operations that cannot be implemented with ands and ors.

Related

Will `&&` always run before `if` on the same line in Ruby

Will every && run before if on the same line in Ruby?
For example:
#building.approved = params[:approved] if xyz && abc && mno...
Can an unlimited number of && be used on the right side of an if without using parentheses?
I'm inclined to use parentheses but I'd like to understand the default behaviour.
Everything after the if must be part of the condition by virtue of the syntax. The only way to get around this is to be really specific:
(#building.approved = params[:approved] if xyz) && abc && ...
Which is obviously not what you're intending here.
Operator binding strength isn't an issue here since if is a syntax element not an operator, so it has the absolute lowest priority.
The only conditions that will be evaluated are the ones that produce a logically false value, or come after one that was logically true as the first one to return logically false will halt the chain.
That is:
if a && b && c
Will stop at a if that is a logically-false value. b and c will not be evaluated. There's no intrinsic limit on chaining though.

How are many comparings connected? Do I need the brackets for `(x != y) && z`?

I'd like to know how many "comparings" in Ruby are connected. Do I need the brackets for the following expression?
(x != y) && z
My guess is no, but I'm not 100% familiar on how Ruby evaluates such things. I would say: comparing operators (don't know how they are called correctly) like !=, == etc. are evaluated first, and after this the combination operators like && and || are evaluated.
Where do I find more informations about this?
No you don't need the parentheses: != has higher precedence than &&.
Refer to the precedence table for more info.

Coding styles in conditional expression of some programming languages

It's a bit confusing to me about what is the difference between these condition expressions below:
if( 1 == a) {
//something
}
and
if( a == 1 ) {
//something
}
I saw the above one in some scripts I have downloaded and I wonder what's the difference between them.
The former has been coined a Yoda Condition.
Using if(constant == variable) instead of if(variable == constant), like if(1 == a). Because it's like saying "if blue is the sky" or "if tall is the man".
The constant == variable syntax is often used to avoid mistyping == as =. It is, of course, often used without understanding also when you have constant == function_call_retuning_nothing_modifiable.
Other than that there's no difference, unless you have some weird operator override.
Many programming languages allow assignments like a = 1 to be used as expressions, making the following code syntactically valid (given that integers can be used in conditionals, such as in C or many scripting languages):
if (a = 1) {
// something
}
This is rarely desired, and can lead to unexpected behavior. If 1 == a is used, then this mistake cannot occur because 1 = a is not valid.
Well, I am not sure about the trick. Generally, we could say the equal sign is commutative. So, a = b implies b = a. However, when you have == or === this doesn't work in certain cases, for example when on the right side you have a range: 5 === (1..10) vs. (1..10) === 5.

tool for testing logic expressions

can anyone recommend software (preferably for mac) or a web based tool, that can be used to evaluate logic expressions?
For example I would like to be able to quickly test whether two expressions like:
$a = 'foo';
$b = 'bar';
$c = 'foo';
( !(($a == $c) && ($b == $c)) )
// and
( ($a != $c) || ($b != c$) )
are interchangeable or not.
And also, is there generally an agreed upon best practice in relation to how to construct such expressions? For example to try to minimize the use of the negation, the order of the elements or something like that?
Sometimes I find myself struggling a bit with these things :)
You can also use Wolfram Alpha
https://www.wolframalpha.com/input/?i=P+%26%26+(Q+%7C%7C+R)&lk=3
or
https://www.dcode.fr/boolean-expressions-calculator
You can use something like http://www-cs-students.stanford.edu/~silver/truth/ and compare the generated truth tables.
I like this site which does exactly what you are looking for, but it only supports limited logical operators:
https://electronics-course.com/boolean-algebra

What is the difference between 'and, or' and '&&, ||'? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
PHP - and / or keywords
Ruby: difference between || and 'or'
What is the difference between this:
a=20
b=30
if (a > 10 && b > 10)
puts a
end
and this:
if (a > 10 and b > 10)
puts a
end
and also explain difference between "||" and "or"
The answer relates to the PHP tag that has been assigned to this question, but has been deleted afterwards
They are the same with one small difference. The and and or operator have a lower precedence compared to && and ||.
Take this example from the manual:
// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;
// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;
You can read about operators in PHP's documentation.
Only the difference in precedence
As you can see from the precedence table for Ruby, the difference is also precedence, as it is in PHP: and and or have lower precedence than && and ||.
But there is a slight difference from PHP: in Ruby, and and or have the same precedence.
I got the answer. Please go and read it. this is what i'm expect it.
http://avdi.org/devblog/2010/08/02/using-and-and-or-in-ruby/
Thanks all of your efforts

Resources