C# example of nested conditional/ternary operator:
// C# program to demonstrate example of
// nested conditional operator
using System;
using System.IO;
using System.Text;
namespace JustTechReview
{
class Test
{
// Main Method
static void Main(string[] args)
{
//finding largest of three numbers
int a;
int b;
int c;
//input numbers
Console.Write("Enter first number : ");
a = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: ");
b = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter third number : ");
c = Convert.ToInt32(Console.ReadLine());
//finding largest number
int large = (a>b)?((a>c)?a:c):(b>c?b:c);
Console.WriteLine("Largest number is {0}", large);
//hit ENTER to exit the program
Console.ReadLine();
}
}
}
Output:
First run:
Enter first number : 30
Enter second number: 20
Enter third number : 10
Largest number is 30
Second run:
Enter first number : 10
Enter second number: 30
Enter third number : 20
Largest number is 30
Third run:
Enter first number : 10
Enter second number: 20
Enter third number : 30
Largest number is 30