Decision Making C# Language
Decision-making is a fundamental aspect of programming, allowing you to create dynamic and responsive applications. In
Decision-making is a fundamental aspect of programming, allowing you to create dynamic and responsive applications. In
a. If Statement:
The if
statement is used to execute a block of code if a specified condition is true. If the condition is false, the code block is skipped.
int age = 25;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
b. Else Statement:
The else
statement is used in conjunction with an if
statement to execute a different block of code when the condition is false.
int age = 15;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are a minor.");
}
c. Else-If Statement:
You can use the else if
statement to check multiple conditions in a sequence.
int score = 80;
if (score >= 90)
{
Console.WriteLine("You got an A.");
}
else if (score >= 80)
{
Console.WriteLine("You got a B.");
}
else
{
Console.WriteLine("You got a C or lower.");
}
switch
statement is useful when you have a specific value and want to execute different code blocks based on the value.string day = "Monday";
switch (day)
{
case "Monday":
Console.WriteLine("It's the start of the week.");
break;
case "Friday":
Console.WriteLine("TGIF!");
break;
default:
Console.WriteLine("It's an ordinary day.");
break;
}
int age = 22;
string message = (age >= 18) ? "You are an adult" : "You are a minor";
Console.WriteLine(message);
Subscribe to get the latest posts sent to your email.