If Statements

Last modified by Microchip on 2023/11/09 09:06

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.

If Statement Flow Chart

Example

1 {
2  int x = 5;
3
4  if (x)
5  {
6     printf("x = %d\n", x);
7  }
8  while (1);
9 }

Testing for TRUE

  • if cancel vs. if (x == 1)
    • if cancel 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?

Answer

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.

Explanation of binary wrap around

Back to Top

Nested if Statements

You can use one if or else if statement inside another if or else if statement(s).

Example

1 int power = 10;
2 float band = 2.0;
3 float frequency = 146.52;
4
5 if (power > 5)
6 {
7     if (band == 2.0)
8     {
9         if ((frequency > 144) && (frequency < 148))
10         {
11             printf("Yes, it's all true!\n");
12         }
13     }
14 }

Back to Top

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.

If-Else Statement Flow Chart

Example

1 {
2  float frequency = 146.52;  //frequency in MHz
3
4  if ((frequency >= 144.0) && (frequency <= 148.0))
5    {    
6      printf("You're on the 2 meter band\n");
7    }
8  else
9    {
10      printf("You're not on the 2 meter band\n");
11    }
12  }

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.

Back to Top

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.

If Else If Statement Flow Chart

Example

1 if ((freq > 144) && (freq < 148))
2    printf("You're on the 2 meter band\n");
3 else if ((freq > 222) && (freq < 225))
4    printf("You're on the 1.25 meter band\n");
5 else if ((freq > 420) && (freq < 450))
6    printf("You're on the 70 centimeter band\n");
7 else
8    printf("You're somewhere else\n");

Back to Top