if and or statements not working as intended

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
EddieLee
Posts: 118
Joined: Thu Oct 25, 2018 1:06 pm

if and or statements not working as intended

Post by EddieLee » Wed Aug 26, 2020 4:31 pm

Hi All,


Anyone has the same issue as me ? i have this fld "search" with the following codes

Code: Select all

if text of fld "search" is not "Search ...”  or text of fld "search" is not empty then
(the following codes will happen)
else
put "Search ..." into fld "search”
focus on nothing
end if
So when the user tries to search when the text of the fld is "Search ..." it should have went to the else statement but it is running the if statement. Is there something wrong with how i use the OR operator here ?

Thanks !
Last edited by EddieLee on Wed Aug 26, 2020 6:11 pm, edited 1 time in total.
Eddie :D

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: if and or statements not working as intended

Post by dunbarx » Wed Aug 26, 2020 5:28 pm

Eddie.

It is hard to tell, because the code you posted has lots of syntax issues, mainly missing quotes. Rewriting to:

Code: Select all

on mouseup
   if text of fld "search..." is not "Search" or text of fld "search" is not empty then
   --(the following codes will happen)
  beep
else
   put "Search ..." into fld "search"
   focus on nothing
end if
end mouseup
This beeps always, because the initial conditional allows, well, everything. But at least it compiles and runs.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: if and or statements not working as intended

Post by FourthWorld » Wed Aug 26, 2020 5:58 pm

Change the first "or" to "and".
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: if and or statements not working as intended

Post by dunbarx » Wed Aug 26, 2020 6:01 pm

Eddie.

Revisiting the above post.

The first conditional fires if the contents of the field "search" is not empty or if the contents does not contain the string "search..."

Can you think of any example of the contents of the field that would NOT fire? :wink:

Just as an aside, these two lines of code are identical:

Code: Select all

if text of fld "search..." is not "Search" or text of fld "search" is not empty then
if fld "search..." is not "Search" or fld "search" is not empty then
LC has a flexible syntax, even though it has a very well defined syntax.

Craig

EddieLee
Posts: 118
Joined: Thu Oct 25, 2018 1:06 pm

Re: if and or statements not working as intended

Post by EddieLee » Wed Aug 26, 2020 6:05 pm

dunbarx wrote:
Wed Aug 26, 2020 5:28 pm
Eddie.

It is hard to tell, because the code you posted has lots of syntax issues, mainly missing quotes. Rewriting to:

Code: Select all

on mouseup
   if text of fld "search..." is not "Search" or text of fld "search" is not empty then
   --(the following codes will happen)
  beep
else
   put "Search ..." into fld "search"
   focus on nothing
end if
end mouseup
This beeps always, because the initial conditional allows, well, everything. But at least it compiles and runs.

Craig
Hi Craig,

Sorry for the typo, there should be a quote behind is not “Search ...”

I will remove the text of fld “search” to just fld “search”
What do you mean by example of the contents of the field that would not fire? And also, what’s the beep for? Just tried the changes but it still fires the if when it should be firing the else.

Eddie
Last edited by EddieLee on Wed Aug 26, 2020 6:13 pm, edited 1 time in total.
Eddie :D

EddieLee
Posts: 118
Joined: Thu Oct 25, 2018 1:06 pm

Re: if and or statements not working as intended

Post by EddieLee » Wed Aug 26, 2020 6:19 pm

FourthWorld wrote:
Wed Aug 26, 2020 5:58 pm
Change the first "or" to "and".
Hi,

I wonder why this work? I changed to and and it worked totally. Why wouldn’t or work?

Eddie
Eddie :D

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: if and or statements not working as intended

Post by dunbarx » Wed Aug 26, 2020 6:33 pm

Eddie.

Richard was more direct than I was, since I like to assign homework.

Code: Select all

if text of fld "search..." is not "Search" or text of fld "search" is not empty then
"or" means that if either part of your two part conditional is true, it will fire:

-- if the field is empty, the field does not contain "search"
-- If the field contains "search", it is not empty.
-- If the field contains any other text at all, it is neither empty nor does it contain "search".

That was your homework, when I glibly said:
Can you think of any example of the contents of the field that would NOT fire? :wink:
"And" requires that BOTH parts of the conditional MUST be true, not that either CAN BE true.

You owe me homework.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: if and or statements not working as intended

Post by FourthWorld » Wed Aug 26, 2020 6:54 pm

dunbarx wrote:
Wed Aug 26, 2020 6:33 pm
Richard was more direct than I was, since I like to assign homework.
Well done, Craig. I appreciate your drawing attention to your method. It's more engaging, and as such more likely to increase retention.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

EddieLee
Posts: 118
Joined: Thu Oct 25, 2018 1:06 pm

Re: if and or statements not working as intended

Post by EddieLee » Thu Aug 27, 2020 6:43 am

dunbarx wrote:
Wed Aug 26, 2020 6:33 pm
Eddie.

Richard was more direct than I was, since I like to assign homework.

Code: Select all

if text of fld "search..." is not "Search" or text of fld "search" is not empty then
"or" means that if either part of your two part conditional is true, it will fire:

-- if the field is empty, the field does not contain "search"
-- If the field contains "search", it is not empty.
-- If the field contains any other text at all, it is neither empty nor does it contain "search".

That was your homework, when I glibly said:
Can you think of any example of the contents of the field that would NOT fire? :wink:
"And" requires that BOTH parts of the conditional MUST be true, not that either CAN BE true.

You owe me homework.

Craig
Hi Craig,

So I understood wrongly, my bad. So the if will fire if either 1 condition is true. Which is why when fld “search” is “Search”, it fire because of the second condition when fld “search” is not empty is true. So by using AND, it will fire only when both conditions are true and not just 1. Thanks!

Eddie
Eddie :D

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”