Branch is instruction in a computer program that can cause a program to execute different task or statements based on a condition. Branching comparing variable value or function return value. There are 3 branching in C++.
- if-else
- switch-case
- ternary operator
Ternary Operator
Ternary operator is a simpler version of if-else. You can use ternary operator if you only need to choose between 2 values.
- Syntax : Condition?if_true:if_false;
Example :
#include <iostream>
using namespace std;
int main(){
int number;
string result;
cout << "Input : ";
cin >> number;
result=(number%2!=0)?"odd":"event";
cout << number << " is an ";
cout << result << " number";
return 0;
}
Example
Input : 23 23 is an odd number