From f5c34eb946926d66d87b26c533dc967779700b3b Mon Sep 17 00:00:00 2001 From: Vinayak Rao Upadhyaya <84091956+vinayakU94@users.noreply.github.com> Date: Fri, 21 Oct 2022 13:33:45 +0530 Subject: [PATCH] Update Short Handed If-else Short Handed If-Else was missing from if-else conditions in C++ . Which has been added with code and proper explanation. Please review and approve --- .../conditional-statements.md | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cpp/control-statements/conditional-statements.md b/cpp/control-statements/conditional-statements.md index 5d065ea..9e07ecf 100644 --- a/cpp/control-statements/conditional-statements.md +++ b/cpp/control-statements/conditional-statements.md @@ -194,3 +194,45 @@ int main() } ``` #### Check Result [here](https://onecompiler.com/cpp/3vmbg85we) + +## 5. Short handed if else / Ternary Operations + +There is also an abbreviation for if else called the ternary operator because it consists of three operands. Can be used to replace multiple lines of code with a single line. Often used to replace simple if else statements. +. + +### Syntax + +```c +variable = (condition) ? expressionTrue : expressionFalse; +``` +### Example +Instead of writing +```c +#include +using namespace std; + +int main() +{ + int age = 20; + if (age < 18) { + cout << "Underaged.; +} else { + cout << "Eligible to vote"; +} +} +``` + We can Simply Write + ```c +#include +using namespace std; + +int main() +{ + int age = 20; + string result = (age < 18) ? "Underaged" : "Eligible to vote"; + cout << result; +} +``` + +#### Check Result [here](https://onecompiler.com/cpp/3ykkar3su) +