Use the IF block to do conditional processing in a Net.Data macro. The IF block is similar to IF statements in most high-level languages because it provides the ability to test one or more conditions, and then to perform a block of statements based on the outcome of the condition test.
You can specify IF blocks almost anywhere in a macro and can nest them. The syntax of an IF block is shown in the language constructs chapter in Net.Data Reference.
The rules for IF block syntax are determined by the block's position in the macro file. The elements allowed in the executable block of statements of an IF block depend on the location of the IF block itself. Any element that is valid in the block containing the IF block is valid within that IF block. For example, if you specify an IF block inside an HTML block, any element that is allowed in the HTML block is allowed in the IF block, such as INCLUDE statements and WHILE blocks.
%HTML block
...
%IF block
...
%INCLUDE
...
%WHILE
Similarly, if you specify the IF block outside of any other block in the declaration part of the Net.Data macro, only those elements allowed outside of any other block (such as a DEFINE block or FUNCTION block) are allowed in the IF block.
%IF ... %DEFINE ... %FUNCTION
When an IF block is nested within an IF block that is outside of any other block in the declaration part, it can use any element that the outside block can use. When an IF block is nested within another block that is in an IF block, it takes on the syntax rules for the block it is inside.
For example, a nested IF block must follow the rules used when it is inside an HTML block.
%IF
...
%HTML block
...
%IF block
Exception: Do not specify a ROW block in an IF block when the IF block is inside a REPORT block.
Net.Data processes the IF block condition list in one of two ways based on the contents of the terms making up the conditions. The default action is to treat all terms as strings, and to perform string comparisons as specified in the conditions. However, if the following two conditions are met, Net.Data performs a numeric comparison:
Examples of valid strings:
+1234567890 -47 000812 92000
Examples of invalid strings:
- 20 (contains blank characters) 234,000 (contains a comma) 57.987 (contains a decimal point)
Net.Data evaluates the IF block at the time it executes the block, which can be different than the time it is originally read by Net.Data. For example, if you specify an IF block in a REPORT block, Net.Data does not evaluate the condition list associated with the IF block when it reads the FUNCTION block definition containing the REPORT block, but rather when it calls the function and executes it. This is true for both the condition list part of the IF block and the block of statements to be executed.
Example: A macro file containing IF blocks inside other blocks
%{ This macro is called from another macro, passing the operating system
and version variables in the form data.
%}
%IF (platform == "AS400")
%IF (version == "V3R2")
%INCLUDE "as400v3r2_def.hti"
%ELIF (version == "V3R7")
%INCLUDE "as400v3r7_def.hti"
%ELIF (version == "V4R1")
%INCLUDE "as400v4r1_def.hti"
%ENDIF
%ELSE
%INCLUDE "default_def.hti"
%ENDIF
%MACRO_FUNCTION numericCompare(IN term1, term2, OUT result) {
%IF (term1 < term2)
@dtw_assign(result, "-1")
%ELIF (term1 > term2)
@dtw_assign(result, "1")
%ELSE
@dtw_assign(result, "0")
%ENDIF
%}
%HTML(report){
%WHILE (a < "10") {
outer while loop #$(a)<BR>
%IF (@dtw_rdivrem(a,"2") == "0")
this is an even number loop<BR>
%ENDIF
@DTW_ADD(a, "1", a)
%}
%}
Restriction: Net.Data does not support numerical comparision of non-integer numbers.