Skip to content

Commit

Permalink
Added Day 7 Content - ternary operator (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Varad0014 authored Jun 2, 2024
1 parent 593c230 commit 40c5f81
Showing 1 changed file with 93 additions and 7 deletions.
100 changes: 93 additions & 7 deletions docs/day-07/ternary-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,101 @@ sidebar_label: "Ternary Operator"
slug: ternary-operator-in-cpp
---

TASK:

1. What is the Ternary Operator in C++?
2. Explain the Syntax of Ternary Operator in C++.
3. How does the Ternary Operator work in C++?
4. C++ Ternary Operator Example
5. C++ Ternary Operator with Nested Ternary Operator
## What is the Ternary Operator in C++?
In C++, the ternary operator, also known as **conditional operator**, is a shorthand way of writing conditional (if-else) statements. It is known as ternary operator because it takes **three** operands. This operator can be used to replace simple multi-line if-else statements with a single line of code.



IMAGE FILE:
![Ternary in CPP](../../static/img/day-07/ternary-operator.png)

## Syntax of Ternary Operator in C++
The ternary/conditional operator `(? :)` takes 3 operands (`condition`, `expression_1` and `expression_2`). The basic syntax of ternary operator is:
```cpp
condition ? expression_1 : expression_2;
```
The above statement, if written using `if-else`, is the same as:
```cpp
if(condition){
expression_1;
}
else{
expression_2;
}
```

## How does the Ternary Operator work in C++?
The ternary operator works by first evaluating the condition. If the condition evaluates to `true`, then the code corresponding to `expression_1` is executed. If the condition evaluates to `false`, then code corresponding to `expression_2` is executed.

## C++ Ternary Operator Examples
### 1. Print the max of two integers
The following program takes two integers as input and prints maximum of them.

```cpp
#include<iostream>
using namespace std;

int main(){
int a, b;
cout<<"Enter two numbers: ";
cin>>a>>b;
int maxi = (a > b) ? a : b;
cout<<"Max number is "<<maxi<<endl;
return(0);
}
```
If condition `a>b` evaluates to `true`, then `maxi` stores the larger value `a`. If condition `a>b` evaluates to `false`, then `maxi` stores the value of `b`.

### 2. Get absolute value of an integer
The function `get_abs` takes an integer as input and returns the absolute value of integer. The function shows the use of `return` with ternary operator.

```cpp
#include <iostream>
using namespace std;

int get_abs(int n){
return (n>=0 ? n : -n);
}

int main()
{
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Absolute value is " << get_abs(n) << endl;
return (0);
}
```
``
NOTE:
``
The following syntax for the function is **incorrect**:
```cpp
int get_abs(int n){
n>=0 ? return(n) : return(-n);
}
// This is because return is a statement
// whereas ternary operator requires operands
// to be expression and not statement.
```

## C++ Ternary Operator with Nested Ternary Operator
The following program takes three integers as input and prints maximum of them.
```cpp
#include<iostream>
using namespace std;

int main(){
int a, b, c;
cout<<"Enter three numbers: ";
cin>>a>>b>>c;
int maxi = (a > b) ? ((a > c) ? a : c) : ((b>c) ? b : c);
cout<<"Max number is "<<maxi<<endl;
return(0);
}
```

If condition `a>b` evaluates to `true`, then we check the condition `a>c`. If `a>c` evaluates to `true`, then `maxi` stores the value of `a`, else `maxi` stores the value of `c`.

If condition `a>b` evaluates to `false`, then we check the condition `b>c`. If `b>c` evaluates to `true`, then `maxi` stores the value of `b`, else `maxi` stores the value of `c`.

0 comments on commit 40c5f81

Please sign in to comment.