simplified ternary operator article (#33077)
This commit is contained in:
@ -4,10 +4,22 @@ title: Ternary Operator
|
||||
|
||||
## Ternary Operator
|
||||
|
||||
Programmers use ternary operators in C for decision making inplace of conditional statements **if** and **else**.
|
||||
The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. If it helps you can think of the operator as shortened way of writing an if-else statement.
|
||||
The ternary operator in C is a shorthand for simple **if/else** statements.
|
||||
|
||||
Here's a simple decision-making example using **if** and **else**:
|
||||
It takes three arguments:
|
||||
1. An condition
|
||||
2. The result if the condition evaluates to true
|
||||
3. The result if the condition evaluates to false
|
||||
|
||||
### Syntax
|
||||
|
||||
`condition ? value_if_true : value_if_false`
|
||||
|
||||
`value_if_true` and `value_if_false` must have the same type, and must be simple expressions not full statements.
|
||||
|
||||
### Example
|
||||
|
||||
Here's an example without the ternary operator:
|
||||
|
||||
```c
|
||||
int a = 10, b = 20, c;
|
||||
@ -22,15 +34,7 @@ else {
|
||||
printf("%d", c);
|
||||
```
|
||||
|
||||
This example takes more than 10 lines, but that isn't necessary. You can write the above program in just 3 lines of code using the **ternary operator**.
|
||||
|
||||
### Syntax
|
||||
|
||||
`condition ? value_if_true : value_if_false`
|
||||
|
||||
The statement evalutes to statement\_1 if the condition is true, and statement\_2 otherwise.
|
||||
|
||||
Here's the above example re-written to use the ternary operator:
|
||||
Here's the above example re-written to use the **ternary operator**:
|
||||
|
||||
```c
|
||||
int a = 10, b = 20, c;
|
||||
@ -40,17 +44,20 @@ c = (a < b) ? a : b;
|
||||
printf("%d", c);
|
||||
```
|
||||
|
||||
Output of the example should be:
|
||||
Both examples will output:
|
||||
|
||||
```c
|
||||
10
|
||||
```
|
||||
|
||||
`c` is set equal to `a`, because the condition `a<b` was true.
|
||||
`c` is set equal to `a` (10), because the condition `a < b` was true.
|
||||
|
||||
This looks pretty simple, right? Do note that `value_if_true` and `value_if_false` must have the same type, and they cannot be full statements but simply expressions.
|
||||
|
||||
The ternary operator can be nested similarly to nested if-else statements. Consider this nested if-else statement :
|
||||
### Nested Example
|
||||
|
||||
The ternary operator can also be nested.
|
||||
|
||||
Consider this nested if-else statement :
|
||||
```c
|
||||
int a = 1, b = 2, ans;
|
||||
if (a == 1) {
|
||||
@ -73,7 +80,7 @@ ans = (a == 1 ? (b == 2 ? 3 : 5) : 0);
|
||||
printf ("%d\n", ans);
|
||||
```
|
||||
|
||||
The output of both of the above codes should be:
|
||||
The output of both of the above code snippets will be:
|
||||
|
||||
```c
|
||||
3
|
||||
|
Reference in New Issue
Block a user