Switch question

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 758 Views - Last Post: 18 May 2009 - 10:00 AM Rate Topic: -----

#1 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2668
  • View blog
  • Posts: 15,703
  • Joined: 24-September 08

Switch question

Posted 17 May 2009 - 03:39 PM

I was wondering if you were allowed to put out multiple returns in a single method if in a switch statement?
Is This A Good Question/Topic? 0
  • +

Replies To: Switch question

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Switch question

Posted 17 May 2009 - 03:58 PM

Just finding your questions slightly hard to understand, do you have any examples? If your asking if whether each case can have more than one statement, then yes they can. But you might be asking somthing totally different?
Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2668
  • View blog
  • Posts: 15,703
  • Joined: 24-September 08

Re: Switch question

Posted 17 May 2009 - 04:12 PM

Oh sorry.

like:

switch(value) {

case 1: return value1;

case 2: return value2;

case 3: return value3;

}




Is that legal?

This post has been edited by NeoTifa: 17 May 2009 - 04:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Switch question

Posted 17 May 2009 - 04:19 PM

Dont see a problem with that. Switch statements dont generally use the return keyword though as if the case is called upon, it generally returns it anyways
Also, just quickly, if you do it with return's, then you will problably have to return -1 outside of the switch.

This post has been edited by nick2price: 17 May 2009 - 04:33 PM

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 question

Posted 17 May 2009 - 04:44 PM

yep you can do that but you will also have to add a default case..suppose what the user enters doesn't match with any of the cases then what will the method return ???..this will cause the compiler to give an error..so what you need to do, either add a default case or add that last return outside the switch
Was This Post Helpful? 0
  • +
  • -

#6 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: Switch question

Posted 17 May 2009 - 04:58 PM

I don't know if there is any convention to this, but I probably would have done this:
returnValue;
switch(value) {

case 1: returnValue=value1;

case 2: returnValue=value2;

case 3: returnValue=value3;

}
return returnValue;

Was This Post Helpful? 0
  • +
  • -

#7 BlackMagick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-May 09

Re: Switch question

Posted 17 May 2009 - 04:59 PM

Yes that perfectly legal.also instead of numbers you can put char's and strings. Putting in a default case is nice in case of bad input or just in case the cases don't match with the switch statement input. The syntax for a switch statement is:


switch()
{
  
	case 1:
	{
	   .... 
	 }

   case 2:
	 {
		.... 
	  }
   
   case 3:
	{
	  ....
	 }

	default:
	{
	 ....
	 }
}



I also agree with nick. Even tho the return in the switch should work. you can also have the cases assign the values and return the value after the switch statement or at the end of the method

This post has been edited by BlackMagick: 17 May 2009 - 05:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Switch question

Posted 17 May 2009 - 05:04 PM

Quote

instead of numbers you can put char's and strings

Unless its changed, i dont believe switch statements can except String's. The only alternative is to use enums
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Switch question

Posted 17 May 2009 - 05:06 PM

yes switch statements dont work with strings
Was This Post Helpful? 0
  • +
  • -

#10 BlackMagick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-May 09

Re: Switch question

Posted 17 May 2009 - 05:09 PM

Yea you are right techniclly you can't do strings you'd have to convert and do char to char comparison which would take awhile. My bad :D
Was This Post Helpful? 0
  • +
  • -

#11 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2668
  • View blog
  • Posts: 15,703
  • Joined: 24-September 08

Re: Switch question

Posted 18 May 2009 - 07:06 AM

Yea I know about default. It was a generic example. I was using enums, and no, Strings do not work. Only ints, because enums are just ints really. I was wondering how to put a private int and mutate another private int in another class. I had to change the int to public, which I didn't like doing, for the time being. Hmmmm. I guess that would work. Thanks. Didn't think of that :D
Was This Post Helpful? 0
  • +
  • -

#12 Jubb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 06-May 09

Re: Switch question

Posted 18 May 2009 - 07:20 AM

Yeah, multiple return statements are bad practice if you're coding in an environment where other people have to look at your code. It's too easy to have one stray return statement end a function before it's finished, and can be hard to debug.
Was This Post Helpful? 0
  • +
  • -

#13 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: Switch question

Posted 18 May 2009 - 07:24 AM

View PostNeoTifa, on 18 May, 2009 - 08:06 AM, said:

I was wondering how to put a private int and mutate another private int in another class. I had to change the int to public, which I didn't like doing, for the time being. Hmmmm. I guess that would work. Thanks. Didn't think of that :D

Perhaps I'm misreading your post, but if you're trying to mutate a private int in another class, why not use a "mutator method"?
i.e.
public class Test1 {
private int shoe;

public int getShoe() { // Accessor Method
return shoe;
}

public void setShoe(int shoe) { // Mutator Method
this.shoe = shoe;
}
}

Then call setShoe(int) from the class that you're in.

Maybe you could show a brief snippet of what you're trying to do, so I can understand better.
Was This Post Helpful? 0
  • +
  • -

#14 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2668
  • View blog
  • Posts: 15,703
  • Joined: 24-September 08

Re: Switch question

Posted 18 May 2009 - 08:07 AM

I did, I'm just trying to say.... lets see if I can remember... I'm at work....

public void useItem(Player character, Item item) {

//switch case potion
character.setHp(character.hp + potion);

}



See, but hp is private. Blah, that's why I wanted to return different values. This goes for ethers too, so it's like different variables can be affected. Get it?
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Switch question

Posted 18 May 2009 - 08:09 AM

Use an accessor for the hp?

public getHP()
{
   return hp;
}

//over wherever

public void useItem(Player character, Item item) {

//switch case potion
character.setHp(character.getHP() + potion);
}


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2