Decision Making
Introduction to Decision Making in C#
Decision making in programming refers to the process of deciding the order in which statements will be executed based on certain conditions. In C#, we use control statements to manage the flow of execution of the program. This is a key concept in any programming language, as it allows our code to make decisions and perform different actions depending on various inputs or conditions.
Types of Decision Making Statements in C#
There are several types of decision making statements in C#, including:
if
statementif-else
statementnested if
statementswitch
statementternary
operator
Let's take a closer look at each of these.
The if
Statement
The if
statement is the simplest form of control statement. It is used to execute a block of code if a certain condition is true.
int x = 10;
if (x > 5)
{
Console.WriteLine("x is greater than 5");
}
In this example, the message will be printed to the console because the condition x > 5
is true.
The if-else
Statement
The if-else
statement extends the if
statement to execute a block of code if the condition is true, and another block of code if it is false.
int x = 10;
if (x > 15)
{
Console.WriteLine("x is greater than 15");
}
else
{
Console.WriteLine("x is not greater than 15");
}
In this example, the "else" message will be printed because the condition x > 15
is false.
The nested if
Statement
A nested if
statement is an if
statement within another if
statement. It allows us to check for multiple conditions and run a particular block of code when a specific condition is met.
int x = 10;
if (x > 5)
{
if (x < 15)
{
Console.WriteLine("x is between 5 and 15");
}
}
In this example, the message will be printed because both conditions are true.
The switch
Statement
The switch
statement is used when we have multiple conditions to check. It is an alternative to the if-else
statement when dealing with many conditions.
int x = 2;
switch (x)
{
case 1:
Console.WriteLine("x is 1");
break;
case 2:
Console.WriteLine("x is 2");
break;
default:
Console.WriteLine("x is not 1 or 2");
break;
}
In this example, the message "x is 2" will be printed.
The ternary
Operator
The ternary
operator, also known as the conditional operator, is a shorthand way of writing an if-else
statement. It consists of a condition followed by a question mark (?), the code to execute if the condition is true, a colon (:), and the code to execute if the condition is false.
int x = 10;
string result = (x > 5) ? "x is greater than 5" : "x is not greater than 5";
Console.WriteLine(result);
In this example, the message "x is greater than 5" will be assigned to the result
variable.
Conclusion
Decision making in C# is an essential concept that allows a program to respond differently to different inputs or conditions. By understanding and using if
, if-else
, nested if
, switch
statements, and ternary
operator, you can control the flow of execution in your program and make your code more flexible and powerful. Happy coding!