Multiple Boolean statements VBA - vba

I am having issues with some Boolean logic.
Essentially I want something to program in VBA a filter such that,
A = True AND (B = True OR B = False)
I just cannot seem to get the coding right to do this in VBA (I am using MS Access).
I have tried:
A = True AND B = True OR B = False
But this obviously fails (looks for either A,B = True OR B = False, essentially).
Am I missing something obvious here?
I chose to left out the actual code, but if requested I can post. My thinking is that I am missing some basic with the Boolean logic.

If A, B, and C are Boolean expressions, then comparing them to a Boolean literal (True, False) is redundant: the expression is already a Boolean expression.
That makes the original logic go like this:
If A And (B Or Not C) Then
The And operator has greater priority than the Or operator, so it gets evaluated first; if the (B Or Not C) part needs to be evaluated as a whole, then the parentheses are required, otherwise A And B takes precedence, making the expression (wrongly) evaluate as:
If (A And B) Or Not C ' redundant parentheses to illustrate operator precedence
Edit: I misread the OP, my brain put that C in there. If A And (B Or Not B) Then is redundant, as BigBen pointed out - the simplified logic would be If A Then. Still worth noting operator precedence and using parentheses when applicable and appropriate though.

Related

How is boolean expression evaluated by the precedence of logical operator?

NOT (A AND B OR D) AND (A OR C AND D). Given A = True, B = False, C = False, D = True.
There are two set of parentheses, (A AND B OR D) and (A OR C AND D). NOT has highest precedence, AND being the middle and OR being the last.
So, my question is, after evaluate A AND B in the first set of parentheses, will it jump to the second parentheses and evaluate C AND D and then jumping back to the first set of parentheses?
or
Will it complete the whole first set of parentheses before evaluate the next set of parentheses?
Thanks in advance.

Why use the OR operator instead of the AND operator for a while loop?

So someone has briefly explained this to me, but unfortunately I still do not understand.
My thinking is, we use an AND because this means we need both conditions to be met in order to pass.
Whereas with an OR, it only requires one condition to pass.
So how come in my example we are using an OR operator for both conditions to be met?
#DOUBLE == MEANS EQUALITY
#SINGLE = MEANS ASSIGNMENT
#THIS WILL BE THE LEGIT USER CHOICE WHERE OUR CHOICE HAS TO BE
#A NUMBER THAT IS WITHIN RANGE, SO TWO VARIABLES TO MEET BIG BOY
def my_choice ():
#VARIABLES SECTION
#INITIALS
choice = 'wrong'
accepted_range = range(1,10)
within_range = False
#Just like our choice we have to give the false answer here to keep
#the while loop- why? I dont know yet, will update
#TWO CONDITIONS TO CHECK
#1-MAKE SURE ITS AN ACTUAL NUMBER
#2-MAKE SURE ITS WITHIN THE RANGE
#CODE TIME
while choice.isdigit()==False or within_range == False:
choice = input('Please enter a value bettwen 1-9, Thanks ')
#Digit check
if choice.isdigit() == False:
print('sorry mate {} is not a digit'.format(choice))
#Range Check
#If we have passed the digit check, we can use it in our range check
if choice.isdigit() == True:
#remember that input returns a string ya?
if int(choice) in accepted_range:
within_range = True
print('Well done, {} is defintely a number in range'.format(choice))
else:
within_range = False
print('Sorry, you have picked a number, just not in range')
If you use boolean algebra you can show that these are equivalent:
is_digit or within_range
not (is_digit and within_range)
not is_digit and not within_range
You can also make a truth table to confirm this.

operators precedence and associativity in C

