Page 1 of 1
if and or statements not working as intended
Posted: Wed Aug 26, 2020 4:31 pm
by EddieLee
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 !
Re: if and or statements not working as intended
Posted: Wed Aug 26, 2020 5:28 pm
by dunbarx
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
Re: if and or statements not working as intended
Posted: Wed Aug 26, 2020 5:58 pm
by FourthWorld
Change the first "or" to "and".
Re: if and or statements not working as intended
Posted: Wed Aug 26, 2020 6:01 pm
by dunbarx
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?
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
Re: if and or statements not working as intended
Posted: Wed Aug 26, 2020 6:05 pm
by EddieLee
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
Re: if and or statements not working as intended
Posted: Wed Aug 26, 2020 6:19 pm
by EddieLee
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
Re: if and or statements not working as intended
Posted: Wed Aug 26, 2020 6:33 pm
by dunbarx
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?
"And" requires that BOTH parts of the conditional MUST be true, not that either CAN BE true.
You owe me homework.
Craig
Re: if and or statements not working as intended
Posted: Wed Aug 26, 2020 6:54 pm
by FourthWorld
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.
Re: if and or statements not working as intended
Posted: Thu Aug 27, 2020 6:43 am
by EddieLee
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?
"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