English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn to use different forms of if..else statements in C ++Decision statements are created in the program.
Statement |
---|
if statement |
if...else statement |
Nested if ... else statements |
Ternary operator |
if (testExpression) { // The statements to be executed if testExpression is true }
The if statement evaluates testExpression within the parentheses.
If the calculation result of testExpression is true, execute the statements within the if body.
If the calculation result of testExpression is false, skip the statements within the if body.
The figure above describes how the if statement works.
// The program prints the positive number entered by the user // Skip if the user enters a negative number #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; // Check if the number is positive if (number > 0) { cout << "You have entered a positive integer: " << number << endl; } cout << "This statement is always executed."; return 0; }
Output1
Enter an integer: 5 You have entered a positive integer: 5 This statement is always executed.
Output2
Enter an integer: -5 This statement is always executed.
If the test expression (test Expression) evaluates to true, the code within the if...else statement is executed in the body of the if statement, and the code within the else body is skipped.
If the test expression (test Expression) is false, the code inside the else statement body is executed, and the code inside the if body is skipped.
//The program checks whether the integer is positive or negative //The program considers 0 as a positive number #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; if (number >= 0) { cout << "You entered a positive integer: " << number << endl; } else { cout << "You entered a negative integer: " << number << endl; } cout << "This line is always printed."; return 0; }
Output Result
Enter an integer: -4 You entered a negative integer: -4. This line is always printed.
The if...else statement executes two different pieces of code depending on whether the test expression (Test expression) is true or false. Sometimes, you must choose from more than two possibilities.
Using nested if...else statements, you can check multiple test expressions (Test expression) and execute different code for more than one condition.
This means you can use another if or else if statement inside an if or else if statement.
if (testExpression1) { // If testExpression1The statement to be executed if true } else if(testExpression2) { // If testExpression1The statement to be executed if false, testExpression2The statement to be executed if true } else{ if (testExpression 3) { // If testExpression1and testExpression2The statement to be executed if false, testExpression3The statement to be executed if true } else { // The statement to be executed if all test expressions are false } }
// The program checks whether an integer is positive, negative, or zero #include <iostream> using namespace std; int main() { int number; cout << "Enter an integer: "; cin >> number; if (number > 0) { cout << "You entered a positive integer: " << number << endl; } else { if (number < 0) { cout << "You entered a negative integer: " << number << endl; } cout << "You entered 0." << endl; } } cout << "This line is always printed."; return 0; }
Output Result
Enter an integer: 0 You entered 0. This line is always printed.
Ternary operator for3one operand for operation, which can be used instead of if...else statements.
The following if code:
if ( a < b ) { a = b; } else { a = -b; }
You can replace the above code with a ternary operator:
a = (a < b) ? b : -b;
The ternary operator is shorter and more readable than the if...else conditional statement.