Understanding C# If-Else Statements: A Beginner's Guide
In C# programming, decision-making based on conditions is a fundamental aspect of creating dynamic and responsive applications. This is where if-else statements come into play. An if-else statement in C# allows a program to execute different blocks of code depending on whether a specified condition is true or false.
This guide, inspired by the video, ‘C# for Beginners Part 2 - Learn how to write an 'if-else' statement and a 'while' loop’ by LaylaCodesIt, will introduce you to the basics of the if-else statement in C#, the use of conditional and logical operators in writing boolean conditions, demonstrating how to make your programs interact intelligently with user input.
Introducing Conditional Logic
Conditional logic allows a program to make decisions based on certain conditions. Layla’s video introduces the concept and syntax of the if-else statement, an example of conditional logic, which executes different blocks of code depending on whether a specified condition is true or false.
In the following code example, Layla codes a greeting message to ask if the user likes cheese:
// Asking the user a question and storing their response
Console.WriteLine($"Hello {name}, do you like cheese?");
string doTheyLikeCheese = Console.ReadLine();
// Asking the user a question and storing their response
Console.WriteLine($"Hello {name}, do you like cheese?");
string doTheyLikeCheese = Console.ReadLine();
Simple If-Else Statement in C#
Then, this if-else conditional statement checks the user’s answer and changes its output response depending on what the user said. The condition is built using conditional operators and based on its result, the statements inside if-else blocks are executed. Here’s a simple example with a single line statement:
// Checking the user's response and providing appropriate output
if (doTheyLikeCheese == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}
// Checking the user's response and providing appropriate output
if (doTheyLikeCheese == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}
If the user said ‘yes’, the code inside the if statement will display a message offering a lump of cheese. If the user responds with anything other than ‘yes’, then the else block will output "Here is a lump of coal."
Case Sensitivity Issue
With a basic if-else statement, a common issue can occur: case sensitivity. If the user types ‘Yes’ instead of ‘yes’, the program does not recognize it as valid input. To resolve this, Layla uses the ToLower()
method, which automatically converts the user’s input to lowercase and avoids the case sensitivity issue.
// Using ToLower() to handle case sensitivity in user input
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}
// Using ToLower() to handle case sensitivity in user input
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}
Handling Multiple Conditions
Layla then explains how to handle more than two possible responses using the else-if statement. Here, the program checks for multiple conditional statements - if the user responds with ‘yes’, ‘no’, or anything else.
// Checking multiple conditions using else-if
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
}
else
{
Console.WriteLine("Please give a yes or no answer.");
}
// Checking multiple conditions using else-if
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
}
else
{
Console.WriteLine("Please give a yes or no answer.");
}
Like before, if the boolean expression in the if statement is true, the program offers a lump of cheese if the user says ‘yes’. However, instead of offering a lump of coal to any other response, in the else-if statement, it now only does so when the user responds with ‘no’. If the user responds with anything other than ‘yes’ or ‘no’, the else part of the program will ask the user to provide a yes or no answer. Let's see the result of the above code in action: (Image can be improved)
Using Loops
To ensure that the user provides a valid response, loops are introduced. A loop can repeatedly ask the user for input until they provide a correct answer. This is called a ‘while’ loop, and the following example shows how it works:
// Using a while loop to repeatedly ask for valid input
bool hasAnswered = false;
while (!hasAnswered)
{
// Checking the user's response and setting hasAnswered to true for valid responses
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
hasAnswered = true;
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
hasAnswered = true;
}
else
{
Console.WriteLine("Please give a yes or no answer.");
doTheyLikeCheese = Console.ReadLine();
}
}
// Using a while loop to repeatedly ask for valid input
bool hasAnswered = false;
while (!hasAnswered)
{
// Checking the user's response and setting hasAnswered to true for valid responses
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
hasAnswered = true;
}
else if (doTheyLikeCheese.ToLower() == "no")
{
Console.WriteLine("Here is a lump of coal.");
hasAnswered = true;
}
else
{
Console.WriteLine("Please give a yes or no answer.");
doTheyLikeCheese = Console.ReadLine();
}
}
In the above example, the loop continues to prompt the user until they respond with ‘yes’ or ‘no’. Here you can see it in action when the code executes, from Layla’s video:
If-Else Statement - Recap
To recap:
- An if-else statement can make decisions in your code
- You can handle multiple conditions with else-if statements
- Loops ensure that users respond correctly
Conditional logic is fundamental to the C# programming language, so it’s best to practice these concepts and start including them in your code. And this is just the beginning. If statement, else statement, else-if ladder, if-else within if-else also called nested if-else, switch statement - conditional logic can dramatically change your program and there is plenty to learn.
Conclusion
And voila! That’s the beginner’s guide to if-else statements and while loops. As mentioned before, this guide was inspired by LaylaCodesIt - check out her channel for video demonstrations of everything above and for more information on C# programming.
In the meantime, check out our other resources where she shares our favorite creators, tutorials, and learning resources to level up your C# skills. Happy coding!