In this case use switch or if?

Asked

Viewed 284 times

1

I have an exercise to do which consists of the following:

The user informs the salary, and depending on the amount informed, he earns an increase according to the table. Example: From 1000 to 1500 he earns 10% From 1500 to 2000 he earns 5% 2000 upwards he earns no increase.

I thought to make using several ifs, but in case, it would be better to use switch? Why?

  • 1

    hello, have you developed an algorithm for the task ? explain what this analysis of the average wage consists of, because from what you have described there, you can take different approaches, which may not be the most appropriate for the situation in question, or simply provide the exercise in its integrity.

  • I had described it wrong. Then the user informs the salary, and depending on the amount informed, he earns an increase according to the table. Example: From 1000 to 1500 he earns 10% From 1500 to 2000 he earns 5% 2000 upwards he earns no increase. I think as the friend down there answered, it will be with several ifs same!

1 answer

3


The question does not make the criteria very clear, but probably each percentage is applied according to a range of values. If this is just the if works. switch does not allow working with tracks, only with constants.

But if for some reason it is possible to reduce to a constant from a formula using the average wage, then not only could use the switch, but perhaps it was the case even to use a array nor use a switch.

  • I had described it incorrectly. Then, the user informs the salary, and depending on the amount informed, he earns an increase according to the table. Example: From 1000 to 1500 he earns 10% From 1500 to 2000 he earns 5% 2000 upwards he earns no increase. I think it will be with several ifs same!

  • So see the [tour] what you need to do now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.