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#, use of conditional and logical operators in writing boolean condition, 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:
Console.WriteLine($"Hello {name}, do you like cheese?");
string doTheyLikeCheese = Console.ReadLine();
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 build using conditional operators and based on its result the statements inside if-else block are executed.
Here’s a simple example with single line statement:
if (doTheyLikeCheese == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}
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’, then 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 in else block "Here is a lump of coal" statement works offering 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.
if (doTheyLikeCheese.ToLower() == "yes")
{
Console.WriteLine("Here is a lump of cheese.");
}
else
{
Console.WriteLine("Here is a lump of coal.");
}
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.
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.");
}
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, under the if statement if the boolean expression 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 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’, in else part the program will ask the user to provide a yes or no answer. Let's see result of above code in action: (Image can be better)
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:
bool hasAnswered = false;
while (!hasAnswered)
{
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();
}
}
bool hasAnswered = false;
while (!hasAnswered)
{
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!