C#. Ternary operator?:. Examples of using

Ternary operator ?:. Examples of using

1. Purpose of the operator ?:. General form

To implement the action, depending on the condition, the ?: operator is used. This is the only ternary operator in C #. For its execution, it requires three operands.

The general form of the operator ?:

Condition ? Expression1 : Expression2

here

  • Condition – conditional expression that can take one of two values: true or false;
  • Expression1 – some expression that needs to be executed if the value Condition = true;
  • Expression2 – expression to be executed if the Condition is not satisfied (Condition = false).

 

2. Examples of using the ternary operator ?:

Example 1. Determine the maximum value between two numbers. The text of an application of Console Application type is as follows.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
  class Program
  {
    static void Main(string[] args)
    {
      double x, y;
      double max;

      // 1. Input data
      Console.Write("x = ");
      x = Convert.ToDouble(Console.ReadLine());
      Console.Write("y = ");
      y = Convert.ToDouble(Console.ReadLine());

      // 2. Determination of the maximum value
      //   using ternary operator ?:
      max = x > y ? x : y;

      // 3. Output result
      Console.WriteLine("Maximum = {0:f4}", max);
      Console.ReadKey();
    }
  }
}

Example 2. The radius of the circle and the side of the square are given. Calculate the area of each figure and print the value of the maximum area.

The solution for the application created by the Console Application template:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
  class Program
  {
    static void Main(string[] args)
    {
      double radius, side; // circle radius and side length of a square
      double sCircle, sRect; // area of a circle and square
      double maxSquare;

      // 1. Input data
      Console.Write("Input radius of a circle: ");
      radius = Convert.ToDouble(Console.ReadLine());
      Console.Write("Input side of a rectangle: ");
      side = Convert.ToDouble(Console.ReadLine());

      // 2. Calculate the area of a circle and a square
      sCircle = Math.PI * radius * radius;
      sRect = side * side;

      // 3. Calculate the maximum value from two areas
      //   using ternary operator ?:
      maxSquare = sCircle > sRect ? sCircle : sRect;

      // 3. Output result
      Console.WriteLine("Square of a circle = {0:f3}", sCircle);
      Console.WriteLine("Square of the rectangle = {0:f3}", sRect);
      Console.WriteLine("Maximum = {0:f3}", maxSquare);
      Console.ReadKey();
    }
  }
}

The result of the program

Input radius of a circle: 2.5
Input side of a rectangle: 3.8
Square of a circle = 19.635
Square of the rectangle = 14.440
Maximum = 19.635

 

3. Can an operator ?: be nested?

Yes, it can. The ?: operator can have an arbitrary number of attachments. Numbers are entered from the keyboard. An application of type Console Application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
  class Program
  {
    static void Main(string[] args)
    {
      int a, b, c;
      int max;

      // 1. Input values a, b, c
      Console.Write("a = ");
      a = Convert.ToInt32(Console.ReadLine());
      Console.Write("b = ");
      b = Convert.ToInt32(Console.ReadLine());
      Console.Write("c = ");
      c = Convert.ToInt32(Console.ReadLine());

      // 2. Calculation of the maximum value of three numbers
      max = a > b ? a > c ? a : c : b > c ? b : c;

      // 3. Output result
      Console.WriteLine("Maximum = {0}", max);
      Console.ReadKey();
    }
  }
}

The result of the program

a = 5
b = 3
c = 8
Maximum = 8