在各种'if'语句中做同样的事情可以改进吗?

0 c++ if-statement

当我遇到这个问题时,我正在编码,我想知道这是否正确,或者出于性能原因可以改进:

if (color == 0) {
    if (c.val[0] >= 0 && c.val[0] <= 64) {
        //Black
        Paint(cursor.x + x, cursor.y + y);
    }
}
else if (color == 1) {
    if (c.val[0] >= 64 && c.val[0] <= 128) {
        //Grey
        Paint(cursor.x + x, cursor.y + y);
    }
}
else if (color == 2) {
    if (c.val[0] >= 128 && c.val[0] <= 192) {
        //White Gray
        Paint(cursor.x + x, cursor.y + y);
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以看到即时通讯在所有的IF语句中完成同样的事情,它看起来很奇怪,程序按我想要的方式工作,但我真的觉得我错过了一些东西..

谢谢!

adr*_*tam 5

由于结构,

if (color >= 0 && color <= 2) {
    if (c.val[0] >= 64*color && c.val[0] <= 64*(color+1)) {
        Paint(cursor.x + x, cursor.y + y);
    }
}
Run Code Online (Sandbox Code Playgroud)