The C program below was written using if statements but then
The C# program below was written using if statements, but then the author decided to implement the code more efficiently. Take the area that has been block-commented and execute the same functionality as above, in a single line of code, using the conditional operator - https://msdn.microsoft.com/en-us/library/ty67wk28.aspx (Links to an external site.) :
class Program
{
static void Main(string[ ] args)
{
string indicator; >
int input = 0;
/*
if (input > 0)
indicator = \"small positive\";
else
indicator = \"small negative\";
if (System.Math.Abs(input) > 3)
indicator = \"large\";
*/
//Execute same functionality as above in a single line, using conditional operator
<???> = <???> ? <???> : ... //Executes same functionality as above
}
}
Solution
Note:-
Conditional Operator is only for if else condition only.
Program:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string indicator = string.Empty;
int input = 4;
indicator = (input > 0 ? \"small positive\" : \"small negative\");
if (System.Math.Abs(input) > 3)
indicator = \"large\";
Console.WriteLine(indicator);
Console.ReadKey();
}
}
}

