C++ Programming Tutorial Lesson 03: Else If Statements

In the previous lesson the concept of if and else statements was presented. This lesson will extend if and else statements to include "else if" statements. This is basically a chain of if and else statements that allows for more flexibility in the conditional tests performed.

Else If Example

#include <stdlib.h>
#include <stdio.h>

int main(void) {
   int n;
   printf("Please enter a number: ");
   scanf("%d", &n);
   if (n == 1) {
      printf("n is equal to 1!\n");
   }
   else if (n == 2) {
      printf("n is equal to 2!\n");
   }
   else if (n == 3) {
      printf("n is equal to 3!\n");
   }
   else {
      printf("n isn't equal to 1, 2, or 3.\n");
   }
   system("PAUSE");
   return 0;
}
Feel free to download the Else If Example source code directly. This example builds off of the If Else Example #2 source code from Lesson 02: If and Else Statements. The program reads user input from the keyboard until the return key is pressed. The user inputs a number, which is stored into the integer variable "n". The variable "n" is then tested in a series of if, else if, and else statements. If the user inputs the number 1, then "n is equal to 1!" is printed as shown below.



If instead the user inputs the number 2, then "n is equal to 2!" is printed as shown below.



If instead the user inputs the number 3, then "n is equal to 3!" is printed as shown below.



However, if the user inputs any number other than 1, 2, or 3, such as 5, then "n isn't equal to 1, 2, or 3." is printed as shown below.



The table below describes the new code presented in the above example.

Source Code Description & Explanation
if (n == 1) {
   printf("n is equal to 1!\n");
}
The first if statement tests if n is equal to 1. If it is, then the printf() function will be run to print out "n is equal to 1!". Otherwise, the code encapsulated in the next else statement will be run.
else if (n == 2) {
   printf("n is equal to 2!\n");
}
This is the next else statement that follows the first if statement above. If n is not equal to 1, then it will run the if statement here to test if n is equal to 2. This if statement comes immediately after the else statement, and is referred to as an "else if" statement. So, if n is not equal to 1, and n is equal to 2, then the printf() function here will be run and "n is equal to 2!" will be displayed. If n is not equal to 1, and n is not equal to 2, then the next else statement (else statement #2) will be run.
else if (n == 3) {
   printf("n is equal to 3!\n");
}
This is the next else statement (else statement #2). It will run the if statement here to test to see if n is equal to 3. Hence, it is our second "else if" statement. If n is not equal to 1 or 2, and n is equal to 3, then the printf() function here will be run and "n is equal to 3!" will be displayed. If n is not equal to 1, 2, or 3, then the next else statement (else statement #3) will be run.
else {
   printf("n isn't equal to 1, 2, or 3.\n");
}
This is the last else statement (else statement #3). It has no if statement following it. Therefore, this is not an "else if" statement. This else statement will only run the printf() function to display "n isn't equal to 1, 2, or 3." if n is not equal to 1, 2, or 3 as tested in the previous if and else if statements.

Copyright © 2008 Pierre Dufilie IV. All Rights Reserved.