Click here to Skip to main content
15,117,549 members
Please Sign up or sign in to vote.
1.75/5 (4 votes)
See more:
This sh*t has been solved.

Updated 22-Aug-12 17:08pm
Prabhakaran Soundarapandian 9-Aug-12 23:57pm
You just posted your code...improve your question what is the scenario and what loop validation you want..then only we could help you..
AmitGajjar 10-Aug-12 0:07am
what you mean by validating if else ????
Vani Kulkarni 10-Aug-12 0:12am
What is the issue you are facing?
AmitGajjar 10-Aug-12 0:18am
your code is correct, nothing to do.
AmitGajjar 10-Aug-12 0:33am
See my solution 4 for your answer. Mark it as answered if it helps you.

Volynsky Alex 10-Aug-12 5:54am
Nice question :)

As I can see your if..else statement is incorrect. You should validate it using >= & <= operators both.
Try this:
//Determine pay rate and tax rates based on entered data
if (annual_salary >= 0 && annual_salary <= 16500)
    tax_rate = 11.32F;
    pay_rate = 8.68F;

Thank you @@Amitgajjar to suggest me.
Hi I executed your code it's working perfectly. For error checking use try..catch blocks.
Use this:
//Determine pay rate and tax rates based on entered data
    if (annual_salary >= 0 && annual_salary <= 16500)
        tax_rate = 11.32F;
        pay_rate = 8.68F;
}catch(Exception ex)

Refer the links for more information:
try-catch (C# Reference)[^]
try-catch-finally (C# Reference)[^]

malakas1821 10-Aug-12 0:02am
First of all thanks for your quick answer.
This is exactly how I did it , but my programming teacher told me to do it the way it is on the first post.
AmitGajjar 10-Aug-12 0:09am
Sorry _Amy but in your code there is no need for first Condition in "else if" statements. because it is already checked by it's previous condition.
_Amy 10-Aug-12 0:11am
To prevent fall through I used that. :)
AmitGajjar 10-Aug-12 0:18am
suggest you to Improve your solution otherwise someone may downvote it. because it is irrelevant.
_Amy 10-Aug-12 0:33am
Try my updated answer. :)
Volynsky Alex 10-Aug-12 13:24pm
Good answer!
No. Whatever your teacher said, its right. I would suggest you to go ahead with your original post..

When you are having it in sequence, like this.
if (annual_salary >= 0 && annual_salary <= 16500)
                tax_rate = 11.32F;
                pay_rate = 8.68F;
            else if (annual_salary <= 19500)
                tax_rate = 15.14F;
                pay_rate = 10.26F;

its same as

if (annual_salary >= 0 && annual_salary <= 16500)
    tax_rate = 11.32F;
    pay_rate = 8.68F;
else if (annual_salary >= 16500 && annual_salary <= 19500 )
    tax_rate = 15.14F;
    pay_rate = 10.26F;

For eg, when i pass 17000, definitely it will take tax rate as 15.14F, since it will fail first if loop and enters else if.

If you want to test the complete code,

use the following values and run once and test your code.

Input:Annual_salary Expected:Tax_rate
14000 11.32F
17000 15.14F
21000 22.65F
32000 27.1F
34000 30.92F
45000 35.72F
78000 40.72F
91000 50.52F
malakas1821 10-Aug-12 0:12am
Is there anything else that I can add or is it fine as it is ?
Santhosh Kumar Jayaraman 10-Aug-12 0:14am
The if else loop is fine.It will definitely work
malakas1821 10-Aug-12 0:20am
Ok , thanks . can you help me with error checking the data ?
Santhosh Kumar Jayaraman 10-Aug-12 0:22am
i am not sure what you want from me?
malakas1821 10-Aug-12 0:25am
to make errors come up if the entering data are not correct
Santhosh Kumar Jayaraman 10-Aug-12 0:59am
is that working?
Santhosh Kumar Jayaraman 10-Aug-12 0:37am
You mean kind of validation..

try this.

Console.Write("Enter your annual salary >");
int annual_salary;
bool result = Int32.TryParse(Console.ReadLine(), out annual_salary);
if (!result)
Console.WriteLine("Please enter correct data");
Santhosh Kumar Jayaraman 10-Aug-12 2:40am
Console.Write("Enter your postcode");
string postcode = Console.ReadLine();
if (postcode.Length != 4)

Like this you have to set validation for all inputs.
Mark this as resolved if you got your solution.

You need to add some exception handling at many places.

Example 1: User should not allowed to enter annual_salary below 0.
So your code would be like,

throw new ArgumentOutOfRangeException(); //// Here you can even prompt some message. if you are developing API then you should throw Exception to handled from client.

Example 2: User should not enter Empty name.
the code would be like,

if (string.IsNullOrEmpty(full_name))
throw new ArgumentNullException(); //// Here you can even prompt to enter correct name.

This way so many validation can be added in your application.

Best of luck

-Amit Gajjar.
malakas1821 10-Aug-12 0:41am
I havent learned these yet. What my professor told me was that "Think about using if statements to check. Also use tryparse to test for integers."
Can you give me an example with that ?
AmitGajjar 10-Aug-12 0:49am
The reason your teacher said that, user may enter incorrect integer value may be some string. you need to handle it using int.TryParse
AmitGajjar 10-Aug-12 0:51am
you are using int.parse this will fail if string is entered by the user. so instead use int.TryParse.
malakas1821 10-Aug-12 0:52am
can you give me one example on how can I use it ?
AmitGajjar 10-Aug-12 0:55am
check MSDN article you and find example there int.TryParse
A similar code snippet was posted in the Horrors recently. I think the correct answer here is the same as it was there: a list of bands and a simple lookup into them:

public class Band {
 public float UpperBound, TaxRate, PayRate;
 public Band(float upperBound, float taxRate, float payRate) { 
  UpperBound = upperBound; TaxRate = taxRate; PayRate = payRate;

class MainClass {
 private static readonly Band[] bands = {
  new Band(16500, 11.32F, 8.68F),
  new Band(19500, 15.14F, 10.26F),
  // etc
  new Band(89500, 40.72F, 47.12F),
  new Band(float.Infinity, 50.52F, 55.67F) // Infinity matches any finite salary

 void YourMethod(){
  // All the code that gets stuff off the console, etc
//Annual Salary data
  Console.Write("Enter your annual salary >");
  annual_salary = int.Parse(Console.ReadLine());
  Band salaryBand = bands.First(b => b.UpperBound >= annual_salary);
  tax_rate = salaryBand.TaxRate; pay_rate = salaryBand.PayRate;

  // Calculate gross pay
  gross_pay = hours_worked * pay_rate;
  // ... etc

And yes you should probably guard against a salary less than zero, otherwise you'll get the first one, which might not be what you want.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900