There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.It can be used to replace multiple lines of code with a single line, and is most often used to replace simple if else statements:...
Syntax:condition ? <expression if true> : <expression if false> Here is an example usingif/else: ExampleGet your own React.js Server Before: if(authenticated){renderApp();}else{renderLogin();} Try it Yourself » Here is the same example using a ternary operator: ...
inttime =20; string result = (time <18) ?"Good day.":"Good evening."; cout << result; Try it Yourself » Exercise? What is the ternary operator in C++? A short-hand way of writing if...else statements A way to declare variables ...
There is also a short-hand if else, which is known as theternary operatorbecause it 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: ...