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();






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 an else, 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



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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)