5 Replies Latest reply on Jan 2, 2010 12:20 PM by TKnTexas

When to use Case or If statement?

Title

When to use Case or If statement?

Post

Is there a guideline as to when a Case() statement should be used instead or in preference to an If() statement.  Is there a difference in the overhead or the speed of porforming either?

• 1. Re: When to use Case or If statement?

Case vs If

http://www.jamesng.com/filemaker/filemaker_quick_tips/index.shtml

Tip 2: Case versus IF Statements
Case statements were introduced in version 3.0 of FileMaker. They perform exactly function as an If statement, however can greatly simplify calculations. Case statements are best used in place of a nested If statement. This is best illustrated by examining two calculations which will return a number in words (in the calculated field Display), based on a numeric entry (in the field Number) Using an If statement: If(Number = 1, "One", If(Number = 2, "Two", If(Number = 3, "Three","")))
Using a Case statement
Case(Number = 1,"One", Number=2, "Two", Number=3,"Three")

FILEMAKER: Nested If Statements Vs Case Statements

If ( test; resultOne; resultTwo )

• 2. Re: When to use Case or If statement?

Thanks Davidanders - clearly the Case() statement wins hands down in terms or being easier to read as the complexity increases.

Is there a performance overhead difference though?

• 3. Re: When to use Case or If statement?

Afterwards, when I remembered your question, I created a modified script to test an unstored 5-level nested If calculation and compared this with a Case statement with 5 arguments on this 36,500 record loop. I did 5 passes for each test, so, approximately 182,500 records per test. In my results, the Case statement was approximately 4% faster.

• 4. Re: When to use Case or If statement?
Wow, thanks for the input.  That clinches it then.  I'll change some of our Ifs to Case() statements.  Shame sometimes that the technicality of the designs is lost on the Users!  Guess they should appreciate it longer term as the database increases and the speed is still there!
• 5. Re: When to use Case or If statement?
I make use of the CASE statement more often than the IF statement.  It is easier to add that third condition.