Simple logic, but, is another way Possible?

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
iwannaprogram
Posts: 5
Joined: Mon Aug 06, 2012 3:12 am

Simple logic, but, is another way Possible?

Post by iwannaprogram » Mon Aug 13, 2012 4:28 am

Hey guys, I am determining whether a variable has either 2, 3 or 4.

In this example , localValueN has "2"

Code: Select all

   local localValueN 
   put "2" into localValueN

      if localValueN = "2"
   then 
      put "is 2" into anotherVar
   else
      if localValueN = "3"
      then
         put "is 3" into anotherVar
      else
         put "is 4"into anotherVar
      end if
   end if

   answer anotherVar
Question 1:
Can anyone think of a leaner, smarter, or shorter way to write this logic?

Question 2:
Can we put more code into each line? maybe some break that LiveCode understands when code starts or ends??
like this

Code: Select all

   local localValueN ; put "2" into localValueN ; if localValueN = "2" ; then; put "is 2" into anotherVar ; else ; if localValueN = "3" ; then ; put "is 3" into anotherVar ; else ; put "is 4"into anotherVar ; end if ;end if
   answer anotherVar

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Simple logic, but, is another way Possible?

Post by shaosean » Mon Aug 13, 2012 6:53 am

You can use the semi-colon as a line break in Rev, so your second example will work, but I think it is still too wordy, so try this:

Code: Select all

local localValueN
local anotherVar

put ("is" && localValueN) into anotherVar
answer anotherVar

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Simple logic, but, is another way Possible?

Post by dave_probertGA6e24 » Mon Aug 13, 2012 7:25 am

Or you could use a Switch statement to cover multiple choices:

Code: Select all

switch localValueN
case "2"
  // do something when it is 2
  break
case "3"
  // likewise for value = 3
  break
case "10000000000"
   // 'cos it's a nice big number (which I really wish I had in my bank account!)
  break
default
  // Handle all other options that are not covered above
end switch
I personally use switch's for any multi value variable (with a simple number of results) instead of a bunch of if's - mainly because they are more readable to me.

As with most languages though - there are a few ways of achieving the results you want - dependent on the effect you are after.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Simple logic, but, is another way Possible?

Post by Klaus » Mon Aug 13, 2012 9:52 am

Hi,

this is everything but OFF-TOPIC!?
Will move the thread...

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Simple logic, but, is another way Possible?

Post by sturgis » Mon Aug 13, 2012 1:52 pm

You can also do it this way.
put any item of "2,3,4" into localValueN

Code: Select all

 if localValueN = "2" then put 2 into anotherVar -- No need for end if as long as its all on 1 line
 if localValueN = "3" then put 3 into anotherVar
 if localValueN = "2" then put 3 into anotherVar
I kinda prefer switch too though.

For something like this you could also do a simple substitution list

Code: Select all

put "1,2,3,4,5,6,7,8" into tNums -- define matching lists
put "a,b,c,d,e,f,g,h" into tLetters

put any item of tNums into n
get itemoffset(n,tNums) -- find what item number n is
put item it of tLetters into anotherVar -- anotherVar now contains the matching item from tLetters
Of course if you're going to do that, and have an exact match value to value you can put everything into an array and lookup based on the array key.

Paul.RaulersonBUSIvAg
Posts: 21
Joined: Tue Aug 07, 2012 2:00 pm

Re: Simple logic, but, is another way Possible?

Post by Paul.RaulersonBUSIvAg » Mon Aug 13, 2012 7:45 pm

I am very new and I just know I am showing my ignorance, but could you not just say something like this?

Code: Select all

if ((localValueN >1) and (localValueN < 5)) then 
put "is " &  localValueN into  anotherVar
end if
That is assuming of course, you are only interested in values of 2, 3, or 4. Might be some kind of "in" construct in the language.
Like:

Code: Select all

if localValueN is in range 2 to 4 then put "is " & localValueN into anotherVar 
I have not tested that to be sure it is legal LC syntax.
Yours,
-Paul


iwannaprogram wrote:Hey guys, I am determining whether a variable has either 2, 3 or 4.

In this example , localValueN has "2"

Code: Select all

   local localValueN 
   put "2" into localValueN

      if localValueN = "2"
   then 
      put "is 2" into anotherVar
   else
      if localValueN = "3"
      then
         put "is 3" into anotherVar
      else
         put "is 4"into anotherVar
      end if
   end if

   answer anotherVar
Question 1:
Can anyone think of a leaner, smarter, or shorter way to write this logic?

Question 2:
Can we put more code into each line? maybe some break that LiveCode understands when code starts or ends??
like this

Code: Select all

   local localValueN ; put "2" into localValueN ; if localValueN = "2" ; then; put "is 2" into anotherVar ; else ; if localValueN = "3" ; then ; put "is 3" into anotherVar ; else ; put "is 4"into anotherVar ; end if ;end if
   answer anotherVar

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Simple logic, but, is another way Possible?

Post by mwieder » Mon Aug 13, 2012 8:05 pm

Your first example is fine. For the second, try

Code: Select all

if localValueN is in "234" then put "is " & localValueN into anotherVar 
I normally prefer the switch construct as well, but there are many ways of getting a solution to any given problem.

Post Reply