Switch help

Need help using switch statement

Page 1 of 1

4 Replies - 810 Views - Last Post: 06 November 2008 - 07:45 PM Rate Topic: -----

#1 louisstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

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

Reputation: 85
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 louisstephens  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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. :^: :^:
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Switch help

Posted 06 November 2008 - 07:12 PM

View PostTriggaMike, 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

Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1