C# - 決策製定
Hello there, future programmers! Today, we're going to dive into one of the most crucial aspects of programming: decision making. In C#, as in life, we often need to make choices based on certain conditions. Let's explore how we can teach our programs to make decisions!
Understanding Decision Making in C
Imagine you're a robot (which, in a way, is what we're programming). You need clear instructions on what to do in different situations. That's exactly what decision making in programming is all about!
The if Statement
The if
statement is the most basic form of decision making in C#. It's like asking a simple yes or no question.
int age = 18;
if (age >= 18)
{
Console.WriteLine("You can vote!");
}
In this example, if the age is 18 or more, the program will print "You can vote!". If not, nothing happens. Simple, right?
The if-else Statement
But what if we want to do something when the condition is false? That's where the if-else
statement comes in handy.
int temperature = 25;
if (temperature > 30)
{
Console.WriteLine("It's hot outside!");
}
else
{
Console.WriteLine("The weather is pleasant.");
}
Here, if the temperature is above 30, it's hot. Otherwise, it's pleasant. Our program now has two possible outcomes!
The else if Statement
Life isn't always black and white, and neither is programming. Sometimes we need to check multiple conditions. Enter the else if
statement.
int score = 75;
if (score >= 90)
{
Console.WriteLine("A");
}
else if (score >= 80)
{
Console.WriteLine("B");
}
else if (score >= 70)
{
Console.WriteLine("C");
}
else
{
Console.WriteLine("Needs improvement");
}
This grading system checks multiple score ranges and assigns the appropriate grade. It's like a cascade of decisions!
The switch Statement
When you have many specific cases to check, the switch
statement can make your code cleaner and more efficient.
int dayNumber = 3;
switch (dayNumber)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
// ... other days ...
default:
Console.WriteLine("Invalid day number");
break;
}
The switch
statement is great for when you have a variable that could have many specific values, like days of the week.
The Ternary Operator (?:)
Now, let's talk about a nifty little operator that can make your code more concise: the ternary operator. It's like a shorthand if-else
statement.
int number = 7;
string result = (number % 2 == 0) ? "Even" : "Odd";
Console.WriteLine(result);
This one-liner checks if the number is even. If it is, result
becomes "Even", otherwise it's "Odd". It's a bit like asking, "Is this true? If yes, do this; if no, do that."
Syntax Breakdown
Let's break down the ternary operator:
condition ? expression1 : expression2
- If
condition
is true, the operator returnsexpression1
- If
condition
is false, it returnsexpression2
When to Use the Ternary Operator
The ternary operator is great for simple, one-line decisions. It can make your code more readable, but be careful not to overuse it. For complex conditions, stick to regular if-else
statements.
Here's another example:
int age = 20;
string canVote = (age >= 18) ? "Yes, can vote" : "No, cannot vote";
Console.WriteLine(canVote);
This checks if someone can vote based on their age. Clean and simple!
Comparison of Decision-Making Methods
Let's summarize our decision-making tools in a handy table:
Method | Use Case | Pros | Cons |
---|---|---|---|
if | Simple conditions | Easy to understand | Can get messy with multiple conditions |
if-else | Two-way decisions | Covers both true and false scenarios | Limited to two options |
else if | Multiple related conditions | Can handle many conditions | Can be verbose for many options |
switch | Many specific cases | Clean for many options | Less flexible than if-else chains |
Ternary | Simple, one-line decisions | Concise | Can be hard to read if overused |
Conclusion
Decision making is at the heart of programming. It's what gives our programs the ability to respond differently based on various conditions. Whether you're using a simple if
statement, a complex switch
, or the concise ternary operator, you're teaching your program to think!
Remember, the key to mastering decision making in C# (and programming in general) is practice. Try writing programs that use different decision-making structures. Maybe create a simple game that asks questions and responds based on the user's input. Or write a program that gives weather advice based on temperature and conditions.
As you practice, you'll start to develop an intuition for which decision-making tool to use in different situations. And before you know it, you'll be making decisions in your code as easily as you decide what to have for breakfast!
Happy coding, future C# masters! Remember, every great programmer started exactly where you are now. Keep practicing, stay curious, and most importantly, have fun with your coding journey!
Credits: Image by storyset