Page 1 of 1

Nested If/Else Statements

Posted: Thu Feb 14, 2013 2:23 am
by KennyR
Hi...noob question here, but I have tried this multiple ways and have no answer on how to structure my if/else statement without it verifying one of the 4 conditions and moving to the "else" statement. Here is my script and I'll explain...

Code: Select all

if vTS is not 0
   then answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
   if vWhite is not 0 
   then answer "ALS supplies missing from inventory. Please go back and complete ALS section"
   if vMisc is not 0
   then answer "Miscellaneous Supplies missing from inventory. Please go back and complete Miscellaneous section" 
   if vAirway is not 0 
   then answer "Airway supplies missing from inventory. Please go back and complete Airway section"
   else
      put "Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
      mobileComposeMail "Test E-mail", someEmail@verizon.net,,, vBody, tAttachment
      end if
end mouseUp
I want to check that all 4 conditions are true before triggering the "ELSE" statement, but what seems to happen is that as long as one condition is true, it goes to the "ELSE" statement instead of stopping. I have tried to make this into a "Switch" but I have the same issue....any ideas on what I am doing wrong??

Re: Nested If/Else Statements

Posted: Thu Feb 14, 2013 2:50 am
by magice
try this

Code: Select all

on mouseUp

if vTS is not 0
   then 
   answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
   exit mouseUp
  end if

if vWhite is not 0
   then 
   answer "ALS supplies missing from inventory. Please go back and complete ALS section"
   exit mouseUp
   end if

if vMisc is not 0
   then 
   answer "Miscellaneous Supplies missing from inventory. Please go back and complete Miscellaneous section"
   exit mouseUp
   end if
   
if vAirway is not 0
   then 
   answer "Airway supplies missing from inventory. Please go back and complete Airway section"
   exit mouseUp
   end if 

      put "Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
      mobileComposeMail "Test E-mail", someEmail@verizon.net,,, vBody, tAttachment
     
end mouseUp

Re: Nested If/Else Statements

Posted: Thu Feb 14, 2013 2:57 am
by KennyR
AHHH! Never considered the "Exit MouseUp"

Thanks for the help!

Re: Nested If/Else Statements

Posted: Thu Feb 14, 2013 3:07 am
by magice
I believe you could also do it this way and it would be closer to the way you were trying to do it.

Code: Select all

on mouseUp
   if vTS is not 0
   then 
      answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
   else
      if vWhite is not 0
      then 
         answer "ALS supplies missing from inventory. Please go back and complete ALS section"
      else
         if vMisc is not 0
         then 
            answer "Miscellaneous Supplies missing from inventory. Please go back and complete Miscellaneous section"
         else
            if vAirway is not 0
            then answer "Airway supplies missing from inventory. Please go back and complete Airway section"
            else
               put "Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
               mobileComposeMail "Test E-mail", someEmail@verizon.net,,, vBody, tAttachment
            end if
         end if
      end if
   end if
end mouseUp

Re: Nested If/Else Statements

Posted: Thu Feb 14, 2013 4:36 am
by dunbarx
I go way back, and in HC one had to do it that way.

This cries out for a switch construction. So much more compact and readable.

switch
case vts is not 0
answer ...
break
case vWhite is not 0
answer something else
break

etc.

end switch

Craig Newman

Re: Nested If/Else Statements

Posted: Thu Feb 14, 2013 9:46 pm
by jsburnett
Hi,

Why not try 'or'
(It may make for a long if statement, which can be decreased in size in the future)...

but
if a is not true or b is not true or c is not true or d is not true
then
do 1 of the above is not true
else
do all are true
end if

Also, I may be missing the point here...

John

Re: Nested If/Else Statements

