7 Replies - 301 Views - Last Post: 30 January 2013 - 03:16 PM Rate Topic: -----

#1 Johan113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-January 12

wrong in if C#

Posted 30 January 2013 - 02:18 PM

            double pris, skatt;

            Console.Write("Skriv in hur mycket du tjänar: ");
            pris = double.Parse(Console.ReadLine());
            if (10000<50000)
            {
                skatt = 0.5;
                Console.WriteLine("\nDin skatt är " + pris * skatt);
            }
            else if (50000<100000)
            {
                skatt = 0.5;
                Console.WriteLine("\nDin skatt är " + pris * skatt + 5000);
            }
            else if (100000>pris)
            {
                skatt = 0.7;
                Console.WriteLine("\nDin skatt är " + pris * skatt + 5000);
            }
            else
            {
                Console.WriteLine("\nDu behöver inte betala någon skatt");
            }

            Console.ReadLine();
        }
    }
}


when i type in a number between 10000 - 50000 it going to take the number and * it whit 0.5.
and the secound it going to * 0.5 between a number 50000 - 100000 and + 5000.
and the theerd it going to look for a number 100000 and higher and ' 0.7 and + 5000.
and if the number is under 10000 it only going to type out a text. i tryd changing the pris in to a int but when i do it it com up Unreachable code detected in the error field on the else if.

so what im i doing wrong in this code

Is This A Good Question/Topic? 0
  • +

Replies To: wrong in if C#

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,643
  • Joined: 02-June 10

Re: wrong in if C#

Posted 30 January 2013 - 02:22 PM

We need to see the version with the unreachable code error if you want help with why it is giving you that error.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,643
  • Joined: 02-June 10

Re: wrong in if C#

Posted 30 January 2013 - 02:31 PM

Quote

if (10000<50000)

This will always be true. 10000 will always be less than 50000.
You aren't comparing your variables to anything here. You aren't using the value input by the user.

Quote

            else if (50000<100000)


same as above

Follow the logic down as it executes.

If you enter 50000

Line 15 will always be true

Quote

15            else if (100000>pris)


because 100000 will always be greater than 50000
So the else after line 15 can NEVER be reached.
Was This Post Helpful? 0
  • +
  • -

#4 Johan113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-January 12

Re: wrong in if C#

Posted 30 January 2013 - 02:53 PM

            if (100000 > pris)
            {
                skatt = 0.7;
                Console.WriteLine("\nDin skatt är " + pris * skatt + 5000);
            }
            else if (50000<100000)
            {
                skatt = 0.5;
                Console.WriteLine("\nDin skatt är " + pris * skatt + 5000);   
            }
            else if (10000<50000)
            {
                skatt = 0.5;
                Console.WriteLine("\nDin skatt är " + pris * skatt);     
            }
            else
            {
                Console.WriteLine("\nDu behöver inte betala någon skatt");
            }

            Console.ReadLine();


i have changet it like this but i still get a error on the last else if that is Unreachable code detected
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: wrong in if C#

Posted 30 January 2013 - 03:01 PM

Did you read the previous posts? This:

else if (50000<100000)

is ALWAYS TRUE so you will never reach the else..if and else clauses below it.

I assume you mean, perhaps, else if (50000 < pris), or something similar.

This post has been edited by andrewsw: 30 January 2013 - 03:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: wrong in if C#

Posted 30 January 2013 - 03:06 PM

You're still doing comparisons of fixed integers. Because of this, what your code REALLY says is

if (100000 > pris)
{
    skatt = 0.7;
    Console.WriteLine("\nDin skatt är " + pris * skatt + 5000);
}
else
{
    skatt = 0.5;
    Console.WriteLine("\nDin skatt är " + pris * skatt + 5000);
}

Console.ReadLine();



Think it through: if 100000 is greater than pris, the if statement will be evaluated. If not, it will check that condition on the first else if. Well, we can guarantee that 50000 is less than 100000, so it's going to evaluate that first else if and then exit the if/else block and move on to the Console.ReadLine(). Your two else/if statements are pointless. I'm surprised your IDE isn't telling you that those conditions are always true. If you really need four separate conditions, then the first three need to contain actual evaluations, most likely against that pris variable.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,643
  • Joined: 02-June 10

Re: wrong in if C#

Posted 30 January 2013 - 03:13 PM

Talking to the wind - either because of a language barrier, or an IQ barrier.

<unsubscribing>
Was This Post Helpful? 0
  • +
  • -

#8 Johan113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-January 12

Re: wrong in if C#

Posted 30 January 2013 - 03:16 PM

sorry for it i'm tired and i fix it know. THANKS ALOT FOR THE HELP:D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1