# multiple conditions and else if statements vs nested case

Hey, guys, I know this is silly but I want to know which one is better

multiple conditions and else if statements:

`````` if (data != "" && over_ride == 0 && cardmode == 0 && ARQ == 0 && ARQ2 == 0)
{
data = "RFIDTx" + data;
sendDoorlock(data);
ARQ = 1;
Serial.println(data);
}

else if (data != "" && over_ride == 0 && cardmode == 1 && ARQ == 0 && ARQ2 == 0)
{
sendDoorlock(data);
ARQ = 1;
counter2 = 1;
}

else if (data != "" && over_ride == 0 && cardmode == 2 && ARQ == 0 && ARQ2 == 0)
{
data = "rfid transmitter delete:" + data;
sendDoorlock(data);
ARQ = 1;
counter2 = 1;
}

else if (data != "" && over_ride == 1 && ARQ == 0 && ARQ2 == 0)
{
data = "rfid transmitter    data:" + data;
sendControlunit(data);
ARQ2 = 1;
}
``````

nested case statements:

``````if (data != "" && ARQ == 0 && ARQ2 == 0)
{
switch (over_ride)
{
case 0:
switch (cardmode)
{
case 0:
data = "RFIDTx" + data;
sendDoorlock(data);
ARQ = 1;
Serial.println(data);
break;
case 1:
sendDoorlock(data);
ARQ = 1;
counter2 = 1;
break;
case 2:
data = "rfid transmitter delete:" + data;
sendDoorlock(data);
ARQ = 1;
counter2 = 1;
break;
}
break;
case 1:
data = "rfid transmitter    data:" + data;
sendControlunit(data);
ARQ2 = 1;
break;
}
}
``````

SWITCH CASE is different because it can only test against values that are known at compile time. That can rule it out completely for many situations.

In general I prefer nested IF (or CASE) statements because it makes it easier to deal with the ELSE parts and it makes it easier to debug because you can add print statements to show what is happening.

``````if (data != "" && over_ride == 0 && cardmode == 0 && ARQ == 0 && ARQ2 == 0)
``````

has 5 tests and so there are 25 or 32 possibilities of which only 1 is correct. When you get to the ELSE part you won’t know which of the 31 other possibilities got you there.

Another advantage of nesting is that you can arrange the tests so the one most likely to fail comes first and you don’t waste time testing the others.

…R

Maybe nesting your if statements is a lot clearer (and possibly faster).

All your if statements e.g. contain data != "". So

``````if(data!="")
{
if ...
}
``````

Thanks :)