Substitute for switch

Pages: 12
Oct 12, 2011 at 10:15pm
I just wanted to know if there is any substitute for switch statement which is easier looking. It shouldn't be arrays because I haven't learned that yet.If I have switch statement like such :

1
2
3
4
5
6
7
8
9
switch(player)
      {
         case 1: name = "ABC" ; break;
         case 2: name = "XYZ" ; break;
         .
         .
         .
         case 10: name = "PPP"; break;
      }


Thanks. Please let me know
Oct 12, 2011 at 10:17pm
One of the substitutes are if and else statements. But I prefer switch statements. They're better and save you more time.
Also, what do you mean when you say "easier" looking. Do you mean cleaner?
Last edited on Oct 12, 2011 at 10:18pm
Oct 12, 2011 at 10:18pm
1
2
3
4
5
6
7
8
9
if (player == 1)
  name = "ABC";
else if (player == 2)
  name = "XYZ";
.
.
.
else if (player == 10)
  name = "PPP";

Last edited on Oct 12, 2011 at 10:20pm
Oct 12, 2011 at 10:19pm
Why are you posting code?
I believe the numbers in your parameters should be surrounded in little commas like so:

if (player == '1')
Last edited on Oct 12, 2011 at 10:20pm
Oct 12, 2011 at 10:27pm
EricDu is testing for integers, Code Assassin is testing for chars. They are both valid examples of code but they test for different things.

@OP: Saying that you haven't learned about array's yet is not a valid reason to avoid them. In fact the example you are posting is a great situation to learn about addressing data by index in arrays. The code would look something like this:

1
2
3
std::string name[11] = {"ABC", "XYZ", /*Fill In The Rest Of The Array*/, "PPP"};

std::cout << name[player];


Done. This will not always replace a good switch case block but in this case it works as long as you are testing for integers.
Oct 12, 2011 at 10:28pm
Not cleaner. I don't think there is a way. IS THERE ?
Oct 12, 2011 at 10:33pm
No. I cannot use arrays because we haven't learned that yet. I don't want the teacher to be suspicious thinking I got it from some expert.
Oct 12, 2011 at 10:41pm
@OP: You didn't mention that this was part of a school assignment, now your objection to using array's has a reason to it, not what I would consider a good reason but at least it is A reason. Right now you're rejecting three perfectly valid ways to write the code. You can make your switch statements look a little neater with some formatting but it's purley aethstetic at that point. Also you forgot to include a default case in the switch you posted above.
Oct 12, 2011 at 10:45pm
@computergeek01 : I don't need a default in it. It is already stated previously in the code. Can you please tell me how to make them look neater. Thanks.
Oct 12, 2011 at 10:50pm
Then what happens when player >= 11?

The problem with making it look neater is that it's a matter of opinion. This is exactly how I write my simple switch statements because I think it looks the best.
Oct 12, 2011 at 10:52pm
I don't get it can u please make an example.
Oct 12, 2011 at 11:00pm
I'm saying that the example you posted is how I write my switch statements. I would not change anything because I think it looks good. A switch statement is FAR neater then the "if() else if()" alternative.
Oct 12, 2011 at 11:04pm
neat or not depends on who thinks of it, let's see the same code below, which is neater? ....... no final answer I think.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(player)
      {
         case 1:
             name = "ABC";
             break;
         case 2:
             name = "XYZ" ;
             break;
         .
         .
         .
         case 10: 
            name = "PPP"; 
            break;
      }


1
2
3
4
if (player == 1)  name = "ABC";
else if (player == 2)  name = "XYZ";
...
else if (player == 10)  name = "PPP";
Oct 12, 2011 at 11:14pm
@EricDu : none of them LOL. Mine is neater. I say that because it is in one line.

@computergeek01 : Yea. I am not going to change it.
Oct 12, 2011 at 11:21pm
Can someone re-write this statement below in their own words:

This program implements and tests a Date class that
stores dates and displays them in different formats.
Oct 12, 2011 at 11:26pm
I would say like below:

Este programa implementa y las pruebas de una clase Fecha que
almacena las fechas y los muestra en diferentes formatos.

โปรแกรมนี้จะดำเนินการและการทดสอบการเรียนวันที่
เก็บข้อมูลวันที่และแสดงไว้ในรูปแบบที่แตกต่างกัน
Oct 12, 2011 at 11:29pm
hahha...no dude seriously.
Oct 12, 2011 at 11:42pm
anyone please ....
Oct 12, 2011 at 11:49pm
This application consummates and scrutinizes a Date object that
squirrels dates and blazons them in contradistinctive compositions.
Oct 12, 2011 at 11:51pm
lol..dude that is too complicated. I want something simple.
Pages: 12