DEV Community

Cover image for if-else construct and ternary operation C#
Zafar Urakov
Zafar Urakov

Posted on • Updated on

if-else construct and ternary operation C#

Conditional constructs are one of the basic components of many programming languages, which direct the work of the program along one of the paths depending on certain conditions. One such construct in the C# programming language is the if..else construct.

The if/else construct checks the truth of a certain condition and, depending on the results of the check, executes a certain code.

Ее простейшая форма состоит из блока if:

if(condition)
{
   executable instructions
}
Enter fullscreen mode Exit fullscreen mode

The if keyword is followed by a condition. The condition must represent a bool value. This can be a bool value itself, or the result of a conditional expression or another expression that returns a bool value. And if this condition is true (equal to true), then the code that is placed further after the condition inside curly braces works.

For example:

int firstNumber = 8;
int secondNumber = 6;

if(firstNumber > secondNumber)
{
   Console.WriteLine($”The number {firstNumber} is greater than the number {secondNumber});
}
Enter fullscreen mode Exit fullscreen mode

In this case, we have the first number greater than the second, so the expression firstNumber > secondNumber is true and returns true, therefore, control passes to the line Console.WriteLine("The number {firstNumber} is greater than the number {secondNumber}");

If the if block contains a single statement, then we can shorten it by removing the curly braces:

int firstNumber = 8;
int secondNumber = 6;
if (firstNumber > secondNumber)
    Console.WriteLine($"The number {firstNumber} is greater than the number {secondNumber}");

// or so
if (firstNumber > secondNumber) Console.WriteLine($"The number {firstNumber} is greater than the number {secondNumber}");
Enter fullscreen mode Exit fullscreen mode

We can also connect several conditions at once using logical operators:

int firstNumber = 8;
int secondNumber = 6;
if(firstNumber > secondNumber && firstNumber == 8)
{
    Console.WriteLine($"The number {firstNumber} is greater than the number {secondNumber}");
}
Enter fullscreen mode Exit fullscreen mode

In this case, the if block will be executed if firstNumber > secondNumber is true and firstNumber == 8 is true.

else expression

But what if we want something to be done if the condition is not met? In this case, we can add an else block:

int firstNumber = 8;
int secondNumber = 6;
if(firstNumber > secondNumber)
{
    Console.WriteLine($"The number {firstNumber} is greater than the number {secondNumber}");
}
else
{
    Console.WriteLine($"The number {firstNumber} is less than the number {secondNumber}");
}
Enter fullscreen mode Exit fullscreen mode

else if

But in the example above, when comparing numbers, we can count three states: the first number is greater than the second, the first number is less than the second, and the numbers are equal. Using the else if construct, we can handle additional conditions:

int firstNumber = 8;
int secondNumber = 6;
if(firstNumber > secondNumber)
{
    Console.WriteLine($"The number {firstNumber} is greater than the number {secondNumber}");
}
else if (firstNumber < secondNumber)
{
    Console.WriteLine($"The number {firstNumber} is less than the number {secondNumber}");
}
else
{
    Console.WriteLine("The number firstNumber is equal to the number secondNumber");
}
Enter fullscreen mode Exit fullscreen mode

Ternary operation

The ternary operation also allows you to check a certain condition and, depending on its truth, perform some actions. It has the following syntax:

[first operand - condition] ? [second operand] : [third operand]
Enter fullscreen mode Exit fullscreen mode

There are three operands here. Depending on the condition, the ternary operation returns the second or third operand: if the condition is true, then the second operand is returned; if the condition is false, then the third one. For example:

int firstNumber = 3;
int secondNumber = 2;

int result = firstNumber < secondNumber ? (firstNumber + secondNumber) : (firstNumber - secondNumber);
Console.WriteLine(result); // 1
Enter fullscreen mode Exit fullscreen mode

Here, the first operand (that is, the condition) represents the expression firstNumber < secondNumber. If it is true, then the second operand is returned - (firstNumber + secondNumber), that is, the result of the addition operation. If the condition is false, then the third operand is returned - (firstNumber - secondNumber).

The result of the ternary operation (that is, the second or third operand depending on the condition) is assigned to the variable result.

My GitHub

References:
1.Microsoft

Top comments (0)