Ternary and Nested Ternary Operators in C#

Have you ever played a game based on a condition? For example let's assume you are playing an interesting game with your friend and if you win the game you will be treated by your friend but if you lose the game then you will also lose the opportunity to have Pizza and a Chocolate Late. I just gave you a real example. Now let's talk in terms of programming. There are very many situations that can develop in a single project that you need to make decisions based upon various conditions and the ternary operator is often useful. It is denoted by a question mark "?" and sometime called a conditional operator.

The following is the basic syntax:

Condition ? "TRUE" : "FALSE"

It is quite clear but let's clarify it with words also to make it more understandable. If "Condition" is true or satisfied then this statement will return the string "TRUE" else it will return the string "FALSE".

Example

In the following example if the Cricket team scores more than 540 then it will win the match otherwise it will be unable to get the trophy.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace TernaryOperator  
  8. {  
  9.   
  10.     class ternary {  
  11.         public int score;  
  12.   
  13.         public ternary(int s)  
  14.         {  
  15.             score = s;  
  16.         }  
  17.     }  
  18.   
  19.   
  20.     class Program  
  21.     {  
  22.         static void Main(string[] args)  
  23.         {  
  24.   
  25.             ternary team = new ternary(580);  
  26.             Console.WriteLine(team.score > 540 ? "Won":"Lost");  
  27.             Console.ReadKey();  
  28.         }  
  29.     }  
  30. }  
Output

program output

Nested Ternary Operators

So far we have introduced the ternary operator in C# to deal with various kinds of simple and complex conditions. Moreover you can also find useful implementations of nested ternary operators in C#.

Do you know what nested actually means? It means putting an object or multiple objects inside a larger one and in technical terminology nested means one logic inside another one logic or one condition in another condition.

Let's explain it in more detail with a real example. Let's assume you have passed your exams and you are waiting for your results. The type of medal indicates your grade that you got from the exam, in other words if you get a White Gold medal then that means your marks are above 90% else if you get a Gold medal then your marks are between 80% and 90% and if you get marks below 80% then you will have a Silver medal. Now let's implement it by coding in C# as in the following:
  1. class NestedTernaryOperator {  
  2.   
  3.     private int marks;  
  4.   
  5.     public int Marks {  
  6.         get {  
  7.             return marks;  
  8.         }  
  9.         set {  
  10.             marks = value;  
  11.         }  
  12.     }  
  13.   
  14.   
  15.   
  16.   
  17. }  
  18. class program {  
  19.   
  20.     static void Main(string[] args) {  
  21.   
  22.         Console.WriteLine("************Nested Ternary Operator in C#*********\n");  
  23.         NestedTernaryOperator student = new NestedTernaryOperator();  
  24.   
  25.         Console.WriteLine("\nEnter Marks that you obtained in your exam ");  
  26.   
  27.   
  28.         student.Marks = Convert.ToInt32(Console.ReadLine());  
  29.   
  30.         string medal = (student.Marks >= 90 && student.Marks <= 100) ? "White gold medal" : (student.Marks >= 80 && student.Marks <= 90) ? "Golden gold medal" : "Silver medal";  
  31.   
  32.         Console.WriteLine("And you deserve a " + medal);  
  33.         Console.ReadLine();  
  34.   
  35.     }  
  36. }  
Output

output

 


Similar Articles