The program below was written using if statements, but then the author decided t
ID: 3773373 • Letter: T
Question
The 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)
{
if (inputB > 3)
indicator = "big";
else if (inputA > 3)
indicator = "small";
if ((inputA < 4) &&(inputB < 4))
indicator = "tiny";
//Execute same functionality as above in a single line, using conditional operator
<???>=< ???>?<???> : ... //Executes same functionality as above
}
}
Explanation / Answer
Answer:
Here are the two ways of writing the code. The required code is highlighted in bold letters.
Program code to copy:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfConditionSingleStatement
{
class Program
{
static void Main(string[] args)
{
int inputB, inputA;
inputB = 3; inputA = 3;
string indicator = "";
indicator = inputB > 3 ? "big" : (inputA > 3 ? "small" : ((inputA<4 && inputB < 4)? "tiny": "null"));
Console.WriteLine(indicator);
Console.ReadKey();
}
}
}
Sample Output:
tiny
or
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfConditionSingleStatement
{
class Program
{
static void Main(string[] args)
{
int inputB, inputA;
inputB = 3; inputA = 3;
string indicator = "";
indicator = inputB > 3 ? "big" : inputA > 3 ? "small": "" ;
indicator = (inputA <4 && inputB < 4)? "tiny": "null";
Console.WriteLine(indicator);
Console.ReadKey();
}
}
}
Sample Output:
tiny
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.