Inner conditional operator can be used in any block,it shows like (a>b)?((a>c)?a:c):(b>c?b:c). Here we input three numbers and finding the largest number using nested conditional operator.
Syntax:
(logical_test1) ?
((logical_test2)? True_block : false_block) :
false_block_outer;
By the above conditional operator, it checks the condition one by one if it is true then “true_block” executes, else “false_block” executes and if first is false then “false_block_outer” will be executed.
Example:
Input: Enter first number : 23
Enter second number: 45
Enter third number : 87
// check by using this method
// (a>b)?((a>c)?a:c):(b>c?b:c);
Output: Largest number is 87
Example 1:
C#
// C# program to illustrate example of // nested conditional operator using System; using System.IO; using System.Text; namespace Geeks { class GFG { // Main Method static void Main(string[] args) { // to find largest of three numbers int a; int b; int c; // to 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()); // to find 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(); } } } |
Input:
Enter first number : 23 Enter second number: 45 Enter third number : 87
Output:
Enter first number : 23 Enter second number: 45 Enter third number : 87 Largest number is 87
Example 2:
C#
// C# program to illustrate example of // nested conditional operator using System; using System.IO; class GFG { // Main Method static void Main(string[] args) { // to find largest of three numbers int a; int b; int c; // to 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()); // to find largest number int large = (a > b) ? ((a > c) ? a : c) : (b > c ? b : c); Console.WriteLine("Largest number is {0}", large); } } |
Input:
Enter first number : 365 Enter second number: 123 Enter third number : 638
Output:
Enter first number : 365 Enter second number: 123 Enter third number : 638 Largest number is 638
Recommended Posts:
- C# Decision Making (if, if-else, if-else-if ladder, nested if, switch, nested switch)
- Nested Classes in C#
- How to get the Elements of the Nested ValueTuple in C#?
- Program to swap numbers using XOR operator in C#
- C# | Operator Overloading
- LINQ | Filtering Operator | OfType
- C# | as Operator Keyword
- C# | is Operator Keyword
- Is vs As operator keyword in C#
- typeof Operator Keyword in C#
- Null-Coalescing Assignment Operator in C# 8.0
- LINQ | Set Operator | Except
- LINQ | Element Operator | Last
- LINQ | Projection Operator | Select
- LINQ | Projection Operator | SelectMany
- LINQ | Filtering Operator | where
- LINQ | Sorting Operator | OrderBy
- LINQ | Sorting Operator | OrderByDescending
- LINQ | Sorting Operator | Reverse
- LINQ | Sorting Operator | ThenBy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

