Home >Backend Development >C#.Net Tutorial >What is the only ternary operator in c language
The only ternary operator in C language is conditional expression, used to simplify if-else statements. Syntax: Condition ? Value 1 : Value 2, where the condition is a Boolean expression, and the return value is when value 1 and value 2 are true and false respectively.
The only ternary operator in C language
The only ternary operator in C language isConditional expression, it is a simplified if-else statement.
Syntax
<code>条件 ? 值1 : 值2</code>
Among them:
Condition
: A Boolean expression that determines whether to execute Value1
or Value2
.Value1
: The value returned if the condition
is true. Value2
: The value returned if the condition
is false. Example
<code class="c">int a = 10; int b = 20; int max = (a > b) ? a : b; // max 将为 20</code>
Working principle
The ternary operator works as follows:
conditional
expression first. condition
is true, then return value 1
. condition
is false, then return Value 2
.This operator can simplify conditional statements and make the code more concise and readable.
The above is the detailed content of What is the only ternary operator in c language. For more information, please follow other related articles on the PHP Chinese website!