I learned that if
or #if
can both be used for condition checks. As we can check conditions using if
, why would we use preprocessor #if
?
What difference will it make to my code if I use #if
instead of if
?
Which one is better to use and why?
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False)
To do something specific when two or more conditions are TRUE, you can use the IF function in combination with the AND function to evaluate conditions with a test, then take one action if the r esult is TRUE, and (optionally) do take another if the...
Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.
if
and #if
are different things with different purposes.
If you use the if
statement, the condition is evaluated at runtime, and the code for both branches exists within the compiled program. The condition can be based on runtime information, such as the state of a variable. if
is for standard flow control in a program.
If you use the preprocessor's #if
, the condition is evaluated at compile-time (originally this was before compile-time, but these days the preprocessor is usually part of the compiler), and the code for the false branch is not included in the compiled program. The condition can only be based on compile-time information (such as #define
constants and the like). #if
is for having different code for different compile-time environments (for instance, different code for compiling on Windows vs. *nix, that sort of thing).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With