Ternary operator - DotNet and DataBase

Friday, 6 July 2018

Ternary operator






Suppose we want to compare two value, obviously we go for a if statement.
In the same way a Ternary operator is a conditional operator which we use similar to a traditional if/if-else statement.


Syntax
------------

datatype Variable= condition true value : false value;


Suppose we want to categorize people like "Young, middle" age based on their age group, the traditional if condition we write as below

namespace TernaryOperator
{
    class Program
    {
        static void Main(string[] args)
        {
            string ageGroup = "10-50";
            string group = "";
            if(ageGroup == "10-40")
            {
                group = "Young";           
            }
            else
            {
                group = "Middle";
            }
            Console.WriteLine(group);
            Console.Read();
        }
    }
}

It returns us the output based on the age group  supplied.
To get the same output, using ternary operator we can achieve as below.

namespace TernaryOperator
{
    class Program
    {
        static void Main(string[] args)
        {
            string ageGroup = "10-50";
            string group = "";
       
             group = ageGroup == "10-40" ? "Young" : "Middle";
       
            Console.WriteLine(group);
            Console.Read();
        }
    }
}

Here "ageGroup == "10-40""' is the condtion, "?" equals to the keyword if and ":" equals to else.
In this way we can achieve the same output which we get in a simple if-else statement.
Let us suppose we have to write a if statement which has multiple if statements as below,

namespace TernaryOperator
{
    class Program
    {
        static void Main(string[] args)
        {
            string ageGroup = "10-40";
            string group = "";
            if(ageGroup == "10-40")
            {
                group = "Young";           
            }
            else if (ageGroup == "41-60")
            {
                group = "Middle";
            }
            else if (ageGroup == "61-80")
            {
                group = "Senior citigen";
            }
            else
            {
                group = "Old";
            }
            Console.WriteLine(group);
            Console.Read();
        }
    }
}

Now, we will see how to get the same output using ternary operator.


 namespace TernaryOperator
{
    class Program
    {
        static void Main(string[] args)
        {
            string ageGroup = "81";
            string group = "";
       
group = ageGroup == "10-40" ? "Young" : ageGroup == "41-60"? "Middle": ageGroup == "61-80" ? "Senior citigen" : "Old";
            Console.WriteLine(group);
            Console.Read();
        }
    }
}

Practice

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.