#include within "if" statement

Greetings

I would to use "#include" in an "if" statement that goes like below, but I get errors messages when I try to do this.  Is something like this possible?  Sub1.src and sub2.src define/redefine similar variables and procedures.

 

If test==1

#include sub1.src;

else;

#include sub2.src;

endif;

1 Answer



0



That is a great question! The reason that your code snippet does not work is that all of the statements that start with #, such as #include , #define, #ifdef, etc are all executed at compile time--when GAUSS is turning the program from text on the page to something that the computer can run. However, the if statement is executed at run time.

Let's look at a simple example of the problems and then go over some solutions. Let's say that we have a procedure that calculates the hypotenuse of a triangle and that we have one version of the procedure that prints out some information about the inputs and another that does not. Let's further assume that one of the procedures is named norm and that the other is named hypotenuse, like this:

test = 1;

if test == 1;
    hypotenuse = norm(3, 4);
    proc (1) = norm(a, b);
        local c;
        c = a.^2 + b.^2;
        c = sqrt(c);
        retp(c);
    endp;
else;
    hyp = hypotenuse(3, 4);
    proc (1) = hypotenuse(a, b);
        local c, c_squared;
        print "a = " a;
        print "b = " b;
        c_squared = a.^2 + b.^2;
        print "c_squared = " c_squared;
        c = sqrt(c_squared);
        retp(c);
    endp;
endif;

Since we can think of the #include statement as telling GAUSS to copy and paste the contents of a file to a particular place in your code, the code snippet above is just like one that uses #include to load in the same code bits from another file.

In this code above, we are creating a variable named hypotenuse AND a procedure named hypotenuse when GAUSS is compiling the file. GAUSS will not allow you to have a variable and a procedure both named hypotenuse. (NOTE: This may or may not be the exact error that you are getting, but the principal for any error that you would get in this context is most likely the same.)
Solution
The solution is to use a compile time if statement. For this case, we will define test at compile time to a value and then test to see if test has been defined also during the compile phase.

new;
#define TEST 1

#ifdef TEST
    hypotenuse = norm(3, 4);
    proc (1) = norm(a, b);
        local c;
        c = a.^2 + b.^2;
        c = sqrt(c);
        retp(c);
    endp;
#else
    hyp = hypotenuse(3, 4);
    proc (1) = hypotenuse(a, b);
        local c, c_squared;
        print "a = " a;
        print "b = " b;
        c_squared = a.^2 + b.^2;
        print "c_squared = " c_squared;
        c = sqrt(c_squared);
        retp(c);
    endp;
#endif

Or in your case:

new;
#define TEST 1

#ifdef TEST
   #include sub1.src    
#else
   #include sub2.src   
#endif

aptech

1,773

Your Answer

1 Answer

0

That is a great question! The reason that your code snippet does not work is that all of the statements that start with #, such as #include , #define, #ifdef, etc are all executed at compile time--when GAUSS is turning the program from text on the page to something that the computer can run. However, the if statement is executed at run time.

Let's look at a simple example of the problems and then go over some solutions. Let's say that we have a procedure that calculates the hypotenuse of a triangle and that we have one version of the procedure that prints out some information about the inputs and another that does not. Let's further assume that one of the procedures is named norm and that the other is named hypotenuse, like this:

test = 1;

if test == 1;
    hypotenuse = norm(3, 4);
    proc (1) = norm(a, b);
        local c;
        c = a.^2 + b.^2;
        c = sqrt(c);
        retp(c);
    endp;
else;
    hyp = hypotenuse(3, 4);
    proc (1) = hypotenuse(a, b);
        local c, c_squared;
        print "a = " a;
        print "b = " b;
        c_squared = a.^2 + b.^2;
        print "c_squared = " c_squared;
        c = sqrt(c_squared);
        retp(c);
    endp;
endif;

Since we can think of the #include statement as telling GAUSS to copy and paste the contents of a file to a particular place in your code, the code snippet above is just like one that uses #include to load in the same code bits from another file.

In this code above, we are creating a variable named hypotenuse AND a procedure named hypotenuse when GAUSS is compiling the file. GAUSS will not allow you to have a variable and a procedure both named hypotenuse. (NOTE: This may or may not be the exact error that you are getting, but the principal for any error that you would get in this context is most likely the same.)
Solution
The solution is to use a compile time if statement. For this case, we will define test at compile time to a value and then test to see if test has been defined also during the compile phase.

new;
#define TEST 1

#ifdef TEST
    hypotenuse = norm(3, 4);
    proc (1) = norm(a, b);
        local c;
        c = a.^2 + b.^2;
        c = sqrt(c);
        retp(c);
    endp;
#else
    hyp = hypotenuse(3, 4);
    proc (1) = hypotenuse(a, b);
        local c, c_squared;
        print "a = " a;
        print "b = " b;
        c_squared = a.^2 + b.^2;
        print "c_squared = " c_squared;
        c = sqrt(c_squared);
        retp(c);
    endp;
#endif

Or in your case:

new;
#define TEST 1

#ifdef TEST
   #include sub1.src    
#else
   #include sub2.src   
#endif

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.