Page 1 of 2
Nested "if" statements
Posted: Wed May 29, 2013 11:44 am
by ninjabunny14
Hi guys,
I have never in my LiveCode history used if before but the current project I am working on requires a nested "if" statement. Now I understand what one is but have no clue how to go about doing this.
I am attempting to convert an excel spreadsheet into an app. Here is the excel code:
Code: Select all
=IF(G4="Private", IF(G6>2000000.01,7%,IF(G6>1000000.01,5%,IF(G6>=500000.01,4%,IF(G6>250000.01,3%,1%)))),15%)
Help with putting this into LiveCode?!

Thx.
Re: Nested "if" statements
Posted: Wed May 29, 2013 12:13 pm
by Klaus
Hi,
sorry, no idea of EXCEL so what is the THEN case in your example?
I avoid nested IF THEN clauses at all, if possible, and use a SWITCH structure,
which is much more readable than nested IF stuff!
Best
Klaus
Re: Nested "if" statements
Posted: Wed May 29, 2013 12:24 pm
by ninjabunny14
Ok thanks for the feedback but what is a switch structure?

i know nothing....
Re: Nested "if" statements
Posted: Wed May 29, 2013 12:27 pm
by Klaus
Check the dictionary!
It is better than you think
But what does your EXCEL script (snippet) do?
So I can show you an example of switch.
Re: Nested "if" statements
Posted: Wed May 29, 2013 12:33 pm
by Klaus
Just saw your other posting:
I have a list of words into field fList:
cat,
cat
,cat
!cat
cat!
cat$
cat%
If any of these words contain at the end "," or "!" or "$" doSomething
Code: Select all
...
repeat for each word tWord in tList
## With IF THEN:
if char -1 of tWord = "!" OR char -1 of tWord = "," OR char -1 of tWord = "$" then
do something
end if
## With SWITCH, more redable, especially when it comes to even more "comparisons"!
switch char -1 of tWord
case "!"
case "%"
case ","
do something
break
end switch
end repeat
Best
Klaus
Re: Nested "if" statements
Posted: Wed May 29, 2013 12:45 pm
by Klaus
I deleted the double post from "Converting to Livecode"!
Please only ONE posting per question, thanks!
Re: Nested "if" statements
Posted: Wed May 29, 2013 1:16 pm
by SparkOut
Hi Klausimausi, I don't think that was the same question (see my offering about "is among" for that solution on the other thread").
This one about Excel may be helped by understanding the nested if/then structures in the spreadsheet formula
Code: Select all
=IF(G4="Private", IF(G6>2000000.01,7%,IF(G6>1000000.01,5%,IF(G6>=500000.01,4%,IF(G6>250000.01,3%,1%)))),15%)
In Excel the condition is the first item in a comma separated list of 3 items, the second is the expression to evaluate if true, the third is the expression if false. In the second item you can nest another if function
This looks to me like it would translate to a sort of function that returns either 7%, 5%, 4%, 3%, 1% or 15% depending on the value of G4 and G6. Maybe the returned values would be literal strings to display, ie "7%" or % values to multiply, eg 0.07 - I shall pretend to use literals in this "fake" function
somthing like:
Code: Select all
on mouseUp
-- make some values, picked from an arbitrary definition as fields on the card
-- how you provide values to the function is up to you
put field "fldG4" into tG4
put field "fldG6" into tG6
put myFunction (tG4, tG6) into field "fldResult"
put mySwitchFunction (tG4, tG6) into field "fldSwitchResult"
-- both functions, using either nested if or switch structures should produce the same result
end mouseUp
function myFunction pG4, pG6
if pG4 = "Private" then
if pG6 > 2000000.01 then
return "7%"
else if pG6 > 1000000.01 then
return "5%"
else if pG6 > 500000.01 then
return "4%"
else if pG6 > 250000.01 then
return "3%"
else
return "1%"
end if
else
return "15%"
end if
end myFunction
function mySwitchFunction pG4, pG6
local tReturnVal
switch
case pG4 = "Private"
switch
case pG6 > 2000000.01
put "7%" into tReturnVal
break
case pG6 > 1000000.01
put "5%" into tReturnVal
break
case pG6 > 500000.01
put "4%" into tReturnVal
break
case pG6 > 250000.01
put "3%" into tReturnVal
break
default
put "1%" into tReturnVal
end switch
break
default
put "15%" into tReturnVal
end switch
return tReturnVal
end mySwitchFunction
Re: Nested "if" statements
Posted: Wed May 29, 2013 1:34 pm
by Klaus
SparkOut wrote:Hi Klausimausi, I don't think that was the same question (see my offering about "is among" for that solution on the other thread").
It WAS the same posting and there were no answers yet

