Im learning about else and else if statements and combining them with Try Parse.
Basically, I ask the user to tell me the temperature outside and based on the answer I give him the output. If instead of numbers, he uses the words, Try Parse gives out the reply that the value entered is not a number, BUT, in that case, the last ELSE IF statement which is set to "<15" is still executing! Why is it executing when the input is a string and not a number, shouldnt the code just stop at ''Value entered is not a number'' and stop it, instead for some reason the last bit executes as well. Please take a look and share your opinion. Thanks!
{
Console.WriteLine("Enter the temperature of today: ");
string temperature = Console.ReadLine();
int numTemp;
int number;
if (int.TryParse(temperature, out number))
{
numTemp = number;
}
else
{
numTemp = 0;
Console.WriteLine("Value entered, was no number. 0 set as temperature");
}
if (numTemp > 15)
{
Console.WriteLine("The jacket is completely unncessarry for this temperature.");
}
else if (numTemp == 15)
{
Console.WriteLine("Just the sweater is gonna be perfectly okay for you.");
}
else if (numTemp < 15)
{
Console.WriteLine("You need a jacket, my friend");
}
else
{
Console.WriteLine("The weather is for an apocalypse!");
}
Console.Read();
}
CodePudding user response:
The function does not return at the else branch of the TryParse
.
else
{
numTemp = 0;
Console.WriteLine("Value entered, was no number. 0 set as temperature");
// NO RETURN HERE, THE REMAINDER OF THE FUNCTION IS ALSO EXECUTED.
}
Therefore the execution continues and evaluates the second if statement, there the else if (numTemp < 15)
statement is true and thus that branch will execute.
CodePudding user response:
string temperature = Console.ReadLine();
int num;
if (int.TryParse(temperature, out num))
{
if (num > 15)
{
Console.WriteLine("The jacket is completely unncessarry for this temperature.");
}
else if (num == 15)
{
Console.WriteLine("Just the sweater is gonna be perfectly okay for you.");
}
else if (num < 15)
{
Console.WriteLine("You need a jacket, my friend");
}
else
{
Console.WriteLine("The weather is for an apocalypse!");
}
}
else
{
Console.WriteLine("Value entered, was no number. 0 set as temperature");
}
Console.Read();
}
Here you go, but you have another problem which you might solve easily with a &&. Your The weather is for apocalypse will never get hit, as Num<15 will always be true fore any number lower.
Your problem has nothing to do with previous answer stated here, your Else statement is wrong. As it falls into another category. what you wanted was to have your else statement with your first if. Your statement will always give two answers, as they are both true.
Should user give you a number a real number then, we search the else if. If not we just use the else statement.
CodePudding user response:
Few points to be noted here.
Assignment num = 0
is unnecessary here, because the default value of int is 0.
And your else
is also useless because any integer can be greater or equal or lesser than itself.
And your main issue is because because num
will be 0 and it’s < 15. You should add these comparison checks when the TryParse method returns true