Multiple Conditionals on if statement?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 64
- Joined: Thu Jan 16, 2020 10:28 pm
- Location: Max Meadows, VA USA
Multiple Conditionals on if statement?
Is it possible to have multiple conditions on an if statement? Such as
If x=3 and y=5 then...
I’ve tried a couple of ways and it doesn’t appear to work. Thanks
If x=3 and y=5 then...
I’ve tried a couple of ways and it doesn’t appear to work. Thanks
-
- VIP Livecode Opensource Backer
- Posts: 9836
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Multiple Conditionals on if statement?
It is. What do you have that isn't working?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Posts: 64
- Joined: Thu Jan 16, 2020 10:28 pm
- Location: Max Meadows, VA USA
Re: Multiple Conditionals on if statement?
Thanks for your response. Don’t have the code here but I was checking if two fields were empty. Something like:
If field1 is empty and field2 is empty then...
-
- VIP Livecode Opensource Backer
- Posts: 9836
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Multiple Conditionals on if statement?
You've got the concept down, I'll bet the answer is in some nuance of the syntax used.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Multiple Conditionals on if statement?
I agree with Richard, I used the syntax you provided for this demo and it works as expected.
The setup is simply 3 fields and 2 buttons - The code in the "And" button -
The code in the "Or" button -
It works as expected, with or without the '()' surrounding the values, see attached demo stack.
The setup is simply 3 fields and 2 buttons - The code in the "And" button -
Code: Select all
on mouseUp
if (field 1 is empty) and (field 2 is empty) then
put "True" into field 3
else
put "False" into field 3
end if
end mouseUp
Code: Select all
on mouseUp
if (field 1 is empty) or (field 2 is empty) then
put "True" into field 3
else
put "False" into field 3
end if
end mouseUp
-
- VIP Livecode Opensource Backer
- Posts: 9662
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Multiple Conditionals on if statement?
What everyone said. There is no limit to the number of snippets within such a construction.
But beware how you form mixed "and" and "or" constructions. This has nothing to do particularly with LC, but you must enclose the "or" parts in a mixed statement:
You may not get an error if this sort of thing is not built correctly, but you will not get the results you expected.
Craig
But beware how you form mixed "and" and "or" constructions. This has nothing to do particularly with LC, but you must enclose the "or" parts in a mixed statement:
Code: Select all
if (a = 3 or b = 4) and c = 5 and d = 6 and (e = 7 or f = 8) then...
Craig
-
- VIP Livecode Opensource Backer
- Posts: 7235
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Multiple Conditionals on if statement?
Also, a field with a single carriage return {or more) may look empty but it's not. If you aren't getting a syntax error but the result is not what you expect, that may be the reason.
To get around this I sometimes check for the number of words in the field instead of empty.
To get around this I sometimes check for the number of words in the field instead of empty.
Code: Select all
if {the number of words in fld 1 is 0} and {the number of words in fld 2 is 0} then...
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- Posts: 64
- Joined: Thu Jan 16, 2020 10:28 pm
- Location: Max Meadows, VA USA
Re: Multiple Conditionals on if statement?
Thanks guy (and gals). Pretty sure it was something else.
-
- VIP Livecode Opensource Backer
- Posts: 9662
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Multiple Conditionals on if statement?
What Jacque said.
Craig
I have been burned by this so often in the past that I always make sure it does not happen. Lots of ways to, say, disable the return key, or insert a little massaging of input data to preclude an empty first line.a field with a single carriage return {or more) may look empty but it's not
Craig
-
- Posts: 64
- Joined: Thu Jan 16, 2020 10:28 pm
- Location: Max Meadows, VA USA
Re: Multiple Conditionals on if statement?
I figured out the issue. I was using
If field "Fld1" is empty and field "Fld2" is empty then
instead of if "Fld1" is empty...
Field appears to be a key word of some sort. Not throwing an error but wouldn't work properly.
If field "Fld1" is empty and field "Fld2" is empty then
instead of if "Fld1" is empty...
Field appears to be a key word of some sort. Not throwing an error but wouldn't work properly.
Re: Multiple Conditionals on if statement?
I don't think that is the issue, JackieBlue1970JackieBlue1970 wrote: ↑Wed Feb 12, 2020 9:21 pmIf field "Fld1" is empty and field "Fld2" is empty then
instead of if "Fld1" is empty...
Field is indeed a keyword, it identifies the object you are testing, in this case field "fld1" and field "fld2". I modified the stack I posted above to include the wording because that is exactly how you should be referring to controls/objects, and it works here after I named my fields "fld1" and "fld2".
If it wouldn't be asking too much, could you possibly post the code you had before the changes you made? I, and I'm sure many others, would love to take a look at what was going on that was causing the original problem.
-
- VIP Livecode Opensource Backer
- Posts: 9662
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Multiple Conditionals on if statement?
Hi.
No.
The correct syntax would be:
You have to have a control reference, apart from however you, er, reference that control.
What you describe would throw an error.
Craig
No.
The correct syntax would be:
Code: Select all
if fld "Field 1" is empty
What you describe would throw an error.
Craig
Re: Multiple Conditionals on if statement?
Not necessarily so much, although it should. Jacque and I had a long bit of a thread about just such matters when I first got here, because I couldn't understand how I could reference a control either with the name in quotes or not.
Code: Select all
button cmdQuotes
Of course, that may have been corrected to be enforced now, but Jacque did say it was recommended to always quote the name going forward, and I agree that it is the safest course.
Re: Multiple Conditionals on if statement?
If you don't put the name in quotes, then the engine searches the whole RAM
(script, local and global vars) for a variable with that name.
This means:
1. it will takes more time, OK, a tiny bit maybe, but you get the picture.
2. If you really have a variable with that name somewhere, you are licked!
3. this will not work with names consisting of two or more words.
(script, local and global vars) for a variable with that name.
This means:
1. it will takes more time, OK, a tiny bit maybe, but you get the picture.
2. If you really have a variable with that name somewhere, you are licked!
3. this will not work with names consisting of two or more words.
Re: Multiple Conditionals on if statement?
All of which are very good reasons to always quote the name, now we need to find out what is happening in poor JackieBlue1970's case