i would be grateful if somebody could help me with this problem. The book I am currently reading has a question
Q What will be the output?
#include <stdio.h>
void main()
{
int a = 3, b = 2;
a = a ==b==0;
printf("%d, %d",a,b);
}
The answer is given as
1,2 ( even on codeblocks got the same answers)
Now i understand that equality operator has precedence over the assignment operator.
So it must be a== b or b == 0 first
Then as both the above have the same operator, The associativity rule causes
a == b to be evaluated first.
But from here on I am lost!
How does one get to 1 and 2 as the answer?
See https://en.cppreference.com/w/cpp/language/operator_precedence
Note the line that says, "Operators that have the same precedence are bound to their arguments in the direction of their associativity." You can see the associativity for each operator on the far right column.
Note that equality operator is on line 10, with associativity left-to-right.
Note that assignment is line 16, so it has lower precedence than equality.
// original
a = a == b == 0
// precedence rule
a = (a == b == 0)
// associativity rule
a = ((a == b) == 0)
I believe that the equality operator does does not have precedence over the assignment operator. C just works itself from left to right.
in
a = a == b == 0;
a is assigned to everything on the right of the equal sign.
On the right side of the equal sign, a == b is evaluated to false. Then we compare equality of the answer to a==b with 0. In c, 0 is equal to false, so we get true. This is our answer to "everything to the right of the equal sign". So then, we assign that value(true) to a, which is an integer. Because of automatic conversion, true is converted to 1 and assigned to a. Now a equals 1.
As an equation, the above process can be represented as: a = (( a == b ) == 0)
Hope this makes sense. Tell me if I need to clarify further.

Example of programming language that is left associative?

I was teaching a friend about programming and I had a hard time convincing them that a = b and b = a are two very different things.
I eventually found the correct words to describe it (right associative) which got me thinking.
Are there any programming languages which are left associative? I have never seen a language where:
a = b results in b being set to the value of a.
You misunderstood associativity. An operator op is associative if (a op b) op c is equivalent to a op (b op c). For operators that are not associative it thus becomes relevant whether a op b op c stands for the former or the latter. Thus we distinguish between left-associative operators, where it's (a op b) op c, and right-associative operators, where it's a op (b op c).
Most operators in most languages are left-associative. Take for example -: a - b - c is equivalent to (a - b) - c in most languages, not a - (b - c).
The assignment operator is an exception from that as (a = b) = c is generally not legal (as you can't assign to the result of an assignment). Thus in most languages a = b = c is equivalent to a = (b = c). A notable exception is Python where a = b = c does not associate at all and is simply illegal.
None of this has anything to do with the difference between a = b and b = a. Since this involves only a single use of the = operator, associativity does not factor into this at all. Rather the relevant property is commutativity: An operator op is commutative if a op b is equivalent to b op a. I'm not aware of any language where assignment is commutative, nor do I have an idea of how it could be.
Concepts like left-commutativity or right-commutativity do not exist. There is, to the best of my knowledge, no term for the question "Does a = b assign b to a or vice-versa?" - that's just part of the semantics of the assignment operator.
I don't believe so. Though you can always overload the assignment operator and cause complete confusion inside your c++.
R has both <- and -> assignment operators defined.
> b <- 42
> b -> a
> a
[1] 42
If we talk about Operator associativity we should actually consider two types of associativity: side_associative (e.g. = ) or non-associative (e.g. == ) as it doesn't make any difference for a machine if it get's the line from left to right or from right to left.
There are no such programming languages that are originally left associative but some of them allow it via operator overloading and some (e.g. R) will allow both: -> and <-.
I believe no-one will like it in Europe but it might be found lovely in the Middle East. I can imagine that there are IDEs that switch right-side with left-side but at the end compilers are written in an European way.

Using two equals signs in Visual Basic 2008

In code, why wouldn't this work?
intMax = intTopValue = 20
This is interpreted as intMax = (intTopValue = 20).
intTopValue = 20 will check whether intTopValue is equal to 20 and return true or false.
This boolean will then be assigned to intMax.
Most languages don't have this issue, since they use separate operators for assignment (= or :=) and equality (== or =).
By contrast, VB shares = for both operations. Therefore, when a = b is written as an expression, it always means equality.