Nested "if" statements

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

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Nested "if" statements

Post by SparkOut » Thu May 30, 2013 8:09 pm

One thing you really do need to do is be careful with the difference between a function and a handler. Handlers invoke scripts when a message generated by an event in the application matches the defined handler in an appropriate place in the message path. When you click a button, for instance (among other messages) there will be a "mouseUp" message generated. In the script of the button you clicked, you define a "mouseUp" handler. When the mouseUp message is generated, the first place it comes across a mouseUp handler is in the button script, so it executes that code.
A function is a module that you define in any appropriate place - you can keep it in the same button script, or on the card or stack to make it available to a wider range of handlers. A function will take data that a handler passes to it, do some processing and return a value to the handler that called it.

Let's take your code line by line and see where you need to get more understanding.

Code: Select all

  put the currentChoice of button "Purchase Type" into tG4
  -- here you are reading a custom property stored against the button "Purchase Type"
  -- and putting it into a variable.
  -- If you have never assigned the custom property value (called "currentChoice") then
  -- your variable will be empty


   put field "Price Input" into tG6
   -- here you read the value in the field and put it into the variable tG6


   if btn "Purchase type"  = "Private" then
   -- this does nothing useful , because btn "Purchase type" will never be equal to "Private"
   -- as mentioned, one line number of the text of the button will be stored in the
   -- menuHistory property of the button. Alternatively you could add a handler in the
   -- button "Purchase type" script to store the whole line of text selected in the menu
   -- choice, either by copying it to a field, or saving it as the custom property (see above)
   -- you really need to be testing:
   -- if tG4 = "Private"
   -- but you first have to get something meaningful into tG4 (or whatever appropriate variable)

   -- here your tests are fine, but there would be no point in having 
   -- put the value of field "Price Input" into variable tG6 if you are going to 
   -- type the whole lot out each time ;-)

      if fld "Price Input" > 2000000.01 then
         -- however, returning a value is no good here
         -- where are you returning it?
         return "7%"
         -- what you are implying here (and on all the lines below) is
         -- put "7%" into field "UO %age"
      else if fld "Price Input" > 1000000.01 then
         return "5%"
         -- put "5%" into field "UO %age"
      else if fld "Price Input" > 500000.01 then
         return "4%"
         -- put "4%" into field "UO %age"
      else if fld "Price Input" > 250000.01 then
         return "3%"
         -- put "3%" into field "UO %age"
      else
         return "1%"
         -- put "1%" into field "UO %age"
      end if
   else
      return "15%"
      -- put "15%" into field "UO %age"
   end if

   -- this does nothing as it tries to interpret two variables but comma separated and 
   -- not as a string literal
   put  (tG4, tG6) into field "UO %age"
   -- the idea implied here is to call a function which would be a way to avoid having the repetitive put "x%" into field "UO %age" in all those lines above. Send off tG4 and tG6 to the function, have it determine what the outcome should be and return that value to us to put into the field.
I'll try and make a little sample stack that shows this, as I think you really need to get an understanding of handlers and functions in order to make things work.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Nested "if" statements

Post by SparkOut » Thu May 30, 2013 8:42 pm

Here's a zipped sample stack. Unzip it and try. I hope it helps
Attachments
ninjabunny14 sampler.zip
sample stacked - unzip first
(2.21 KiB) Downloaded 167 times

Post Reply