Computer Science, asked by Ashishsharanag5944, 1 year ago

What is ternary operator in C#?

Answers

Answered by saranyaammu3
0

Answer:

Ternary operator is a Conditional operator in C#. It takes three arguments and evaluates a Boolean expression.

For example −

y = (x == 1) ? 70 : 100;

Above, if the first operand evaluates to true (1), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated.

The following is an example −

Example

Live Demo

using System;

namespace DEMO {

  class Program {

   

     static void Main(string[] args) {

         

        int a, b;

        a = 10;

        b = (a == 1) ? 20 : 30;

        Console.WriteLine("Value of b is {0}", b);

        b = (a == 10) ? 20 : 30;

        Console.WriteLine("Value of b is {0}", b);

        Console.ReadLine();

     }

  }

}

Output

Value of b is 30

Value of b is 20

Explanation:

Answered by Anonymous
0

Answer:

The ternary operator is unique to c++. It is used to specify a condition with two values,and if the condition is evaluated to true, then the first value is assigned to a new variable. if the condition is evaluated to be false, then the second value is assigned to a new variable.

syntax:variable=condition? value1:value2

the following program is an example of ternary operators

#include<iostream.h>

void main()

{

clrscr();

int a=5;

int b=8;

int c;

c=(a>b)?a:b;

cout<<"the greater number is"<<c;

getch();

}

output for following program: the greater number is 8

Similar questions