# If vs Case

This topic is 7830 days old. Please don't post here. Open a new topic instead.

## Recommended Posts

I've noticed that a lot of users in this forum make frequent use of the "Case" where I probably would have used "If"

Could someone exlain the difference, and why one might be prefered over the other?

Thanks!

--

Jason Wood

HeyWoody.com

##### Share on other sites

Hi:

It might help to think of If like a logic statement. "If [ this, then that. Else this]."

As you can see, If will only give you one of two results based on if the statement is true or not. Case, on the other hand, will evaluate the entire statement and will give results on the first value that is true, not just one result or the other.

I hope that makes sense.

Ken

##### Share on other sites

I understand "If" fully from my QBasic days... maybe you could give me a syntax example for Case (I still don't quite get how it works).

--

Jason Wood

HeyWoody.com

##### Share on other sites

Ok, let me try this.

A typical case statement might look like this:

Case (test1, result1, test2, result2, test3, result3, default result)

If test1 qualifies, then you will get result1. If test2 qualifies, then you will get result2, and so on. If none qualify, you will get the default result. For example, suppose I have a field (we'll call it Num) that will be populated with numbers 1 through 4. I want a calc field (call it NumCalc) that will change the number to be spelled out ( "1" would be "one"). Sorry its a silly example, but bear with me. I would set up the NumCalc field like this:

Case (PatternCount(Num, "1"), "one" , PatternCount(Num, "2"), "two", PatternCount(Num, "3"), "three", PatternCount(Num, "4"), "four", "five").

Just think of it as an If statement with more than one test. Does that help??

Ken

##### Share on other sites

I had to lookup PatternCount() but now it all makes sense!

I had been using nested If statements to achieve these results, so I sure am glad I asked!

--

Jason Wood

HeyWoody.com

##### Share on other sites

That is the main reason for using Case statements. Nested If's can be horrible to debug. Apparently Case statements execute faster too.

##### Share on other sites

This topic is 7830 days old. Please don't post here. Open a new topic instead.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

×
×
• Create New...

## Important Information

By using this site, you agree to our Terms of Use.