added ternary operation to conditional statements (#22773)
This commit is contained in:
@ -234,6 +234,7 @@ printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumbe
|
||||
Finally, the break statement ends the switch statement.
|
||||
|
||||
If break statement is not used, all cases after the correct case is executed.
|
||||
|
||||
## finding the Bigger among two numbers using if else statement.
|
||||
```C
|
||||
int a,b;
|
||||
@ -251,3 +252,22 @@ else
|
||||
printf("B is the bigger number");
|
||||
}
|
||||
```
|
||||
## 6. Ternary operation
|
||||
|
||||
The ternary operator (AKA conditional 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 flase comparison .It can be thought of as a shortened way of writing an if-else statement. It is often used to to assign variables based on the result of a comparison.
|
||||
|
||||
#### Syntax
|
||||
```C
|
||||
v = (conditional_statement) ? value_if_true : value_if_false
|
||||
|
||||
```
|
||||
#### Example
|
||||
```C
|
||||
int a, b = 10, c = 100;
|
||||
a = (b > c) ? 1 : 2;
|
||||
printf("%d", a);
|
||||
```
|
||||
|
||||
#### Result
|
||||
|
||||
`2`
|
Reference in New Issue
Block a user