Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The C# program below was written using if statements, but then the author decide

ID: 3782661 • Letter: T

Question

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
}

}

Explanation / Answer

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();
}


}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote