Introduction
After studying C+++ if-else statement and C++ switch case statement, this article will talk about the c++ conditional operator: what is T? What is the proper expression, and give some examples to understand the concept?
C++ conditional operator
What is a conditional operator?
The conditional operators evaluate a condition and return one of two values.The conditional operator is the short and simplified version of the if-else statement, and it is not a very different switch case statement.
The conditional operator syntax
The conditional operator syntax
(conditional_expression)? true-block : false-block
The following lines explain the syntax of the conditional operator:
- The conditional operand, entre parentheses, is a conditional expression.
- The conditional operand, entre parentheses, is a conditional expression.
- The question mark.
- Colon symbol.
- The second expression.
How it works
How it works
The conditional operator is evaluated. When the result is true, the conditional operator returns the value of block 1. When the result is false, the conditional operator returns the value of block 2.
Example of C++ conditional operator
Example 1
introduce the example
Prompt the user to enter the number of males and the number of males. The program tells which one is more female or male.
The code of the program
#include<iostream>using namespace std;int main() {int number_males {};int number_females {};cin >> number_males;cout << endl;cin >> number_females;
cout << endl;cout << ((number_males > number_females) ?”The number of males is greater than the number of females” :”The number of females is greater than the number of males”) << endl;}
The output of the program
Example 2
introduce the example
Prompt the user to enter the highest score he had today. The program gives feedback about the score.
The code of the program
#include<iostream>using namespace std;int main() {int score {};cout << “Enter the highest score you had today” << endl;cout << endl;cin >> score;cout << endl;(score >= 10) ? (cout << “You are great” << endl) : (cout << “Completely unacceptable result” << endl);}
The output of the program
Example 3
introduce the example
Prompt the user to enter a number. The program displays if the number is divisible by 3 or not.
The code of the program
#include<iostream>using namespace std;int main() {int choose_number {};cout << “Learn the numbers that are divisible by 3” << endl;cout << “Enter a number” << endl;cout << endl;cin >> choose_number;cout << endl;cout << ((choose_number % 3 == 0) ? “The number you suggested is divisible by 3 .” :”The number you suggested is not divisible by 3 .”) << endl;cout << endl;}
The output of the program
Example 4 (Another way to solve the situation of the third example)
introduce the example
Prompt the user to enter a number. The program displays if the number is divisible by 3 or not.
The code of the program
#include<iostream>using namespace std;int main() {int choose_number {};cout << “Learn the numbers that are divisible by 3” << endl;cout << “Enter a number” << endl;cout << endl;cin >> choose_number;cout << endl;if(choose_number%3==0)cout<<choose_number<<” divisible by 3″<<endl;elsecout<<choose_number<<” not divisible by 3 .”<<endl;}
The output of the program
Conclusion
C++ conditional operator is a simple and easy way to deal with conditions in C++ programs. There is a condition expression if it is true, the program returns the first expression, and if it is false, the program returns the second expression. Not as simple as that.