Epi Info™ User Guide

Command Reference


Check Commands: IF THEN ELSE

Description
This command defines conditions and one or more consequences which occur when the conditions are met. An alternative consequence can be given after the ELSE statement to be realized if the first set of conditions is not true. The ELSE statement is optional.

Syntax
IF <expression> THEN
     [command(s)]
END-IF

IF <expression> THEN
     [command(s)]
ELSE
     [command(s)]
END-IF

  • The <expression> represents a condition that determines whether or not subsequent commands will be run.  If the condition evaluates to true, the commands inside of the IF block will run.  If the condition evaluates to false and an ELSE block is used, the commands inside of the ELSE block will run instead.  If no ELSE exists and the condition is false, then no commands inside of the IF block are run.
  • The (command[s]) represents at least one valid command.
  • The ELSE statement is optional and will run any code contained inside of it when the <expression> evaluates to false.

Comments
Each IF/THEN command must terminate with an END-IF.  An optional ELSE command may appear between IF/THEN and END-IF.   One or more IF/THEN statements can be nested within another IF/THEN statement to create a more complex decision tree.

Examples
Example 1: If you select “Male” for the patient’s sex then the fields named Pregnancy and ChildBirth are hidden. The example assumes a form exists with the following fields: Sex (Text), Pregnancy (Yes/No), and ChildBirth (Yes/No).  The code below would appear in the AFTER section of the Sex field.

IF Sex = "Male" THEN
     HIDE Pregnancy Childbirth 
END-IF

Example 2: If the date entered for Date of Birth (DOB) is before January 1, 1915 or after the computer’s system date (ie. a future date), then the Enter module gives a warning beep, a dialog indicating invalid input, and highlights the DOB field.  However, if the date of birth is between January 1, 1915 and the system date, then the DOB field is unhighlighted.   This example assumes a form exists with a date field named DOB.  The code below would appear in the AFTER section of the DOB field.

IF DOB < 01/01/1915 OR DOB > SYSTEMDATE THEN 
     BEEP 
     DIALOG "Warning: Invalid date of birth detected." TITLETEXT="Invalid Date"
     HIGHLIGHT DOB
ELSE 
     UNHIGHLIGHT DOB
END-IF

Example 3:  The date of birth field is validated to ensure correct input. The date of birth field must not be less than January 1, 1915, must not be greater than the current time, and must not be greater than the date of the survey. If any of these conditions is not met, a warning dialog is displayed and the invalid input erased. The example assumes a form exists with that has the following fields: DOB (Date) and SurveyDate (Date). The code below would appear in the AFTER section of either DOB or SurveyDate, depending on whichever one will be filled in last.

IF (DOB  < 01/01/1915) OR (DOB > SYSTEMDATE) OR  (DOB > SurveyDate) THEN 
     BEEP
     DIALOG "Warning: Invalid date of birth  detected" 	
     CLEAR DOB 
END-IF

Example 4: An IF command is used to check if a field has been left blank. The example assumes a form exists with the following field: LastName (Text). The code below would appear in the AFTER section of the LastName field.

IF NOT LastName = (.) THEN 
     BEEP
     DIALOG "Last name field should not be blank." 
END-IF

Example 5: Multiple IF commands are used to generate more than two possible outcomes. The example assumes a form exists with the following fields: AgeType (Text), AgeYears (Numeric), and Age (Numeric). The code below would appear in the AFTER section of AgeType or Age, depending on which one will be filled in last.

IF AgeType  = "Days" THEN 
     ASSIGN AgeYears = Age / 365.25 
END-IF  
IF AgeType = "Months" THEN
     ASSIGN AgeYears = Age / 12 
END-IF
IF AgeType = "Years" THEN
     ASSIGN AgeYears = Age 
END-IF

Example 6: The AND operator requires both Sex to be “F” and Pregnancy to be true in order for the GOTO command to be executed. The example assumes a form exists with the following fields: Sex (Text), Pregnancy (Yes/No), and ChildBirth (Yes/No). The code below would appear in the AFTER section of either Sex or Pregnancy, depending on which one is filled in last.

IF (Sex =  "F") AND (Pregnancy = (+)) THEN
     GOTO ChildBirth
END-IF

Example 7: Several IF commands are used to determine if a patient is ill. If any one of the symptoms listed in the form are true, the field ill is assigned true. The example assumes a form exists that has the following fields: Ill (Yes/No), Vomiting (Yes/No), Fever (Yes/No), and Diarrhea (Yes/No). The code below would appear in the AFTER section of the ill field.

ASSIGN Ill = (-) 
IF Vomiting = (+) THEN
     ASSIGN Ill = (+) 
END-IF
IF Diarrhea = (+) THEN
     ASSIGN Ill = (+) 
END-IF
IF Fever = (+) THEN
     ASSIGN Ill = (+) 
END-IF

Example 8: Several IF commands are used to determine the number of symptoms a patient is presenting with. If the number of symptoms is greater than or equal to two, the Case variable is assigned true. If the number of symptoms is less than two, the Case variable is assigned false. The example assumes a form exists that has the following fields: MajorSymp (Numeric), Vomiting (Yes/No), Fever (Yes/No), Diarrhea (Yes/No), and Case (Yes/No).

ASSIGN  MajorSymp = 0
IF Diarrhea = (+) THEN
     ASSIGN MajorSymp = MajorSymp + 1
END-IF
IF Fever = (+) THEN
     ASSIGN MajorSymp = MajorSymp + 1 
END-IF 
IF Vomiting = (+) THEN
     ASSIGN MajorSymp = MajorSymp + 1 
END-IF
IF MajorSymp >= 2 THEN
     ASSIGN Case = (+) 
ELSE
     ASSIGN Case = (-) 
END-IF

Example 8: The IF command can be nested to provide a more complex decision tree.  In this example, if the patient ate ham then, if the patient also ate eggs, then the meal included ham and eggs, otherwise, the meal was just ham.  The example assumes a form exists with the following fields: AteHam (Yes/No); AteEggs (Yes/No); MealIncluded (Text).

IF AteHam = (+) THEN
     IF AteEggs = (+) THEN
          ASSIGN MealIncluded = "Ham-n-Eggs"
     ELSE
          ASSIGN MealIncluded = "Just Ham"
     END-IF
     ASSIGN Case = (+) 
ELSE
     IF AteEggs = (+) THEN
          ASSIGN MealIncluded = "Just Eggs"
          ASSIGN Case = (+) 
     ELSE
          ASSIGN MealIncluded = "Neither Ham nor Eggs"
          ASSIGN Case = (-) 
     END-IF
END-IF

Tip:  Indent the command contents of each block to help ensure that each IF/THEN block is terminated by a corresponding END-IF.

Page last reviewed: September 16, 2022, 12:00 pm