SA-MP Forums Archive
if and #if - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: if and #if (/showthread.php?tid=598432)



if and #if - [eLg]elite - 12.01.2016

What is the difference between if and #if. Which one is better to use and in what instances? I have never seen #if used other then in the basic 'new' SAMP script. I apologize if this is a stupid question.


Re: if and #if - -CaRRoT - 12.01.2016

#if is known as a directive, its a preprocessor in PAWN and you can use them to check specific conditions.

You can choose what to compile and what to not, ex:

Код:
#define LIMIT 10
 
if (LIMIT < 10)
{
	printf("Limit too low");
}
which would compile as :

Код:
if (10 < 10)
{
	printf("Limit too low");
}
The compiler knows that its not true and never will be so it would give you a constant expression warning, so the only reason you would want to keep this code if someone changes the limit and recompiles, so that's when #if comes in play, Unlike normal if which give a warning if the expression is constant, #if expressions MUST be constant. So:

Код:
#define LIMIT 10
 
#if LIMIT < 10
	#error Limit too low
#endif
If the limit is too small, it would give you a compiler error instead of you having to manually test it as it would check the code.

Source: Wiki for examples.


Re: if and #if - [eLg]elite - 12.01.2016

Oh, I see, thank you. Can it be used in a loop too?


Re: if and #if - Vince - 12.01.2016

A directive is only evaluated at compile time. It can be placed anywhere.


Re: if and #if - [eLg]elite - 12.01.2016

Oh, okay. Thank you.