Multiple Conditionals on if statement?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

JackieBlue1970
Posts: 27
Joined: Thu Jan 16, 2020 10:28 pm

Multiple Conditionals on if statement?

Post by JackieBlue1970 » Wed Feb 12, 2020 2:43 am

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

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

Re: Multiple Conditionals on if statement?

Post by FourthWorld » Wed Feb 12, 2020 3:21 am

It is. What do you have that isn't working?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

JackieBlue1970
Posts: 27
Joined: Thu Jan 16, 2020 10:28 pm

Re: Multiple Conditionals on if statement?

Post by JackieBlue1970 » Wed Feb 12, 2020 3:55 am

FourthWorld wrote:
Wed Feb 12, 2020 3:21 am
It is. What do you have that isn't working?
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...

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

Re: Multiple Conditionals on if statement?

Post by FourthWorld » Wed Feb 12, 2020 4:12 am

You've got the concept down, I'll bet the answer is in some nuance of the syntax used.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

bogs
Posts: 4211
Joined: Sat Feb 25, 2017 10:45 pm

Re: Multiple Conditionals on if statement?

Post by bogs » Wed Feb 12, 2020 10:22 am

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 -
aPic_And_Or.png
Would you like anchovies with that?
aPic_And_Or.png (6.13 KiB) Viewed 450 times
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
The code in the "Or" button -

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
It works as expected, with or without the '()' surrounding the values, see attached demo stack.
andorlogic.livecode.zip
(844 Bytes) Downloaded 7 times
Image

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

Re: Multiple Conditionals on if statement?

Post by dunbarx » Wed Feb 12, 2020 4:12 pm

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:

Code: Select all

if (a = 3 or b = 4) and c = 5 and d = 6 and (e = 7 or f = 8) then...
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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5227
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Multiple Conditionals on if statement?

Post by jacque » Wed Feb 12, 2020 5:34 pm

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.

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

JackieBlue1970
Posts: 27
Joined: Thu Jan 16, 2020 10:28 pm

Re: Multiple Conditionals on if statement?

Post by JackieBlue1970 » Wed Feb 12, 2020 9:14 pm

Thanks guy (and gals). Pretty sure it was something else.

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

Re: Multiple Conditionals on if statement?

Post by dunbarx » Wed Feb 12, 2020 9:19 pm

What Jacque said.
a field with a single carriage return {or more) may look empty but it's not
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.

Craig

JackieBlue1970
Posts: 27
Joined: Thu Jan 16, 2020 10:28 pm

Re: Multiple Conditionals on if statement?

Post by JackieBlue1970 » Wed Feb 12, 2020 9:21 pm

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.

bogs
Posts: 4211
Joined: Sat Feb 25, 2017 10:45 pm

Re: Multiple Conditionals on if statement?

Post by bogs » Wed Feb 12, 2020 9:33 pm

JackieBlue1970 wrote:
Wed Feb 12, 2020 9:21 pm
If field "Fld1" is empty and field "Fld2" is empty then

instead of if "Fld1" is empty...
I don't think that is the issue, JackieBlue1970 :|

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.
aPic_field1-2.png
Put me in coach, I'm ready to play...today...
Image

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

Re: Multiple Conditionals on if statement?

Post by dunbarx » Wed Feb 12, 2020 9:35 pm

Hi.

No.

The correct syntax would be:

Code: Select all

 if fld "Field 1" is empty
You have to have a control reference, apart from however you, er, reference that control.

What you describe would throw an error.

Craig

bogs
Posts: 4211
Joined: Sat Feb 25, 2017 10:45 pm

Re: Multiple Conditionals on if statement?

Post by bogs » Wed Feb 12, 2020 9:46 pm

dunbarx wrote:
Wed Feb 12, 2020 9:35 pm
What you describe would throw an error.
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
would work, and I asked is quoting the name a requirement or just a suggestion, because I found that to be confusing.

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.
Image

Klaus
Posts: 11495
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Multiple Conditionals on if statement?

Post by Klaus » Wed Feb 12, 2020 10:03 pm

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! :-D
3. this will not work with names consisting of two or more words. 8)

bogs
Posts: 4211
Joined: Sat Feb 25, 2017 10:45 pm

Re: Multiple Conditionals on if statement?

Post by bogs » Wed Feb 12, 2020 10:13 pm

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 :wink:
Image

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”