using if-else statements?

I can't figure out how to use the if else statement or even if tha

Page 1 of 1

2 Replies - 1682 Views - Last Post: 12 October 2009 - 05:11 PM Rate Topic: -----

#1 yubtub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 09

using if-else statements?

Posted 12 October 2009 - 09:28 AM

Hello, Im still pretty new at programming with C#, and I have been attempting to figure out this program using if-else. I don't know if I should even us a if-else statement haha. It seems like it should work but it is saying 'invalid exspression term'.

I need to have a tax rate of 10% on gross pay under $300.00 and a 12% rate on gross pay over $300.00
It seems that this should work but it doesn't!!
This is what i have so far.

using System;

namespace PayrollOffice
{
	class Payroll
	{
		static void Main()
		{
			double grossPay, hoursWorked, netPay, taxRate, hourPay;
			string grossPayString, hoursWorkedString, netPayString, taxRateString, hourPayString;
			const Double tax299 = .10, tax300 = .12;
 
			Console.WriteLine("*** Payroll Office ***");
			Console.WriteLine();
			Console.WriteLine("Enter hourly pay rate: ");
			hourPayString = Console.ReadLine();
			hourPay = Convert.ToDouble(hourPayString);

			Console.WriteLine("Enter hours worked: ");
			hoursWorkedString = Console.ReadLine();
			hoursWorked = Convert.ToDouble(hourPayString);

			Console.WriteLine();

			grossPay = hourPay * hoursWorked;
			if (grossPay < 300.00)

			taxRate = grossPay * tax299;
			netPay = grossPay - taxRate;

				Console.WriteLine("Gross Pay: {0}", grossPay.ToString("C"));
				Console.WriteLine("Tax withholding: {0}", taxRate.ToString("C"));
				Console.WriteLine("Net pay: {0}", netPay.ToString("C"));

			else(grossPay >= 300.01)
								
			taxRate = grossPay * tax300;
			netPay = grossPay - taxRate;

				Console.WriteLine("Gross Pay: {0}", grossPay.ToString("C"));
				Console.WriteLine("Tax withholding: {0}", taxRate.ToString("C"));
				Console.WriteLine("Net pay: {0}", netPay.ToString("C"));

			Console.WriteLine("Press the <Enter> Key to terminate this program");
			Console.Read();


I need suggestions to how I can get this to work for me, or which decision to use.

thanks -Andy

Is This A Good Question/Topic? 0
  • +

Replies To: using if-else statements?

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4437
  • View blog
  • Posts: 12,308
  • Joined: 18-April 07

Re: using if-else statements?

Posted 12 October 2009 - 09:44 AM

Get in the habit of always including opening and closing curly braces. What is happening is that because you don't use curly braces on your if statement, it assumes that the if statement is one line. So it only really executes the line taxRate = grossPay * tax299; as part of the if statement and then it jumps back out. It executes the other statements and all of a sudden hits the "else" and doesn't recognize it was part of the previous if statement. To fix this, add curly braces to tell the if statement which code what the body is of the if and body of the else...

// Notice the opening curly brace and the one closing later. 
// This tells the if what statements to execute as part of the if/else.
if (grossPay < 300.00) {

	 taxRate = grossPay * tax299;
	 netPay = grossPay - taxRate;

	 Console.WriteLine("Gross Pay: {0}", grossPay.ToString("C"));
	 Console.WriteLine("Tax withholding: {0}", taxRate.ToString("C"));
	 Console.WriteLine("Net pay: {0}", netPay.ToString("C"));
}
else (grossPay >= 300.01) {
							   
	 taxRate = grossPay * tax300;
	 netPay = grossPay - taxRate;

	 Console.WriteLine("Gross Pay: {0}", grossPay.ToString("C"));
	 Console.WriteLine("Tax withholding: {0}", taxRate.ToString("C"));
	 Console.WriteLine("Net pay: {0}", netPay.ToString("C"));
}



So as you can see we include { and } to tell which statements belong to the if else. Since you didn't have them, if/else assumes only the first line is part of the if/else and the rest are to be treated as their own lines.


Now one tip for you here, we see that in both instances of the if and the else we have the same statements. We want to reduce this code so that there is only one set of these lines. This cuts down the redundancy. Since these lines are going to be executed no matter if the first part of the if or the second part, the else, we can pull those lines out of both and put it after the if/else....

// Notice the opening curly brace and the one closing later. 
// This tells the if what statements to execute as part of the if/else.
if (grossPay < 300.00) {

	 taxRate = grossPay * tax299;
	 netPay = grossPay - taxRate;
}
else (grossPay >= 300.01) {
							   
	 taxRate = grossPay * tax300;
	 netPay = grossPay - taxRate;
}

// These were the same for both, so we can put it ONCE after the if

Console.WriteLine("Gross Pay: {0}", grossPay.ToString("C"));
Console.WriteLine("Tax withholding: {0}", taxRate.ToString("C"));
Console.WriteLine("Net pay: {0}", netPay.ToString("C"));



Essentially this cuts down 6 lines (2 sets of 3 lines) down to one. Now we don't have to worry about updating two places in the code if these lines needed updating, just the one set. Hope you get the idea here. Enjoy!

"At DIC we be the if/else code ninjas... IF you are talking trash, you better run ELSE you are going to die!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: using if-else statements?

Posted 12 October 2009 - 05:11 PM

View PostMartyr2, on 12 Oct, 2009 - 08:44 AM, said:

Get in the habit of always including opening and closing curly braces.


This has been the cause of so many errors it makes me question why they don't require them everywhere. It's not like it makes your code and slower, but would stop a lot of wasted time trying to track these down.

So C# language specifier peoples, what's your rational for not requiring them? "Because you don't have to in other languages" is not a valid reason!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1