Reputation: 1
I am working on a BMI calculation assignment using the If-else statements. I get an error
CS1525 unexpected symbol 'else'
and warning
CS0642: Possible mistaken empty statement
This is a BMI calculation in C#
bmi = (weight * 703) / height^2;
if (bmi < 18.5);
{
Console.WriteLine("Underweight");
else if (bmi <= 25)
Console.WriteLine("Optimal");
else if (bmi > 25);
Console.WriteLine("Overweight");
}
Mono C# compiler version 4.6.2.0
mcs -out:main.exe main.cs
main.cs(20,5): warning CS0642: Possible mistaken empty statement
main.cs(23,6): error CS1525: Unexpected symbol `else'
main.cs(27,13): warning CS0642: Possible mistaken empty statement
Compilation failed: 1 error(s), 2 warnings
compiler exit status 1
Upvotes: 0
Views: 1193
Reputation: 90
Can you please update the code as below and try -
bmi = (weight * 703) / Math.Pow(height,2);
if (bmi < 18.5) Console.WriteLine("Underweight");
else if (bmi <= 25) Console.WriteLine("Optimal");
else if (bmi > 25) Console.WriteLine("Overweight");
Upvotes: 0
Reputation: 350
I dont have Mono compiler, but try this:
bmi = (weight * 703) / Math.Pow(height,2);
if (bmi < 18.5)
{
Console.WriteLine("Underweight");
}
else if (bmi <= 25)
{
Console.WriteLine("Optimal");
}
else if (bmi > 25)
{
Console.WriteLine("Overweight");
}
Upvotes: 2
Reputation:
You have a semicolon right in front of the if
statement. In languages like C or C++ it would mean that the if statement would do nothing but C# treats it as an error. Because the code inside the curly braces does not belong to the if
statement, because of the semicolon, the else
keyword can't be there.
Also, your else if
s should be after closing the curly braces
Upvotes: 0