Sunday, 6 December 2015

Ternary Operator in C#

Ternary Operator in C#

Ternary operator:-
The Ternary operator is not a type of conditional statement but it is also used to check the conditions. It returns only one value based on conditions. Use the ternary operator to check many values in a single line of coding.

Syntax:-

Variable = condition ? True Part : False Part;





Conditional Operator:-
The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator.

Syntax:-

Variable = condition ? first_expression : second_expression;


Source Code :-

static void Main(string[] args)   
{  
    int a, b, Value;  
    a = 100;  
    b = 50;  
    Value = a > b ? a : b ;  
    Console.WriteLine("Greater Value:" + Value);  
    Console.Read();  
} 

Output:-
      Greater Value:100          





No comments:

Post a Comment

Factorial of a Number

Recently Viewed