if and #if
#1

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.
Reply
#2

#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.
Reply
#3

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

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

Oh, okay. Thank you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)