HP 3000 Manuals

$IF Command (Conditional Compilation) [ SPL to HP C/XL Migration Guide ] MPE/iX 5.0 Documentation


SPL to HP C/XL Migration Guide

$IF Command (Conditional Compilation) 

          Table 10-3.  $IF Command 

---------------------------------------------------------------------------------------------
|                                             |                                             |
|                     SPL                     |             HP C/XL Equivalent              |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| if-command:                                 | if-directive:                               |
|                                             |                                             |
|      $IF [Xn={OFF                           |      #if constant-expression                |
|                       ON}]                  |                                             |
|                                             |      ...                                    |
|                                             |                                             |
|                                             |      [#else                                 |
|                                             |      ...]                                   |
|                                             |                                             |
|                                             |      #endif                                 |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| SPL predefines ten switches named           | The SPL switches may be emulated by         |
| X0,...,X9, whose initial values are OFF.    | defining them equal to zero in #define      |
| The switches may be changed with the $SET   | directives.                                 |
| command (see below) and tested with the $IF |                                             |
| command.                                    |      #define OFF 0                          |
|                                             |      #define ON 1                           |
|                                             |      #define X0 OFF                         |
|                                             |      ...                                    |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| When a $IF command is executed, and the     | When an #if directive is executed, and the  |
| switch test is true (or the test is         | expression is true (nonzero), then all the  |
| omitted), then all the following source     | following source lines are compiled, down   |
| lines are compiled, down to the next $IF    | to the next #else or #endif directive.  If  |
| command.  If the test is false, the same    | the test is false (zero), the same source   |
| source lines are skipped.                   | lines are skipped.                          |
|                                             |                                             |
| Note that there is no form of "else" except | The #else directive marks the start of a    |
| a $IF with the opposite test.  A $IF with   | block of lines that are compiled only if    |
| no parameter serves to end the conditional  | the test is false.  They are skipped if the |
| block.                                      | test is true.  The #else block is           |
|                                             | terminated by an #endif directive.          |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| For example, X3 is used to control a choice | Assuming appropriate initialization, as     |
| between two DEFINE declarations:            | above, the corresponding example in HP C/XL |
|                                             | could be coded as:                          |
|      $IF X3 = ON                            |                                             |
|      DEFINE GLOBALVAL = 99#;                |      #if X3 == ON                           |
|      $IF X3 = OFF                           |      #define GLOBALVAL 99                   |
|      DEFINE GLOBALVAL = 101#;               |      #else                                  |
|      $IF                                    |      #define GLOBALVAL 101                  |
|                                             |      #endif                                 |
|                                             |                                             |
---------------------------------------------------------------------------------------------
|                                             |                                             |
| In a case where two switches are used in    | This is rendered in HP C/XL as:             |
| series, you might see:                      |                                             |
|                                             |      #if X3 == ON                           |
|      $IF X3 = ON                            |      #define GLOBALVAL 99                   |
|      DEFINE GLOBALVAL = 99#;                |      #endif                                 |
|      $IF X5 = OFF                           |      #if X5 == OFF                          |
|      DEFINE GLOBALVAL = 101#;               |      #define GLOBALVAL 101                  |
|      $IF                                    |      #endif                                 |
|                                             |                                             |
---------------------------------------------------------------------------------------------

The conditional compilation facility in HP C/XL is considerably more
powerful than that available in SPL. Instead of ten fixed switches, you
can define arbitrary names as defined variables, and can test an
expression composed of these variables and constants.

The directives "#ifdef id" and "#ifndef id" are also available to test
whether or not an identifier, id, has been defined with a #define
directive.  You can use #ifdef and #ifndef in place of #if.  See the HP C 
Reference Manual for more information.



MPE/iX 5.0 Documentation