Skip to content

Using the IF Statement for Calculations in Laboratory Informatics Programming

March 7, 2012

As I occasionally do, I saw a number of search phrases that brought people to this blog and decided to make a post based on them. Lately, the most popular terms seem to be consultants trying to figure out if they can get out of their contracts and customers wondering if their consultants are going to try to get out of their contracts. Wow! Lots of unhappy situations, I guess! But since I’ve blogged several times on that topic, recently, instead, I’m going to pick this one “using the if statement for calculations in lims programming” which applies regardless the product because an IF statement is an IF statement.

What is an IF Statement?
An IF statement is part of what we would call a “conditional” statement. That means, it only happens based on some particular condition. To check this condition, it uses what we call “boolean logic” and a “boolean” has a value of TRUE or FALSE. Thus, if the condition is TRUE, the condition happens. If it’s FALSE, it doesn’t happen.

An Example
Suppose I have two numbers, X and Y. Also, suppose that if X is the largest that I want to report X, if Y is the largest, I want to report Y, and if they are equal, I want to put out text that states this. I could write that IF statement like this:

NOTE: I have problems keeping the indentation in this example so I have decided to use apostrophes to represent the spaces — in a real IF statement, there would not be these apostrophes.

IF ( X > Y ) THEN
”’RETURN X
ELSEIF ( X < Y ) THEN
”’RETURN Y
ELSE
”’RETURN “They Are Equal”
ENDIF

Comments on Example
This example might be used in code in all types of places in a laboratory informatics product, where a LIMS, an ELN or something else. This might be a calculation or some other piece of code. Notice that it mixes the data types. So, X and Y are numbers and in the first two blocks, the value returned is a number (X or Y) but in the last block, it is text (“They Are “Equal”). This works in many of the systems where we are allowed to mix data types in our programming. In these types of systems, the data types are converted for us in many (but not all) cases (in fact, the problem with learning these types of programming is to learn when you have to convert since you begin with the assumption that you never have to convert).

There will be systems where this might not work. If your system requires you to stay within your data type, you do not have a choice or returning either numbers or text – it must be one or the other. However, another things to consider is this – when you are calculating, many systems make the value returned a Text (or Alphanumeric or Varchar) type so you can return a number as text or anything that you convert to text.

Gloria Metrick
GeoMetrick Enterprises
http://www.GeoMetrick.com/

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: