# If Else Alternative [closed]

I've written a function that heavily relies on the If / Else statement but I feel it's a very beginner way to achieve functionality. Are there any better ways to achieve the same functionality?

Code:

``````void jumpThree() // Update from left then right
{
if (effectCounter < tiles) // Right, first tile
{
Serial << flag << "\t";
Serial << newRed << " " << newGreen << " " << newBlue << "\t";

if (flag == 1)
{
oldRed   = (tileBuffer[effectCounter] & 0xff0000) >> 16;
oldGreen = (tileBuffer[effectCounter] & 0x00ff00) >> 8;
oldBlue  = (tileBuffer[effectCounter] & 0x0000ff);

Serial << effectCounter << "\t";
}

else if (flag == 0)
{
oldRed   = (tileBuffer[6 - effectCounter] & 0xff0000) >> 16;
oldGreen = (tileBuffer[6 - effectCounter] & 0x00ff00) >> 8;
oldBlue  = (tileBuffer[6 - effectCounter] & 0x0000ff);

Serial << (6 - effectCounter) << "\t";
}

Serial << oldRed << " " << oldGreen << " " << oldBlue << endl;

if ((newRed - oldRed != 0) || (newGreen - oldGreen != 0) || (newBlue - oldBlue != 0))
{
// Red difference
if (newRed - oldRed > 0) // Positive red change
{
oldRed ++;
}
else if (newRed - oldRed < 0) // Negative red change
{
oldRed --;
}

// Green difference
if (newGreen - oldGreen > 0) // Positive green change
{
oldGreen ++;
}
else if (newGreen - oldGreen < 0) // Negative green change
{
oldGreen --;
}

// Blue difference
if (newBlue - oldBlue > 0) // Positive blue change
{
oldBlue ++;
}
else if (newBlue - oldBlue < 0) // Negative blue change
{
oldBlue --;
}

if (flag == 1)
{
tileBuffer[effectCounter] = tile.Colour(oldRed, oldGreen, oldBlue);
}

else if (flag == 0)
{
tileBuffer[6 - effectCounter] = tile.Colour(oldRed, oldGreen, oldBlue);
}

// tileBuffer[effectCounter] = tile.Colour(oldRed, oldGreen, oldBlue);
}

else if (((newRed - oldRed) == 0) && ((newRed - oldRed) == 0) && ((newRed - oldRed) == 0))
{
effectCounter ++;  // Go to next tile
//flag = false;
}
}

else
{
flag = false;
effectCounter = 0;

if (flag == 1) // Flip flop between left and right updates
{
flag = 0;
}

else if (flag == 0)
{
flag = 1;
}
}
delay(wait);
}
``````
• Hopefully oldX and newX are signed, otherwise you will never get into if(newRed - oldRed < 0) as it is always false if one of both is unsigned. – Kwasmich May 11 '17 at 13:52
• They're not signed but it does work correctly, any idea why? – Matt May 11 '17 at 13:55
• This question might belong over at Code Review. – Johnny Mopp May 11 '17 at 14:55
• If-else is just fine. However, the function might become more readable if you split the code into multiple separate functions. – Gerben May 11 '17 at 16:18

For part of this you can use the switch case style
Which looks like

``````switch(flag) {
break;       // and exits the switch
case 1 : cout << '2';
break;
default: //all other cases
}
``````

There is also the ? syntax that can help

condition ? result_if_true : result_if_false

If I "refactor" this type of code (really we all get there from time to time) I try to go back to "what I want". From What I understand your code would look like (no code validation this is a educational example)

``````int bufferoffset=0;
int bufferMultiplyer=1;

if(flag == 0)
{
bufferoffset=6;
bufferMultiplyer=-1;
}
oldRed   = (tileBuffer[bufferoffset+bufferMultiplyer*effectCounter] & 0xff0000) >> 16;
oldGreen = (tileBuffer[bufferoffset+bufferMultiplyer*effectCounter] & 0x00ff00) >> 8;
oldBlue  = (tileBuffer[bufferoffset+bufferMultiplyer*effectCounter] & 0x0000ff);

Serial << (bufferoffset+bufferMultiplyer*effectCounter) << "\t";

Serial << oldRed << " " << oldGreen << " " << oldBlue << endl;

int redDiff=newRed - oldRed;
int greenDiff=newGreen - oldGreen ;
int blueDiff=newBlue - oldBlue ;
if(redDiff!= 0)
{
redDiff > 0?         oldRed ++:   oldRed --;
}
if(greenDiff!= 0)
{
greenDiff> 0?         oldGreen ++:   oldGreen --;
}
if(blueDiff!= 0)
{
blueDiff> 0?         oldBlue ++:   oldBlue --;
}

tileBuffer[bufferoffset+bufferMultiplyer*effectCounter] = tile.Colour(oldRed, oldGreen, oldBlue);
}
``````

for more details on switch case see http://en.cppreference.com/w/cpp/language/switch for more detail on ? syntax https://stackoverflow.com/questions/795286/what-does-do-in-c

• `if(greenDiff!= 0)` == `if(greenDiff)` – dandavis May 11 '17 at 22:15
• I personally don't like if(greendiff) because greendiff is not a boolean. I consider that style. – jantje May 11 '17 at 22:30

you can shorten a lot of the vertical space with more compact conditionals, including ternaries to remove double conditionals where one result must follow from another.

For example, you can turn this:

``````  // Green difference
if(newGreen - oldGreen > 0)  // Positive green change
{
oldGreen ++;
}
else if(newGreen - oldGreen < 0) // Negative green change
{
oldGreen --;
}
``````

into just this:

``````// Green difference
int gDiff = newGreen - oldGreen;
if(gDiff) oldGreen+= (gDiff > 0) ?  1 : -1;
``````

No, if-else is really the simplest way of dealing with that.

If you wanted to abstract it a little since you have repeated code just with separate variables, you could make a little function

``````// Note: set the right parameter variable types here
// to match your unspecified variables.
static inline int testUpDown(int a, int b) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
}

oldRed = testUpDown(newRed, oldRed);
oldGreen = testUpDown(newGreen, oldGreen);
oldBlue = testUpDown(newBlue, oldBlue);
``````

If you wanted an inline conditional variant you could do it with this:

``````oldRed = ((newRed-oldRed) ? ((newRed-oldRed)/abs(newRed-oldRed)) : 0);
``````

But who can understand that? You could always macro it:

``````#define UPDOWN(A,B) (((A)-(B)) ? (((A)-(B)) / abs((A)-(B))) : 0

oldRed = UPDOWN(newRed, oldRed);
``````

//when there are multiple if/if-else/nested if/nested if condition then obviously it creates complexity in the code. I think switch is best option to use in such conditions. plz check switch condition if it is feasible in your program. Moreover i will check it in detail if it still not works. Try.........

• @Matt please elaborate the objective of your code? – Hafeez Ahmad Fii Zue May 11 '17 at 20:12