Now you’ll probably say ‘Teacher, again with coding?’ Yes, again with coding! But this time, it’s not something fancy, just aimed at strengthening the fundamentals. Like when you start a new game and learn how to move your character, this is kind of like those initial movement commands in coding. Honestly, especially if you’re like me, an old-school blog author, it might seem a bit intimidating at first, but trust me, once you understand the logic, you’ll be saying ‘Wow!’ In fact, recently a friend of mine made a simple calculator program that does addition, subtraction, etc. But he was stuck somewhere, constantly giving errors. I said ‘We’ll fix it, let me take a look.’ Turns out, he had incorrectly set up an ‘if-else’ structure, comparing texts instead of numbers. Funny but true, believe me, such things happen.
Anyway, let’s get back to our topic. What are these ‘control structures’ we talk about? Basically, they let our program follow different paths based on specific conditions. Like stopping at a red light in traffic and going when it turns green, the program behaves similarly. If this is true, do this; otherwise, do that.
Let’s start with the ‘if-else’ structure. This is one of the basic building blocks in programming. When you need to make a decision, and you have two options. If A is true, do this; if not, do the other. For example, if someone’s game character’s health drops below zero, display ‘Game Over’, otherwise, keep playing. Here, ‘if’ kicks in.
Suppose we take an example: we will check a student’s grade to see if they pass or fail. If the grade is greater than or equal to 50, say ‘Passed!’; otherwise, say ‘Failed!’. This is a simple scenario. In real life, situations can be more complex. For instance, you might check both the grade and attendance. If both conditions are met—grade above 50 and attendance below a certain number—you’ll say ‘Successful!’; otherwise, ‘Unsuccessful!’. The ‘if-else if-else’ structure comes into play here. You check conditions in sequence. If the first is true, do it; if not, check the next. This logic probably sounds familiar to you.
Now, there’s also ‘switch-case’, which offers an alternative. It’s very useful when you have multiple options. For example, if you’ve prepared a menu where pressing 1 executes A, 2 executes B, and 3 executes C. You can do this with ‘if-else if-else’, but ‘switch-case’ makes it more readable and sometimes more efficient. Instead of multiple ‘else if’ statements, you handle them with a single ‘switch’ block. And, of course, there’s a ‘default’ case to cover unexpected inputs. It’s nice, right? It prevents the program from being confused about what to do.
There’s also the ‘break’ statement in this context. In switch-case, if you don’t put ‘break’ at the end of each ‘case’ block, the program will fall through to the next case. So, if you press 1, it might perform both A and B operations, which causes confusion, like in that friend’s calculator example! That’s why ‘break’ is very important. It stops the execution at the end of each case.
Now, let’s look at practice. Even for a developer like me, these structures are indispensable. For example, I might check if a button is active using ‘if’, or display different menus depending on a user’s role. Recently, in a project, I needed to verify a value received from the user. If the value is 1, do this; if 2, do that; if 3, do another thing. Here, ‘switch-case’ came to the rescue. It made my code cleaner and easier to understand. Previously, I would have used multiple ‘else if’ statements, and then I’d wonder ‘What is this mess?’
Let me show you a simple example code. This program will ask the user for a day name and tell whether it’s a weekday or weekend. I generally prefer ‘switch-case’ for such cases, but you can choose whichever you prefer. Just remember to include the ‘break’ statement! Otherwise, it might cause unexpected behaviors.
First, with ‘if-else’:
// Expression: Check whether input day name is a weekday or weekend // INCORRECT: When comparing strings, be careful with case sensitivity // Because 'Monday' and 'monday' could be treated differently.
string dayName = "Monday"; // Assume this value is received from the user.
if (dayName.ToLower() == "saturday" || dayName.ToLower() == "sunday") { Console.WriteLine("Weekend!"); } else if (dayName.ToLower() == "monday" || dayName.ToLower() == "tuesday" || dayName.ToLower() == "wednesday" || dayName.ToLower() == "thursday" || dayName.ToLower() == "friday") { Console.WriteLine("Weekday."); } else { Console.WriteLine("You entered an invalid day name."); }
And now, the same with ‘switch-case’. Notice how much cleaner and more organized it looks. The ‘||’ (or) operators are replaced with combined cases.:
// CORRECT: using switch-case for cleaner and more understandable control
string dayName = "Monday"; // Assume this value is received from the user.
switch (dayName.ToLower()) { case "saturday": case "sunday": Console.WriteLine("Weekend!"); break; // Important! Exit the block after.
case "monday": case "tuesday": case "wednesday": case "thursday": case "friday": Console.WriteLine("Weekday."); break; // Important! Exit the block after.
default: Console.WriteLine("You entered an invalid day name."); break; }
See? Both do the same, but ‘switch-case’ looks more tidy. It’s more organized than stacking many ‘||’ operators. Also, the logic is similar across many languages, like Python, which has similar structures.
In conclusion, these ‘if-else’ and ‘switch-case’ structures give intelligence to our programs. They turn them from mere command sequences into entities capable of decision-making, reacting differently depending on the situatie. Believe me, once you build these fundamentals solidly, everything else will come easily—like a stack of dominoes. Well, you get the idea 🙂
By the way, if you’d like to see more examples, you can search on Google for ‘control structures examples C#’. There are plenty of resources. Also, YouTube has fantastic explanations, which I sometimes watch for revisiting concepts. Recently, I watched a video that explained different scenarios quite effectively, very helpful. If you want to check it out, search ‘control structures C#’ on YouTube.
Remember, programming is a journey. Every new structure you encounter on this journey will carry you further. Take care of yourself and keep coding!