9 Replies Latest reply on Aug 9, 2010 1:40 PM by raybaudi

    Calculations Q

    jjfcpa_1

      Title

      Calculations Q

      Post

      I have scoured the help file and a couple of books and can't find the answer.

      What control structures can be used in a calculation.

      I know that you can use a Case statement, but what about If - elseif - endif and loop - exit loop.  

      Also, is there no for - next control structure?

        • 1. Re: Calculations Q
          LaRetta_1

          Please provide a specific example.  Do you want a calculation field or a script which runs depending upon the results of a particular calculation test?  Loop/End Loop only apply to scripts. 

          is there no for - next control structure


          Not sure what you mean.  Are you looking for a loop test, something like this for a script:

          Go To Record/Request/Page [ first ]

          Loop

          If [ YourField = "ABC" ]

          ... do this action  Set Field [ FieldB ; "Exclude" ]

          End If

          Go To Record/Request/Page [ Next ; Exit After Last ]

          End Loop

          ... this will sequence through the found record set (starting with the first) and apply the test, if the field has ABC, place Exclude in Field B.  Then go to the next record and repeat the test and action.  When you hit the last record, exit the loop.  Once the loop ends, there are no more script steps so exit the script.

          • 2. Re: Calculations Q
            jjfcpa_1

            What I'm looking for is more logic in the calculation rather than building it into the script step.  This allows the developer to create control logic in one place (the calculation) instead of putting the control logic in the script step and call a calculation to do the actual calc.

            It is documented that you can use a Case statement in the calculation engine, and you can also us an IF() statement.  It would be nice if you could use an IF-ELSEIF-ENDIF (which is essentially a Case) and have some sort of loop control structure.  I have actually gotten an IF - ENDIF to work, but as soon as I add an ELSIF, it goes wacky.

            • 3. Re: Calculations Q
              LaRetta_1

              You aren't making sense to me.  You do NOT want to create a calculation in field definitions and then use the calculation within a script.  Why would you want to have a needless calc when the test can be within script.

              And without an example of what you mean by IF-ELSEIF-ENDIF (which is essentially a Case) and have some sort of loop control structure we surely can't suggest a logic to help you.  If you want recursive calculations, you need to use custom functions.  But until you give me some logical example, I can't properly advise.

              • 4. Re: Calculations Q
                philmodjunk

                "IF-ELSEIF-ENDIF (which is essentially a Case)"

                Why would you want to use If-ElseIF.... instead of Case?

                You can do that by nesting If functions inside of If functions, but a Case function results in a much cleaner and eaiser to work with expression that does excactly the same thing. (The Case function in filemaker branches on the first boolean expression that is true unlike some implemnations of case in other systems where it evaluates a list of discrete values--which might possibly explain why you've asked this question.)

                • 5. Re: Calculations Q
                  jjfcpa_1

                  Looping structures inside a calculation (almost) eliminates the need to do recursive calls.

                  A branching control structure allows to design more complicated calculations.

                  Imagine you are calculating a payment schedule for an amortization schedule.  Currently, the only way to do this is in a script (where you can place the loop) and then call the actual calculation of the payment date, interest, principal, and remaining principal balance.

                  • 6. Re: Calculations Q
                    philmodjunk

                    That doesn't answer the question I asked. A Case function IS a branching statment and it allows for a very complicated calculation expression to be laid out in a clear format.

                    To answer your other question. You cannot do any looping within a filemaker calculation expression except for the recursion that LaRetta has already described. Such looping would need to be done either with recursion or in a script.

                    • 7. Re: Calculations Q
                      raybaudi

                      "Looping structures inside a calculation (almost) eliminates the need to do recursive calls."

                      There is a way to have a loop inside a calculation without the need of a script, they call it: Custom Function.

                      • 8. Re: Calculations Q
                        LaRetta_1

                        I mentioned that Daniele.

                        • 9. Re: Calculations Q
                          raybaudi

                          Oh, I see now... yes, you did and I didn't see.