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;
}
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?
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.
@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.
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'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.