Anyway I can simplify these if statements?
Hey everyone, here's the code I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
if (letter == 'a')
{
xCoordinate = 0;
}
else if (letter == 'b')
{
xCoordinate = 1;
}
else if (letter == 'c')
{
xCoordinate = 2;
}
else if (letter == 'd')
{
xCoordinate = 3;
}
else if (letter == 'e')
{
xCoordinate = 4;
}
else if (letter == 'f')
{
xCoordinate = 5;
}
else if (letter == 'g')
{
xCoordinate = 6;
}
else if (letter == 'h')
{
xCoordinate = 7;
}
else if (letter == 'i')
{
xCoordinate = 8;
}
else
{
xCoordinate = 9;
}
|
Anyway I can simplify this? Can I use Macro?
Last edited on
|
xCoordinate = letter >= 'a' && letter <= 'j' ? letter - 'a' : 9;
|
Note: This doesn't work on EBCDIC systems (EBCDIC systems are extremely rare).
Last edited on
@helios,
thank you very much man, you're a life saver :)
Topic archived. No new replies allowed.