Re: Nested "if" statements
Posted: Wed May 29, 2013 2:45 pm
by ninjabunny14
Thanks for your help guys will try all of these out. Sorry for double posting I thought I had posted this in the wrong place.
Thanks all of you

Re: Nested "if" statements
Posted: Wed May 29, 2013 3:11 pm
by ninjabunny14
OK I'm getting there.... albeit a little slowly
When I have entered this code into the necessary card it would probably run ok but I need to modiy it to the stack; as I have not called all my fields/buttons G4/G6 etc.
Couple of questions therefore :-
What does pG6 and pG4 do?
And if I replaced G6 with field "Price Input" and G4 with button "Purchase type" would my code look like this somewhat (not using the switch function):
Code: Select all
put btn "Purchase type" into tG4
put field "Price Input" into tG6
if btn "Purchase type" = "Private" then
if fld "Price Input" > 2000000.01 then
return "7%"
else if fld "Price Input" > 1000000.01 then
return "5%"
else if fld "Price Input" > 500000.01 then
return "4%"
else if fld "Price Input" > 250000.01 then
return "3%"
else
return "1%"
end if
else
return "15%"
end if
put WhatdoIputhere? (tG4, tG6) into field "UO %age"
Also what do I need to put where in the code it says: WhatdoIputhere?
Thanks for the support it's my first day on the forums and I'm loving all the support. Thanks to all!

Re: Nested "if" statements
Posted: Wed May 29, 2013 7:06 pm
by SparkOut
You can't put a button into a variable and have it do anything meaningful in this situation. You would have to determine what value you wanted according to the button state, or some other condition you determine. I imagine you are more likely to want to have a button to press which performs the calculation according to the values in fields?
Let's say you have a field "Price Input" and a field "Purchase Type".
If you type in the value 565000 into field "Price Input" and type in the value "Private" into field "Purchase Type" and have another empty field "UO %age" then you could add a calculate button.
In the script of the button you would type (or copy/paste) code such as:
Code: Select all
on mouseUp
-- get the values from the fields and put into variables
-- simply to make it easier to pass them to the function
put field "Purchase Type" into tG4
put field "Price Input" into tG6
-- pass those values to the function and take the returned value from the function
-- and store it in the destination field
put calculatePercentage (tG4, tG6) into field "UO %age"
-- this sends the values in variables tG4 and tG6 to the calculatePercentage function
-- it amounts to the same as hardcoding
-- put calculatePercentage ("Private", 565000) into field "UO %age"
-- given the values mentioned
end mouseUp
function calculatePercentage pG4, pG6
-- when you invoked the function you passed the variables tG4 and tG6 to be used
-- as parameter values to process
-- these are referred to within the scope of this function as pG4 and pG6 because
-- that was what we defined these parameter placeholder names as in the declaration
-- of the function right here
-- so given that we passed the variable tG4 containing the value "Private" to the function
-- then within the scope of this function the parameter pG4 will contain the value "Private"
-- (unless you change its value within the function, of course - but this one doesn't)
-- so now all the function has to do is to determine which value to return
-- with a few tests of the two conditions, we will return one of the values back to the
-- invoking command in the mouseUp handler
-- back when we said
-- put [the value returned from the function] calculatePercentage ("Private", 565000) into field "UO %age"
-- [the value returned from the function] is of course just here for implicit clarity
-- it's not part of the syntax
-- check if the pG4 parameter was "Private" or not
if pG4 = "Private" then
-- yes it is, so do our other checks on pG6 and return a value accordingly
if pG6 > 2000000.01 then
return "7%"
else if pG6 > 1000000.01 then
return "5%"
else if pG6 > 500000.01 then
return "4%"
else if pG6 > 250000.01 then
return "3%"
else
return "1%"
end if
else
-- no pG4 was not "Private" so return the appropriate value
return "15%"
end if
end calculatePercentage
I hope that makes it a bit clearer? In your question, WhatdoIputhere? would be the call to the function name you defined, in this case "calculatePercentage". You needn't call a function (although it's often more flexible and maintainable if you do) but if you don't then your if..then actions wouldn't return a value, they'd just put the value into the destination field. HTH
Re: Nested "if" statements
Posted: Thu May 30, 2013 9:17 am
by ninjabunny14
Thank you for all of this SparkOut. The reason Purchase type is a button is because it is a drop down menu which you select your purchase type from before pressing the button to calculate. This means the percentage is affected by the 3 possible out comes of the button (select, Private, Company). Make sense?
Sorry if i made this unclear but am amazed on my 2nd day on the forums the amount of support you can get. Thank you!

