C++ If Else C-PLUS-PLUS
- C++ Introduction
- C++ Syntax
- C++ Output (cout <<)
- C++ Comments
- C++ Variables
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ If Else
- C++ Switch
- C++ While Loop
- C++ Do/While Loop
- C++ For Loop
- C++ Break and Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Functions Parameters
- C++ Function Overloading
- C++ OOP
- C++ Classes and Objects
- C++ Class Methods
- C++ Constructors
- Destructors in C++
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Polymorphism
- C++ Files
- C++ Exception Handling
C++ If Else
C++ Conditions and If Statements
C++ supports the usual logical conditions of mathematics. Example:
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
C++ has the following conditional statements:
- Use
ifto specify a block of code to be executed, if a specified condition is true - Use
elseto specify a block of code to be executed, if the same condition is false - Use
else ifto specify a new condition to test, if the first condition is false - Use
switchto specify many alternative blocks of code to be executed
C++ if Statement
Use the if statement to specify a block of C++ code to be executed if a condition is true.
Syntax
// block of code to be executed if the condition is true
}
Note: if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
Example
#include <iostream>
using namespace std;int main() {
int x = 21;
int y = 16;
if (x > y) {
cout << "x is greater than y";
}
return 0;
}
Output:
x is greater than y
In the example above we use two variables, x and y, to test whether x is greater than y (using the > operator). As x is 21, and y is 16, and we know that 21 is greater than 16, we print to the screen that "x is greater than y".
C++ else Statement
Use the else statement to specify a block of code to be executed if the condition is false.
Syntax:
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
#include <iostream>
using namespace std;int main() {
int time = 21;
if (time < 16) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
Output:
Good evening.
In the example above, time (21) is greater than 16, so the condition is false. Because of this, we move on to the else condition and print to the screen "Good evening". If the time was less than 16, the program would print "Good day".
C++ else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Syntax
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Example
#include <iostream>
using namespace std;int main() {
int time = 21;
if (time < 11) {
cout << "Good morning.";
} else if (time < 21) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
Output:
Good evening.
In the example above, time (21) is greater than 11, so the first condition is false. The next condition, in the else if statement, is also false, so we move on to the else condition since condition1 and condition2 is both false - and print to the screen "Good evening".
C++ If Else (Ternary Operator)
The ternary operator consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if-else statements.
Syntax
Instead of writing:
Example
if (time < 16) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
You can simply write:
Example
string result = (time < 16) ? "Good day." : "Good evening.";
cout << result;
Example
#include <iostream>
#include <string>
using namespace std;int main() {
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
return 0;
}
Output:
Good evening.