Data editing > Flow control > Statements of condition: if
 
Statements of condition: if
Quick reference
To define statements to be executed if a certain condition is true, type:
if (logical_exp) statement1[; statement2; ... ]
More information
The if statement defines a statement whose execution depends upon the value of a logical expression. For example, you might say ‘If it is raining, I will take my umbrella’. The statement is ‘I will take my umbrella’ and it depends upon the logical expression ‘It is raining’. If the expression is true (that is, it is raining), the statement is executed (I take my umbrella), if it is false (no rain) it is ignored (I don’t even think about my umbrella).
In Quantum, you might have a shopping survey in which respondents have been asked to name the supermarkets in which they shop at least once a week. These responses are coded into column 21 of card 1, and you want to keep a count of the number of respondents shopping in Safeway (code 4). The sentence would say ‘If column 21 contains a 4, increment the counter by 1’.
A Quantum if statement consists of three items:
1 The word if.
2 The logical expression whose value controls the action to be taken, enclosed in parentheses.For more information about logical expressions, see Logical expressions.
3 The statement(s) to be executed if the expression is true.
To translate the sentence into the Quantum language, write:
if (c121'4') safe=safe+1
Here is another statement:
if (numb(c10,c11,c12).gt.3) emit c20'9'
The logical expression to be tested states that the number of codes in columns 10, 11 and 12 is greater than three. If it is true, and there are, say, 5 codes altogether in those columns, add a 9 into column 20 in addition to what is already there. On the other hand, if it there are 3 or fewer codes in that field, leave column 20 as it is and continue with the statement on the line immediately after the if. For example:
+----1----+----2----+
     62-       1
       0       /
               4
yields
+----1----+----2----+
     62-       1
       0       /
               4
               9
but:
+----1----+----2----+
      2-       1
       0       /
               4
yields
+----1----+----2----+
      2-       1
       0       /
               4
Once the emit statement has been executed, Quantum continues with the statement on the next line.
The statement to be executed if the expression is true can be any Quantum statement, even another if. For example:
if (c130'1'); if (c131'9') c181'19'
says ‘if c130 contains a ‘1’, and then if c131 contains a ‘9’, then put the multicode ‘19’ in c181’. This statement is not incorrect, but it can be more efficiently written as:
if (c130'1'.and.c131'9') c181'19
The if keyword can be followed by a whole series of statements as long as each one is separated by a semicolon. These statements will then be executed in the order in which they appear. For example:
if (t4.le.5) c235'45'; emit c567'2'; delete c789'0'
This says, if the value of t4 is less than or equal to 5, put the multicode ‘45’ in column 235 overwriting whatever is there already, then add a ‘2’ into column 567 and, finally, remove the ‘0’ from column 789.
Note You cannot switch missing values processing on or off with an if statement. A missingincs statement is always executed wherever it appears in the edit. This means that although the compiler will accept statements of the form:
if (....) missingincs 1
Quantum switches on missingincs for the rest of the edit or until a missingincs 0 statement is read. It does not switch on missingincs selectively for only those records that satisfy the expression defined by the if clause. For more information about missingincs, see Missing values in numeric fields.
See also
Flow control