Re: Nested "if" statements
Posted: Thu May 30, 2013 1:14 pm
by SparkOut
With a drop down (ie combo box or option menu) button you can find out the currently selected item in the menu by testing the menuHistory property, which returns the line number of the last selected item in the menu. In the script of the calculate button you could:
Code: Select all
-- say the menu choices are "Private" and "Corporate" and "Other" (in that order)
-- and the latest selected choice is "Private"
-- we get the line number
put the menuHistory of button "Purchase Type" into tPTline
-- tPTline should now contain 1
-- we can then get that line of text for our purposes
-- the text of button "Purchase Type" is the list of all the menu choices
-- now that we know which line of the text it is we want, we can
put line tPTline of the text of button "Purchase Type" into tG4
Of course, when you make your choice on the original combo box or option menu button, it will trigger its own "menuPick" message so you could use that to put the current selected choice into a field "Purchase Type" which you may find simpler. Or you could set a custom property which is just as simple (perhaps not so obvious, but still). Instead of the above, in the script of the menu button
Code: Select all
on menuPick pItemName
-- put the current choice into a visible field
put pItemName into field "Purchase Type"
-- or set a custom property
set the currentChoice of me to pItemName
end menuPick
Then in the script of the calculate button you can ignore the method of getting the right value for tG4 using menuHistory, and either stick with reading the value from the field, or
Code: Select all
put the currentChoice of button "Purchase Type" into tG4
There are lots of ways to do almost anything in LiveCode. Sometimes this can be confusing, but it is a great flexibility when you have more experience. Try checking all the tutorials you can to get more used to the basics, it will greatly help in the future.
PS, in all of this the variable names tG4 etc are only chosen because of the original reference to the cell on the Excel sheet you were interested in, just to make it clearer what we were dealing with. There's nothing to stop you using more specific variable names (eg tPurchaseType) if you wished.
Re: Nested "if" statements
Posted: Thu May 30, 2013 5:48 pm
by ninjabunny14
Thanks once again for the help will be on my way now hopefully!

Re: Nested "if" statements
Posted: Thu May 30, 2013 6:07 pm
by ninjabunny14
Never mind my earlier post: this seems to go on forever!

So...
Here is my final code:
Code: Select all
put the currentChoice of button "Purchase Type" into tG4
put field "Price Input" into tG6
if btn "Purchase type" = "Private" then
if fld "Price Input" > 2000000.01 then
return "7%"
else if fld "Price Input" > 1000000.01 then
return "5%"
else if fld "Price Input" > 500000.01 then
return "4%"
else if fld "Price Input" > 250000.01 then
return "3%"
else
return "1%"
end if
else
return "15%"
end if
put (tG4, tG6) into field "UO %age"
In the end it returns nothing into field "UO %age"
I decided to keep it all as one function so I could understand it better so is there any way I can change the script to enter the percentage into UO %age it is quite vital for the rest of the function.
I'm so confused I don't know if I am confused LOL
