What is wrong with my code?

Prompt user for hourly rate. If less than $5.65 or greater than $49.99 display error message.

{
            //declare variables
            const double HIGH = 49.99;
            const double LOW = 5.65;
            double HourlyPayRate



            //input
            Console.Write("Enter your hourly pay rate:");
            HourlyPayRate = Convert.ToDouble (Console.ReadLinne());

            //output
            if (HourlyPayRate > HIGH);
            Console.WriteLine ("The amount entered is to much");

            else
            if (HourlyPayRate < LOW);
            Console.WriteLine (The amount you entered is to small");
            
        }

Recommended Answers

All 8 Replies

#
if (HourlyPayRate > HIGH)               // not end of statement 
#
Console.WriteLine ("The amount entered is to much");
#
 
#
else if (HourlyPayRate < LOW)     //same
#
Console.WriteLine (The amount you entered is to small");

sotake out the ; because those are only for end of statements?

Thanks, I found some other errors also in going through it such as spelling and missing an ;.

I checked out the responses to the above question.
From what I have coded the correct syntax is:
if (condition test)
{
some code
}
else
{
some code
}
Omitting the braces will cause a runtime/compile error showing that a } is required.
Just my take on the question.

Omitting the braces will cause a runtime/compile error showing that a } is required.

Not necesarily true. It is recommended to always use the bracers to maintain formatting and avoid mistakes, but it isnt required if you are only executing a single statement after the condition:

if(true)
    DoSomething() //no error
....

int x;
if(true)
    x = 1;
else
    x = 2; //also no error
....

int x;
if(true)
    x = 1;
    DoSomething(x);
else
    x = 2;
    DoSomething(x); //ERROR

When executing more than one line you use the bracers to group them into a single code block. The code block is then executed or skipped based on the condition.

It may not be necessarily true, but, you're in for a world of errors if you don't do it just right.
}'s may be a pain in the butt, but they are totally worth it for ease of reading and debugging.

Personally, I prefer no brackets on a one-line block. I realize it's just a preference thing, but just as whitespace in code is good, too much is annoying. However, I won't mismatch brackets on if-else blocks. Meaning if one of the blocks is multiple lines of code, then the single-line block gets bracketed as well.

// would do
if (condition)
    DoXMethod();
else 
    DoYMethod();

// would do
if (condition)
{
    DoXMethod();
}
else 
{
    DoYMethod();
    DoZMethod();
}

// would not do
if (condition)
    DoXMethod();
else 
{
    DoYMethod();
    DoZMethod();
}

I have done this, take it for what it's worth.
if (condition) { do something; }
else { do something else; }
and
if (condition) { do something; do something; }
Bracing at least allows you to keep total track of what you're doing in the if / else statements.
The ones without the bracing kinda looks like Python lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.