If And Or

padvou

Active Member
Licensed User
Longtime User
Could someone please post a working example of this scenario:
We have x,y,z
and we want to check with If statements whether a combination of their values exists:
if x is "" and y is "" or z is ""
If x is '' or x is 0 and y is ""
 

lagore

Active Member
Licensed User
Longtime User
using standard mathematical notation re truth tables something like this should work, you just need to define what types x,y,& z are.
B4X:
If (x = "" AND y = "") OR z = "" Then
      'your code
   End If
   
   If (x = "" OR x = 0) AND y = "" Then
      'your code
   End If
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
How about <> ?
Is it valid to check if a value "is not" something?
ex. if x <> "" ?
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
This scenario does not work:
x and y are all empty ""
if y="" and (x<>"" or x<>"0") then
...
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
The expression I wrote
if y="" and (x<>"" or x<>"0") then
...
is expressed correctly to reflect my intention, however it is not working at runtime. I tried it adding a level meaning:
if y="" then
if x <>"" then
if x <> "0" then
...
end if
end if
end if
This way it works.
It doesn't work when they are all expressed in a single line.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
You need to re-think your condition as Erel says above.

What does this statement mean:
B4X:
(x<>"" or x<>"0")
If A = x <> "" : x is ANY string except for empty
If B = x <> "0" : x is ANY string except for 0

A OR B = x is ANY string.

So both your conditions re-include each other.

When you rewrite it as:
B4X:
if y="" then
if x <>"" then
if x <> "0" then
it forms and AND condition i.e. x <> "" AND x <> "0"
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
You need to re-think your condition as Erel says above.

What does this statement mean:
B4X:
(x<>"" or x<>"0")
If A = x <> "" : x is ANY string except for empty
If B = x <> "0" : x is ANY string except for 0

A OR B = x is ANY string.

So both your conditions re-include each other.

When you rewrite it as:
B4X:
if y="" then
if x <>"" then
if x <> "0" then
it forms and AND condition i.e. x <> "" AND x <> "0"

A or B is not any string. It is any string except zero and empty, zero being a string here. Isn' it so?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you should declare it as

if x is not "" and x is not "0"

so if (x<>"" and x<>"0") is valid since it will return a false when 1 statement if incorrect

in yours you have always a trigger been hit

x="0": if (x<>"" or x<>"0") here x is not "" but it is "0" so it returns true since 1 of both checks is correct

x="": if (x<>"" or x<>"0") here x is not "0" but it is "" so it returns true since 1 of both checks is correct

x="whatever": if (x<>"" or x<>"0") here x is not "0" and not "" so it returns true since both checks are correct
 
Last edited:
Upvote 0
Top