Declaring wrong?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 588 Views - Last Post: 17 February 2013 - 10:31 AM Rate Topic: -----

#16 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 17 February 2013 - 08:58 AM

Not fully. Was i not supposed to set them to 0?
Was This Post Helpful? 0
  • +
  • -

#17 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 17 February 2013 - 09:08 AM

That depends on what exactly you're trying to accomplish, but probably not. I'm not really sure what purpose those variables serve.

Anyway your code is currently equivalent to this:

            string classif;
            //Ask their age
            Console.WriteLine("How old are you?");
            //Save the answer
            age = int.Parse(Console.ReadLine());
            //Display persons age
            Console.WriteLine("Persons Age: {0}", age);
            //Calculate the class and multiplier
            if (age <= childD)
            {
                classif = "0"; // Because child is the same as "0"
                
            }
            else if (age <= teenD && age > childD)
            {
                classif = "0"; // Because teen is the same as "0"
            }
            else if (age <= adultD && age > teenD)
            {
                classif = "0"; // Because adult is the same as "0"
            }
            else
            {
                classif = "0"; // Because senior is the same as "0"
            }
            if (classif == "0")
            {
                rateMul = childRate;
            }
            else if (classif == "0")
            {
                rateMul = teenRate;
            }
            else if (classif == "0")
            {
                rateMul = adultRate;
            }
            else
            {
                rateMul = seniorRate;
            }



So in effect the first set of if-statements and their else-part all have the same body. So instead of writing that whole if you can just write classif = "0"; and leave out the if altogether.

And for the second set of if statements the conditions are all the same. So we can scratch all the else-ifs as they're redundant. So this is what we end up with:

classif = "0";
if(classif == "0")
{
    rateMul = childRate;
}
else
{
    rateMul = seniorRate;
}



I hope you see now that the if condition will always be true, so what we effectively end up with is:

classif = "0";
rateMul = childRate;


Was This Post Helpful? 1
  • +
  • -

#18 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 17 February 2013 - 09:18 AM

Yea and thats most definitely not what i was aiming for haha. My intentions for the first set of if statements were to take the persons age and depending on how old they were label them as child teen adult or senior. Not 0 obviously. And the purpose of the second set of if statements was to take what ever they were labeled as before and correlate a number to them as their rate. (1, 1.5, 1.7, 2). and then display age, classification and and rate. EX Age: 23 Classification: adult Rate Multiplier: 1.7
Was This Post Helpful? 0
  • +
  • -

#19 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,204
  • Joined: 21-June 11

Re: Declaring wrong?

Posted 17 February 2013 - 09:22 AM

So why not get rid of the child etc. variables and instead write classif = "child"; etc.? Or even better: use enums.
Was This Post Helpful? 0
  • +
  • -

#20 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Re: Declaring wrong?

Posted 17 February 2013 - 10:31 AM

Care to brief me on that? I just learned switches and enums and it was taught to me very poorly to the point where i dont understand how to use it or even why its used.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2