# Switch help

Page 1 of 1

## 4 Replies - 1267 Views - Last Post: 06 November 2008 - 07:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=70788&amp;s=b78dbad5014977167c711808732d1755&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 louisstephens

Reputation: 0
• Posts: 9
• Joined: 30-October 08

# Switch help

Posted 06 November 2008 - 01:47 PM

Ok, well I was just messing around with some exercises in my book, and I came to a chapter using switch. I am having some trouble with this..

it says I need to set up the switch statement to implement the following commision schedule:

fewer than 10 items sold = 1% commission
between 10 and 39 items = 2% commission
between 40 and 99 items = 2% commission
more than 99 items sold = 4% commission

I know you could use an if statement for this, such as "if items > 10", but I dont know how to really set up the switch using cases.

Is This A Good Question/Topic? 0

## Replies To: Switch help

### #2 TriggaMike

• Using up all your 1's and 0's

Reputation: 85
• Posts: 1,103
• Joined: 26-September 08

## Re: Switch help

Posted 06 November 2008 - 02:05 PM

K, this seems like a very poor place to use a switch statement so I'll give two solutions.

```public class switchHelp
{
public static void main(String[] args)
{
int var = 0; //the number of sales
int switchControl = 0; //controller for the switch

if(var < 10)
switchControl = 0;
else if(var < 40)
switchControl = 1;
//more else ifs and also an else

switch (switchControl)
{
case 0:
//do stuff
break;
case 1:
//do other stuff
break;
//More Cases
}
}
}
```

Now that piece of code uses else ifs to set a dedicated switch variable to jump to different cases. If you just wanted to use a switch I hope you like typing repetitively.

```public class switchHelp
{
public static void main(String[] args)
{
int var = 0; //the number of sales

switch (var)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
//Give 1% commission
break;
//More Cases
}
}
}
```

So either you have to type like, 10 lines of else ifs and have 4 cases, or just type 100 cases

### #3 louisstephens

Reputation: 0
• Posts: 9
• Joined: 30-October 08

## Re: Switch help

Posted 06 November 2008 - 02:15 PM

Thanks. Yeah, I think I have a lot of cases to start typing.

Thanks for the help. I really appreciate it.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Switch help

Posted 06 November 2008 - 07:12 PM

TriggaMike, on 6 Nov, 2008 - 01:05 PM, said:

K, this seems like a very poor place to use a switch statement so I'll give two solutions.

```public class switchHelp
{
public static void main(String[] args)
{
int var = 0; //the number of sales
int switchControl = 0; //controller for the switch

if(var < 10)
switchControl = 0;
else if(var < 40)
switchControl = 1;
//more else ifs and also an else

switch (switchControl)
{
case 0:
//do stuff
break;
case 1:
//do other stuff
break;
//More Cases
}
}
}
```

Now that piece of code uses else ifs to set a dedicated switch variable to jump to different cases. If you just wanted to use a switch I hope you like typing repetitively.

```public class switchHelp
{
public static void main(String[] args)
{
int var = 0; //the number of sales

switch (var)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
//Give 1% commission
break;
//More Cases
}
}
}
```

kind of weird....
switch statements are to test a specific condition... if it is not the case use if statements that can compare > and <
OK implement a switch stament for the following:

Tax rate for annual income:
- between \$ 0 and \$10,000 2%
- between \$10,001 and 20,000 4%
- between \$20,001 and \$ 30,000 6%
.....
have fun... a 30,000 lines switch statement ?

So either you have to type like, 10 lines of else ifs and have 4 cases, or just type 100 cases

### #5 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Switch help

Posted 06 November 2008 - 07:45 PM

well said in the previous post, in that problem you are solving it will be much easier to do it using if-statements because you are comparing, the switch should be used when u have an output that corresponds to an input and doesnt require any comparing