What character(s) are used to form the ternary conditional operator in c#?

C# includes a decision-making operator ?: which is called the conditional operator or ternary operator. It is the short form of the if else conditions.

Syntax:

condition ? statement 1 : statement 2

The ternary operator starts with a boolean condition. If this condition evaluates to true then it will execute the first statement after ?, otherwise the second statement after : will be executed.

The following example demonstrates the ternary operator.

Example: Ternary operator

int x = 20, y = 10;

var result = x > y ? "x is greater than y" : "x is less than y";

Console.WriteLine(result);

Try it

output:

x is greater than y

Above, a conditional expression x > y returns true, so the first statement after ? will be execute.

The following executes the second statement.

Example: Ternary operator

int x = 10, y = 100;

var result = x > y ? "x is greater than y" : "x is less than y";

Console.WriteLine(result);

output:

x is less than y

Thus, a ternary operator is short form of

int x = 20, y = 10;

var result = x > y ? "x is greater than y" : "x is less than y";

Console.WriteLine(result);
1 statement. The above example can be re-write using
int x = 20, y = 10;

var result = x > y ? "x is greater than y" : "x is less than y";

Console.WriteLine(result);
1 condition, as shown below.

Example: Ternary operator replaces if statement

int x = 10, y = 100;

if (x > y)
    Console.WriteLine("x is greater than y");
else
    Console.WriteLine("x is less than y");

Try it

output:

x is greater than y

Nested Ternary Operator

Nested ternary operators are possible by including a conditional expression as a second statement.

Example: Nested ?:

int x = 10, y = 100;

string result = x > y ? "x is greater than y" : 
                    x < y ? "x is less than y" : 
                        x == y ? "x is equal to y" : "No result";

Console.WriteLine(result);

Try it

The ternary operator is right-associative. The expression

int x = 20, y = 10;

var result = x > y ? "x is greater than y" : "x is less than y";

Console.WriteLine(result);
3 is evaluated as
int x = 20, y = 10;

var result = x > y ? "x is greater than y" : "x is less than y";

Console.WriteLine(result);
4, not as
int x = 20, y = 10;

var result = x > y ? "x is greater than y" : "x is less than y";

Console.WriteLine(result);
5.

Using the Ternary operator in c is a way to shorten the if-else code block in C/C++. So before you move further in this article, please go through the C if-else statement. (if you are a beginner).

Ternary Operator in C takes three arguments:

  1. The first argument in the Ternary Operator in C is the comparison condition.
  2. The second argument in the Ternary Operator in C is the result if the condition is true.
  3. The third argument in the Ternary Operator in C is the result if the condition is false.

So, according to the above three arguments in the ternary operator in c, we can say that the Ternary operator in C allows us to execute different code depending on the first argument, i.e. based on condition.

The symbol for the Ternary operator in C is ? :.

Syntax

The syntax of the Ternary Operator in C is:

Syntax:

Working of Syntax:

  • If the condition in the ternary operator is met (true), then the exp2 executes.
  • If the condition is false, then the exp3 executes.

Example:

The following example explains the working of the Ternary Operator in C.

int mxNumber = 10 > 15 ? 10 : 15;

So, if the condition 10 > 15 is true (which is false in this case) mxNumber is initialized with the value 10 otherwise with 15. As the condition is false so mxNumber will contain 15. This is how Ternary Operator in C works.

Flow Chart for Ternary Operator in C

The flow chart of the Ternary Operator in C looks like this:

What character(s) are used to form the ternary conditional operator in c#?

Let's understand this flow chart of Ternary Operator in C:-

Suppose we have taken a ternary operator statement exp1? exp2: exp3, if our exp1 met the condition or yield result in true the control flows to exp2. Similarly, if exp1 gives a false result, then our control goes to exp3.

Therefore, if the exp1, a condition that is true, then control flows to the True-Expression otherwise, control goes to the False_Expression. And if there is any next statement, the control goes to that statement, like in the above example variable mxNumber gets the value 15.

Isn't it similar to the simple if-else code in C ? YES !! That's why the Ternary Operator in C is also known as Conditional Operator as its works in the same way as if-else works in C.

Examples

These examples will teach us how to use the Ternary operator in C.

Example #1

Find the maximum number from the given two integer type numbers using the if-else block in C and with the Ternary Operator in C.

Using if-else block

#include
#include

int main() {
  int num1, num2, mxNumber;
  num1 = 10;
  num2 = 20;

  if (num1 >= num2) // condition checking
  {
    mxNumber = num1; // if condition true
  } else {
    mxNumber = num2; // if condition false
  }

  printf("Maximum Number from %d and %d is %d", num1, num2, mxNumber); //output
  return 0;
}

Output:

Maximum Number from 10 and 20 is 20

In the above code, we have two integer type variables named num1 and num2 and they contain values 10 and 15 respectively. As we have to find out which one is the maximum number among these two variables. We applied a condition num1 >= num2, and according to the result of this condition, our mxNumber will contain the maximum number from these two numbers.

Using Ternary Operator in C

This example demonstrates how to utilize the Ternary Operator in C.

#include
#include

int main()
{
    int num1, num2,mxNumber;
    num1 = 100;
    num2 = 20;
    
    // result = condition ? exp1 : exp2;
    // isn't the if-else block ? just in one line.
    
    mxNumber = num1 >= num2 ? num1 : num2; 
    
    printf("Maximum Number from %d and %d is %d",num1,num2, mxNumber); //output
    return 0;
}

Output:

Maximum Number from 100 and 20 is 100

In the above code, we write down the previous code if-else condition in the form of the ternary operator in c. Using the ternary operator in C, we can easily shorten our code, which is memory efficient also. The working of the above code is the same as the previous example code. So it's totally up to you to either use a simple if-else block or Ternary Operator in c both ways are correct but try to use the ternary operator because it looks neat and clean with a memory-efficient advantage.

Example #2

Find out the given number is even or not using Ternary Operator in C.

#include
#include

int main()
{
    int num = 10;
    
    // condition to check number is even or not
    (num % 2 == 0) ? printf("Number is even") : printf("Number is not even"); 
    
    return 0;
}

Output:

In the above code, we are going to check given number is even or not using the ternary operator in c. To do the same, we applied the condition num%2 == 0 which checks our number and, based on the result, it's going to print the number is even or not.

Which is the conditional operator ternary operator used in C?

Conditional or Ternary Operator (?:) in C/C++ The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.

Which symbols are used in ternary or conditional operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

Which operator in C is called a ternary operator symbol?

It helps to think of the ternary operator as a shorthand way or writing an if-else statement. Here's a simple decision-making example using if and else: int a = 10, b = 20, c; if (a < b) { c = a; } else { c = b; } printf("%d", c); This example takes more than 10 lines, but that isn't necessary.

Which symbols are used in conditional operator?

The conditional operator (? :) is a ternary operator (it takes three operands)..
The first operand is implicitly converted to bool . ... .
If the first operand evaluates to true (1), the second operand is evaluated..