The control structure if

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Control structures

    /

  5. The control structure if

Dynamic programs

We have seen how to code conditional operations. Now, it is time to learn to make our programs react accordingly to the result of those operations.

If

The control structure if is used to make the program execute a section of code only if the result of a conditional operation is true. With control structures, the conditional operation used to determine what to do is called the condition. The structure if is coded by writing the keyword if followed by the condition inside parentheses () and finally, inside braces {}, the instructions to execute if the condition is true.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> int main() { int n = 23; if(n == 23) { // Executed only if n equals 23. std::cout << "n equals 23." << std::endl; } // The execution of the program continues here. if(n > 765) { // Executed only if the value of n is bigger than 765. std::cout << "The value of n is bigger than 765." << std::endl; } return 0; }

An other example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> int main() { int num; std::cout << "Enter an integer number: "; std::cin >> num; if(num < 10 || num > 100) { std::cout << "The number entered is either smaller than 10 or bigger than 100.\n"; } return 0; }

Actually, if the value inside the parentheses () of an if statement is different than 0, the instructions inside the braces {} below it are executed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream> int main() { bool boolean = true; if(boolean) { std::cout << "The variable equals true." << std::endl; } if(true) { std::cout << "The value inside the parentheses is true." << std::endl; } if(1) { std::cout << "true equals 1" << std::endl; } if(false) { std::cout << "I will not be printed into the console." << std::endl; } int boolean2 = 56; if(boolean2) { std::cout << "If the value inside its parentheses is different than zero, the instructions"; std::cout << " in the braces of the control structure are executed." << std::endl; } return 0; }

Else if

It is possible to write an else if statement after an if or else if statement to execute a section of code only if the condition inside it is true and the conditions inside the if and else if statements above it are false. It is written the same way as an if statement, except that the keyword is else if and it must placed after an if or else if statement.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> int main() { int n = 50; if(n == 23) { std::cout << "n equals 23." << std::endl; // Executed only if n equals 23. } else if(n > 40) { std::cout << "n is bigger than 40." << std::endl; // Executed only if n is bigger than 40. } return 0; }

Else

Finally, the else statement is used to execute a section of code only if all the conditions inside the if and else if statements above it equal false. Unlike with the if and else if statements, there is no condition inside the else statement so there are no parentheses () following it.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> int main() { int n = 65; if(n == 9) { std::cout << "n equals 9." << std::endl; // Executed only if n equals 9 } else { std::cout << "n does not equal 9." << std::endl; // Executed only if n does not equal 9 } return 0; }

The example above is equivalent to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> int main() { int n = 65; if(n == 9) { std::cout << "n equals 9." << std::endl; // Executed only if n equals 9 } else if(true) { std::cout << "n does not equal 9." << std::endl; // Executed only if n does not equal 9 } return 0; }

Braces

The braces after all control structures are optional if, and only if, there is only one instruction inside them. When there are no braces, the instruction following the statement is considered to be the only instruction it contains.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream> int main() { int n=7; if(n == 7) { // Here, braces are required, because there are more than one instruction (There are 2). std::cout << "n equals 7." << std::endl; n = 0; } else // Here, braces are optional, because there is only one instruction. std::cout << "n does not equal 7." << std::endl; std::cout << "I am not inside the 'else' statement above me."<< std::endl; return 0; }

Complete example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> int main() { unsigned int n; std::cout << "Enter a positive number: "; std::cin >> n; if(n < 10) std::cout << "The number entered is smaller than 10." << std::endl; else if(n < 50) std::cout << "The number entered is smaller than 50." << std::endl; else if(n < 100) std::cout << "The number entered is smaller than 100." << std::endl; else std::cout << "The number entered is bigger than 99." << std::endl; return 0; }