C++ Programming Tutorial Lesson 02: If and Else Statements

Often in programming we find a situation where we would like to tell the computer to test a condition and based upon the outcome of that condition, perform an associated action. One of the most common ways to do this is through the use of if and else statements.

If Else Example #1

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

int main(void) {
   if (1 == 1) {
      printf("1 is equal to 1 after all!\n");
   }
   else {
      printf("1 isn't equal to 1? What!?\n");
   }
   system("PAUSE");
   return 0;
}
Feel free to download the If Else Example #1 source code directly. If you compile and run this program, then you should see a console window as depicted below that says "1 is equal to 1 after all!" and then an indication that you can press any key to continue the program execution.



Notice that the source code is very similar to the Hello World example. In this example, the call to the printf() function was replaced by an if statement and an else statement along with two different calls to the printf() function. This new code is described in the table below.

Source Code Description & Explanation
if (1 == 1) {
   printf("1 is equal to 1 after all!\n");
}
Here we have an if statement. The if statement has a condition in parentheses, "(1 == 1)" in this case, that will either evaluate to true or false. The if statement encapsulates a set of code with the curly braces, { and }. If the condition in parentheses, "(1 == 1)", evaluates to true, then the encapsulated code will be run. In this example the printf() function will be called if 1 is equal to 1. Of course, this is always true, and therefore, "1 is equal to 1 after all!" will be displayed by the printf() function.
else {
   printf("1 isn't equal to 1? What!?\n");
}
Following the above if statement is an else statement. The else statement encapsulates a set of code that will be run in the case where the condition in the above if statement evaluates to false. In this example, the code encapsulated by the else statement will be run if 1 is not equal to 1. Of course, this will never happen because 1 is always equal to 1.

Simply put, we're telling the computer "if this condition is true then run this code. Otherwise, run this other code." To better demonstrate the usefulness of if and else statements, let's look at another example.

If Else Example #2

#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 {
      printf("n isn't equal to 1.\n");
   }
   system("PAUSE");
   return 0;
}

Feel free to download the If Else Example #2 source code directly. This second example of if and else statements makes use of the scanf() function. It reads user input from the keyboard until the return key is pressed. First we indicate to the user using the printf() function that we are waiting for the user to enter a number using the keyboard and press return. The user then inputs a number, which is stored into the integer variable "n". Remember that the user must hit the return key before the scanf() function will return. The if statement tests to see whether or not n is equal to 1. As shown below, when the user inputs the number 5, "n isn't equal to 1." is printed as a result.



However, if the user inputs the number 1, "n is equal to 1!" is printed as shown below.



The table below describes the new code presented in example #2.

Source Code Description & Explanation
int n;
This tells the compiler that we are defining a variable of integer type that will be referred to as "n". The variable "n" will store a number that is in the form of an integer. We can use variable "n" to store a number input by the user using the keyboard.
printf("Please enter a number: ");
Here, the printf() function is called to print out on the screen "Please enter a number: " so that we can tell the user we are waiting for a number to be entered using the keyboard.
scanf("%d", &n);
This is the scanf() function. It is defined in stdio.h, and it is used to accept input from the user's keyboard. The function will wait for the user to enter characters from the keyboard and then for the return key to be pressed. In this case we are looking for a number as indicated by the "%d" string. The scanf() function accepts an additional parameters, which are separated from the "%d" string and by each other by commas. The additional parameter "&n" indicates to the scanf() function that the number that is input by the user will be stored into the variable "n" that we defined above. After scanf() returns, we will have the number the user input stored in variable "n".
if (n == 1) {
   printf("n is equal to 1!\n");
}
Here is our if statement again; however, this time we are testing to see if the value stored in variable "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 else statement will be run.
else {
   printf("n isn't equal to 1.\n");
}
Here is the associated else statement that goes with the above if statement. It encapsulates code that will be run in the case where the condition of the if statement evaluates to false. In this case, if variable "n" is not equal to 1, then the program will run the printf() function to print out "n isn't equal to 1.".

Copyright © 2008 Pierre Dufilie IV. All Rights Reserved.