Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Create Conditional_operator.md #4178

Merged
merged 1 commit into from
Aug 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/pages/cplusplus/Conditional_operator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Conditional Operator
---

## Conditional Operator

Conditional operator is a ternary operator, that is it needs 3 operands.
Conditional operator is used to replace a simple if-else statements.

Syntax :
```cpp
(condition)?(expression-1):(expression-2);
```
Here, expression-1 is evaluated when condition is true and expression-2 is evaluated when condtion is false.
Similar if-else statement would be :
```cpp
if(condition)
{
expression-1;
}
else
{
expression-2;
}
```
Hence conditional operator is very handy when you need to write simple if-else statement. It can also be used in #define
preprocessor when similar condition is to be used in multiple places.

For example, to find maximum of two number conditional operator can be used as follows :

```cpp
#define big(a,b) (a>=b)?a:b

int maximum,x=5,y=6; // variable to store maximum of two numbers
maximum=(x>y)?x:y; // directly using conditional operator
maximum=big(x,y); // using the #define preprocessor defined above as big
```
**Good Luck to all of you**

**Happy Coding ! :)**

**Feel free to ask any queries on FreeCodeCamp's GitHub page or [FreeCodeCamp's Forum .](https://forum.freecodecamp.org/)**