If Statements
An if statement consists of a Boolean expression followed by one or more statements.
Syntax
if (expression) statement
expression is evaluated for Boolean TRUE (≠0) or FALSE (=0). If TRUE, then statement is executed. If the Boolean expression evaluates to FALSE, then the first set of code after the end of the if statement will be executed.
Whenever you see statement in a syntax guide, it may be replaced by a compound (block) statement.
Remember: spaces and new lines are not significant.

Example
{ int x = 5; if (x) { printf("x = %d\n", x); } while (1);} |
Testing for TRUE
- if (x) vs. if (x == 1)
- if (x) only needs to test for not equal to 0
- if (x == 1) needs to test for equality with 1
- Remember: TRUE is defined as non-zero, FALSE is defined as zero
Questions
What will print if x = 5 ? … if x = 0?
if x = -82?
if x = 65536?
Answers
If x = 5, we will print "x = 5".
If x = 0, nothing will print.
If x = -82, we will print "x = -82".
If x = 65536, nothing will print.
- An unsigned integer can only hold a value up to 216-1 = 65535 = 0xFFFF (signed integer values larger than 32767 are interpreted as negative but still take up the same number of bits). Therefore, we have overflowed the capacity of an integer. In binary, 216 = 65536 = 0x10000 is a 1 followed by 16 zeros. Since an int can only hold 16-bits, the 17th bit is chopped off, leaving us with a value of 0x0000 or false. Using 65536 will result in 0 regardless of whether x is a signed or unsigned int.
- As a point of interest, if you assign a value of 216-1 = 65535 = 0xFFFF to a signed int:
int x = 65535;
The value of x will be -1, since 65535 = 0xFFFF is the twos complement of 1 = 0x0001 and it represents a value of -1 = 0xFFFF.
Remember that a signed int can hold values from -32768 = 0x8000 to 32767 = 0x7FFF.

Nested if Statements
You can use one if or else if statement inside another if or else if statement(s).
Example
int power = 10;float band = 2.0;float frequency = 146.52;if (power > 5){ if (band == 2.0) { if ((frequency > 144) && (frequency < 148)) { printf("Yes, it's all true!\n"); } }} |
if else Statement
Syntax
if (expression) statement1
else statement2
expression is evaluated for boolean TRUE (≠0) or FALSE (=0). If TRUE, then statement1 is executed. If FALSE, then statement2 is executed.

Example
{ float frequency = 146.52; //frequency in MHz if ((frequency >= 144.0) && (frequency <= 148.0)) { printf("You're on the 2 meter band\n"); } else { printf("You're not on the 2 meter band\n"); } } |
In this example, not only are we seeing a real-world if-else statement but we are also seeing an example of a more complex condition expression. Here, two conditions must be met: the frequency must be greater than 144.0 and it must be less than 148.0 for the first statement to be executed. If frequency does not fall in the range of 144.0 to 148.0, then the second statement will be executed (the one in the else clause). Notice that we are using the logical AND && operator and not the bitwise and & operator.
if else if Statement
Syntax
if (expression1) statement1
else if (expression2) statement2
else statement3
expression1 is evaluated for boolean TRUE (≠0) or FALSE (=0). If TRUE, then statement1 is executed. If FALSE, then expression2 is evaluated. If TRUE, then statement2 is executed. If FALSE, then statement3 is executed.

Example
if ((freq > 144) && (freq < 148)) printf("You're on the 2 meter band\n");else if ((freq > 222) && (freq < 225)) printf("You're on the 1.25 meter band\n");else if ((freq > 420) && (freq < 450)) printf("You're on the 70 centimeter band\n");else printf("You're somewhere else\n"); |