Print Page | Close Window

and logical operator

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Report Design
Forum Discription: The best way to design a report and problems you have encountered
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=18190
Printed Date: 04 May 2024 at 1:00am


Topic: and logical operator
Posted By: koo9
Subject: and logical operator
Date Posted: 06 Dec 2012 at 4:44am

hi all,

 
when using the AND operator to evaluate two conditions, does CR do the short circuit evaluation? aka if the first expression is false, the second condition won't be evaluated. if that's the case we could write expression like
 
if not isnull(somestringfield) and somestringfield <> "" then
 
instead of using two if statements.
 
Thanks
 
Kevin



Replies:
Posted By: DBlank
Date Posted: 06 Dec 2012 at 5:04am
I believe it stops evaluating as soon as it hits something that makes it false. So in general you want to use more simple/efficient condition first.
however, null data and how you have it configured to handle nulls will partially imapcts the behavior.
 
If you do not explicitly write in the null handler as the first condition or use the formual editor option to 'use default values for nulls' then the crystal will stop evaluting the fomrula as soon as it hits a null.
 


Posted By: DBlank
Date Posted: 06 Dec 2012 at 5:55am
to further try and explain the NULL issue in crystal here is an example,
(this example assumes you tha you are not using default values for nulls)
 
isnull(fielda) or fielda=""
 
will not evaluate the same
 
fielda="" or isnull(fielda)
 
the first statement will return "True" for nulls or empty strings and "False" for all other instances.
The second statement will return "True" for empty strings, false for non empty strings and nothing for nulls.
 
the second statement is evaluating fielda for an empty sting first, since we did not tell it how to handle the a null, it just stops evaluating and never gets to the second condition and does not return any value at all.
 
You can place these two formula fields side by side on your report to see how they act differently.
 
If you use the formula editor option setting 'Default values for null' it tells the formual how to handle the null and allows it to continue to check all of the conditions in the formula.


Posted By: koo9
Date Posted: 06 Dec 2012 at 6:24am
thanks for the detailed explaination. that's why I am using the statement as this one
not isnull(fielda) and fielda<>"",
to make sure the string is not null first then check if the string is empty.



Print Page | Close Window