Substitute for switch

Pages: 12
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
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
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
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
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.
Not cleaner. I don't think there is a way. IS THERE ?
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.
@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.
@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.
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.
I don't get it can u please make an example.
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.
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";
@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.
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.
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.

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