Getting an error that says } is expected after the first console read line. what should i do?
Getting an error that says } is expected after the first console read line. what should i do?
I'm new to this any pointers would be helpful thanks..
int salary = 4000;
if (salary >= 4000)
Console.WriteLine(salary);
Console.ReadLine();
else
Console.WriteLine("Minimum+salary+requirement+not+met");
Console.ReadLine();
Console.WriteLine(salary);
else
2 Answers
2
Curly braces are technically optional if the statement following is a one liner, however it's a best practice to always use braces due to the situation you have. The current flow of yours is (with braces):
int salary = 4000;
if (salary >= 4000)
Console.WriteLine(salary)
Console.ReadLine();
else
Console.WriteLine("Minimum+salary+requirement+not+met");
Console.ReadLine();
As you can see your else
statement is not paired to its required if
statement.
else
if
The proper format for this would have been
int salary = 4000;
if (salary >= 4000)
Console.WriteLine(salary)
Console.ReadLine();
else
Console.WriteLine("Minimum+salary+requirement+not+met");
Console.ReadLine();
There can be no other statements in between an if/else
set. So, as a reminder, always use curly braces!
if/else
Just to add extra info, if you are going to perform a Console.ReadLine()
no matter the result of the if
and else
, then you better use it outside the brackets.
Console.ReadLine()
if
else
int salary = 4000;
if (salary >= 4000)
Console.WriteLine(salary);
else
Console.WriteLine("Minimum+salary+requirement+not+met");
Console.ReadLine();
That way you can skip using brackets and your code will still work
int salary = 4000;
if (salary >= 4000)
Console.WriteLine(salary);
else
Console.WriteLine("Minimum+salary+requirement+not+met");
Console.ReadLine();
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Without curly braces, the only line associated with your if statement is
Console.WriteLine(salary);
. So, you have an if statement, a separate statement, and then anelse
, which isn't paired with anything. Taking a look at some examples or a textbook should help you get a better handle on the syntax. This is pretty fundamental stuff.– jonsca
Sep 15 '18 at 2:18