[Tutorial] Directives #if, #else, #elseif, #endif and #error
#1

Pawn-Tutorial n°2 :
Directives #if, #else, #elseif, #endif and #error


In this tutorial I will teach you how to handle the #if directive, accompanied #else directives #elseif, and #endif #error without which the directive would have no purpose.
We will also mention the #define directive, I invite you to read my previous tutorial: https://sampforum.blast.hk/showthread.php?tid=571802 !

What is the directive #if ?

This directive allows you to check a very specific condition, it is also a preprocessor in the Pawn.

What is the directive #else ?

This directive is in contradiction with the #if directive, it helps to check if the outcome is not accurate.

What is the directive #elseif ?

This directive is useful if you want to mention several contradictions in your checking. We will study it in more detail in a moment.

What is the directive #endif ?

This directive will be fully used to declare the end of our audit. We will study it in more detail in a moment.

What is the directive #error ?

This directive will be used to declare a compilation error in our script. It is very useful if you want to perform specific checks.
We will study it in more detail in a moment.

Use the directives

Usually you check a condition using if and else like this:

PHP Code:
#define MyInfo 3
if(MyInfo >= 3)
{
    print(
"MyInfo enough !");
}
else
{
    print(
"MyInfo inadequate !");

Thanks to our directives #if #else #error and #endif , here is our new code:

PHP Code:
#define MyInfo 3
#if MyInfo >= 3
    #error MonInfo enough !
#else
    #error MonInfo inadequate !
#endif 
It is obvious that you will not do all your checks using these directives. I advise you to use the top of your script to check fixed and precise information.

Now about the directive #elseif. We will use it to check other information:

PHP Code:
#define MyInfo 3
#if MyInfo >= 3
    #error MyInfo enough !
#elseif MyInfo < 3
    #error MyInfo inadequate !
#endif 
The advantage of using #elseif not #else each contradiction is already normal but above we use to perform several contradictions in the same verification .

This tutorial was, again, very short but it's really the principle of these directives, verification. I would say that the directive #error is a result after checking, but you understand how it is used. Hoping you have cleared again, do not hesitate to inform me of my tutorial erroneous information. If necessary, I am available for MP.

Good day!
Reply
#2

Not very thoroughly explained, imo. I even think your first example will give a "constant expression is non-zero" error, because the compiler has already established that 3 >= 3 at that point.
Reply
#3

yea must be ?

PHP Code:
if(MyInfo 3

    print(
"MyInfo inadequate !"); 

else 

    print(
"MyInfo enough !"); 

Reply
#4

@up:
I really suggest exiting early strategy, this way you won't get nested if's upon if's:

pawn Code:
if(MyInfo < 3)
{
    print("My info loves you, but it's not enough");
   
    return 1;
}

print("My info is adequate, you can work further here!");
Reply
#5

You two missed the point completely. "MyInfo" is a constant with value 3 (and constants should be written in all upper case), therefore when the preprocessor is done it will be fed to the compiler as:
PHP Code:
if(3
Clearly, 3 isn't smaller than 3, so the compiler translates it to:
PHP Code:
if(false
Therefore giving a compile time error because the corresponding compound block is never (or always) executed.
Reply
#6

Sorry, I just suggested a little bit of code for user above. Some people won't get the difference between compile time error and runtime one though Didn't want to confuse anyone, and it's nice you clarified it.
Reply
#7

Quote:
Originally Posted by Buthers
View Post

Qu'est-ce que la directive #endif ?


Vйrifier une condition

[/FONT] [/CENTER]
PS : this is not english lool "sorry for this up ^"
Reply
#8

Quote:
Originally Posted by Azula
View Post
PS : this is not english lool "sorry for this up ^"
I'm sorry, I'll translate my tutorial because I'm french ^^'
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)