Construction if...else
Conditional constructs are one of the basic components of many programming languages, which direct the program operation along one of the paths depending on certain conditions. One of such constructs in the C# programming language is the if..else construct
The if/else construct checks if some condition is true and executes some code depending on the results of the check.
Its simplest form consists of an if block:
if(condition)
{
executable instructions
}
A condition is placed after the if keyword. The condition must represent a value of the bool type. It can be a value of the bool type directly or the result of a conditional expression or another expression that returns a value of the bool type. And if this condition is true (equal to true), then the code that is placed further after the condition inside curly braces is triggered.
For example:
int num1 = 8;
int num2 = 6;
if(num1 > num2)
{
//true
}
In this case, we have the first number greater than the second, so the expression num1 > num2 is true and returns true, hence control passes to the line Console.WriteLine(“The number {num1} is greater than the number {num2}”);
If the if block contains a single instruction, we can shorten it by removing the curly braces:
int num1 = 8;
int num2 = 6;
if (num1 > num2)
//true
// or like this
if (num1 > num2) //false
We can also combine several conditions at once using logical operators:
int num1 = 8;
int num2 = 6;
if(num1 > num2 && num1==8)
{
//false
}
In this case, the if block will be executed if num1 > num2 is true and num1==8 is true.
The else expression
But what if we want to have some action to be performed when the condition is not met as well? In this case, we can add a else block:
int num1 = 8;
int num2 = 6;
if(num1 > num2)
{
//true
}
else
{
//false
}
The else block is executed if the condition after if is false, i.e. equal to false. If the else block contains only one instruction, we can shorten it by removing curly braces:
int num1 = 8;
int num2 = 6;
if(num1 > num2)
//true
else
//false
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 num1 = 8;
int num2 = 6;
if(num1 > num2)
{
//true
}
else if (num1 < num2)
{
//false
}
else
{
//
}
If necessary, you can add more than one else if expression:
string name = “Alex”;
if (name == “Tom”)
//false
else if (name == “Bob”)
//false
else if (name == “Mike”)
//false
else
//true
Ternary operation
The ternary operation also allows you to check some condition and, depending on its truth, perform some actions. It has the following syntax:
[first operand - condition] ? [second operand] : [third operand].
There are three operands here at once. Depending on the condition, the ternary operation returns the second or third operand: if the condition is true, the second operand is returned; if the condition is false, the third operand is returned. For example:
int x=3;
int y=2;
int z = x < y ? (x+y) : (x-y);
Here, the first operand (i.e., the condition) represents the expression x < y. If it is true, the second operand, (x+y), that is, the result of the addition operation, is returned. If the condition is false, the third operand - (x-y) - is returned.
The result of the ternary operation (i.e. the second or third operand depending on the condition) is assigned to the variable z.