Posted: Fri Feb 15, 2013 3:21 pm
by KennyR
Thanks guys for the input....I had tried to write this as a switch in a number of different ways but was getting some unusual results....I tried to break each statement, but it seemed as if it was still pushing down the cases and firing the default statement. I even tried doing the "or" and making a long statement of each but was still having issues....the "exit mouseUp" trick in the end did it for me......I feel I have gotten good at writing "if/else" and "switch" functions so I was unsure what had actually happend that I wasn't getting the results I wanted....Most of the time when I write programs like the one I'm currently doing, I can't help but ask myself if there is a simpler more elegant way to structure my code. I REALLY wish there was someone down the street from me that worked in LC and could mentor me!!! Alas, I will keep pecking on the keyboard and reading until that day comes!!!

Re: Nested If/Else Statements

Posted: Sat Feb 16, 2013 3:57 am
by Newbie4
This may not be more elegant but it is simpler and easier to understand. It also checks all of them each mouseUp.

Code: Select all

 Global vTSZero, vWhiteZero, …

on openCard
   put false into vTSZero 
   put false into vWhiteZero 
   …
end openCard

on mouseUp
   if vTS is 0 then
      put true into vTSZero
   else
      answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
   end if
   if vWhite is 0 then
      put true into vWhiteZero
   else
      answer "ALS supplies missing from inventory. Please go back and complete ALS section"
   end if

   …
   if vTSZero and vWhiteZero and … then
       put "Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
      mobileComposeMail "Test E-mail", someEmail@verizon.net,,, vBody, tAttachment
   end if
end mouseUp
This may not be the best way to do it (globals are discouraged in LiveCode) but I am new to LiveCode and this is the way I would have done it elsewhere.

Re: Nested If/Else Statements

Posted: Sat Feb 16, 2013 4:30 am
by Simon
Hi Kenny,
I'm chiming in because the switch thing bothers me:

Code: Select all

local vTS,vWhite,vMisc
on mouseUp
   put 0 into vTS
   put 0 into vWhite
   put 0 into vMisc
   switchIt
end mouseUp

on switchIt
   switch 
      case vTS <> 0
         answer "vTS <> 0"
         break
      case vWhite <> 0
         answer "vWhite <> 0"
         break
      case vMisc <> 0
         answer "vMisc <> 0"
         break
      default
         answer "Currently Stocked Items..."
   end switch
end switchIt
This works for me.
I use if-then far too often when I should be using switch. Can you tell me what are your results?

Simon

Re: Nested If/Else Statements

Posted: Sat Feb 16, 2013 6:25 am
by magice
I just love seeing the many different ways people code the same scenario. This has to be the most educational board I have ever been on.

Re: Nested If/Else Statements

Posted: Tue Feb 19, 2013 12:46 pm
by KennyR
Simon, as promised, here is what I went with in the end. I like this because it "breaks" at each condition, and doesn't run through them all and alert each section that needs completed until it passes the previous condition. I prefer to use a switch in these cases, but after trying multiple ways of doing it, this seemed to work best. Plus, as soon as I find something that works, I move on! :D No need to fix what ain't broke...Thanks everyone for all your great input...that is what I LOVE about this community....everyone likes to help each other out...in the end, we all learn a few new tricks!

Code: Select all

global vEmail,vShift,vCrew,vRig
on touchEnd pld
   mobGUIUntouch the long id of me
   if vTS is not 0
   then 
   answer "Trauma Supplies missing from inventory. Please go back and complete Trauma section"
   exit touchEnd
  end if

if vWhite is not 0
   then 
   answer "ALS supplies missing from inventory. Please go back and complete ALS section"
   exit touchEnd
   end if

if vMisc is not 0
   then 
   answer "Miscellaneous Supplies missing from inventory. Please go back and complete Miscellaneous section"
   exit touchEnd
   end if
   
if vAirway is not 0
   then 
   answer "Airway supplies missing from inventory. Please go back and complete Airway section"
   exit touchEnd
   end if 
   put fld "defaultMail" group "emailAlert" into vEmail
   
      put vCrew & cr & cr &"Currently Stocked Items" & cr & fld "iot" card "Send" & cr & cr & "Items Needing Re-stocked" & cr & fld "not" card "Send" into vBody
      mobileComposeMail vRig && vShift, vEmail,,, vBody, tAttachment