Reputation:
so I am pretty new to C# and after learning some fundamentals I tried making a simple program to check for someone's eligibility using if statements. The program asks for 3 things: Age, Height, and CollegeDegree You have to be 18 to go to the next question and you have to be taller than 160cm to go to the CollegeDegree question.
Now the program works fine, however looking at it, I come to realize that there are a lot of nested if statements and the code is just messy and unreadable.
What would I need to do to make it cleaner? I tried just doing it as follows
if
else
if
else
However, that causes the issue that if the first condition is not met, so the age is < 18 it just runs the next if statement instead of making the user be ineligible.
Current Code:
static void Main(string[] args)
{
int ageInput;
int heightInput;
bool hasCollegeDegree;
Console.WriteLine("How old are you?");
ageInput = Convert.ToInt32(Console.ReadLine());
if (ageInput >= 18)
{
Console.WriteLine("You are older than 18");
Console.WriteLine("How tall are you?");
heightInput = Convert.ToInt32(Console.ReadLine());
if (heightInput >= 160)
{
Console.WriteLine("You are taller than 160cm");
Console.WriteLine("Do you have a college degree?");
hasCollegeDegree = Convert.ToBoolean(Console.ReadLine());
if (hasCollegeDegree == true)
{
Console.WriteLine("You have a college degree");
Console.WriteLine("You are eligible");
}
else
{
Console.WriteLine("No College Degree");
Console.WriteLine("You are ineligible");
}
}
else
{
Console.WriteLine("You are too short");
}
}
else
{
Console.WriteLine("You are too young");
}
}
Upvotes: 0
Views: 910
Reputation: 39132
A disadvantage with the answers so far using return
is that the method is exited and therefore you can't have any code below that point in the same method.
A different approach is to use a COMPOUND boolean statement and combine all the conditions into one boolean expression:
if (ageInput >= 18 && heightInput >= 160 && hasCollegeDegree)
{
Console.WriteLine("Congratulations! You meet the criteria.");
}
else
{
Console.WriteLine("You do not meet the criteria.");
}
This style of check might be useful if you have many different combinations that you need to check for. So the user enters all the information up front and then you can have a whole series of different compound checks to see which combinations they satisfy. Maybe you're checking to see which grants from a list that the user qualifies for.
See Boolean logical operators for more information on &&
, ||
, and !
.
Upvotes: 1
Reputation: 186718
I suggest extracting methods to read integer:
private static int ReadInteger(string question) {
while (true) {
if (!string.IsNullOrWhiteSpace(question))
Console.WriteLine(question);
if (int.TryParse(Console.ReadLine(), out int result))
return result;
Console.WriteLine("Sorry, not a valid integer; please, try again.");
}
}
and boolean:
private static HashSet<string> s_Yes =
new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"Y", "Yes", "T", "True", "OK"
};
private static HashSet<string> s_No =
new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"N", "No", "F", "False"
};
private static bool ReadBoolean(string question) {
while (true) {
if (!string.IsNullOrWhiteSpace(question))
Console.WriteLine(question);
string input = Console.ReadLine().Trim();
if (s_Yes.Contains(input))
return true;
if (s_No.Contains(input))
return false;
Console.WriteLine("Sorry, not a valid value; please, try again.");
}
}
then you can put
if (ReadInteger("How old are you?") < 18) {
Console.WriteLine("You are too young");
return;
}
Console.WriteLine("You are older than 18");
if (ReadInteger("How tall are you?") < 160) {
Console.WriteLine("You are too short");
return;
}
Console.WriteLine("You are taller than 160cm");
if (!ReadBoolean("Do you have a college degree (Y/N)?")) {
Console.WriteLine("No College Degree");
Console.WriteLine("You are ineligible");
return;
}
Console.WriteLine("You have a college degree");
Console.WriteLine("You are eligible");
Upvotes: 1
Reputation: 21140
You can exit out of the code using a reverse condition. Example:
Console.WriteLine("How old are you?");
int ageInput = Convert.ToInt32(Console.ReadLine());
if (ageInput < 18)
{
Console.WriteLine("You are too young");
return;
}
Console.WriteLine("You are older than 18");
Console.WriteLine("How tall are you?");
int heightInput = Convert.ToInt32(Console.ReadLine());
if (heightInput < 160)
{
Console.WriteLine("You are too short");
return;
}
Console.WriteLine("You are taller than 160cm");
Console.WriteLine("Do you have a college degree?");
bool hasCollegeDegree = Convert.ToBoolean(Console.ReadLine());
if (!hasCollegeDegree)
{
Console.WriteLine("No College Degree");
Console.WriteLine("You are ineligible");
return;
}
Console.WriteLine("You have a college degree");
Console.WriteLine("You are eligible");
Upvotes: 1