# Beginner here, seeking to write shorter code, please guide me!

I have a condition that is very long, but it points to the same comparison.

When is not executed, I would like to print out the one of six variables that did not comply.
Of course I could do that with 6 if-else but that’s so long…
I am trying to become a better programmer …

``````  float LagAll =3;  // this is the lag allowed
if (Lag [1]<LagAll and Lag [2]<LagAll and Lag [3]<LagAll and Lag [11]<LagAll and Lag [12]<LagAll and Lag [13]<LagAll)
{i++;  // incrementing
}

else
{Serial.printf( "L1 %.2f L2 %.2f L3 %.2f \n", Lag [1]*10, Lag [2]*10, Lag [3]*10 );
Serial.printf( " L11 %.2f L12 %.2f L13 %.2f \n", Lag [11]*10, Lag [12]*10, Lag [13]*10 );
Here I would like to print out only the Lag that exceeded 3.
``````

Is there a smart way to do this?
Thanks

Something like below; finetune to needs

``````  float LagAll =3;
for (uint8_t lagCnt = 1; lagCnt < 3; lagCnt++)
{
if (Lag[lagCnt] > LagAll)
{
Serial.printf( "L%d %.2f ", lagCnt, Lag[lagCnt] * 10);
}
}
for (uint8_t lagCnt = 11; lagCnt < 13; lagCnt++)
{
if (Lag[lagCnt] > LagAll)
{
Serial.printf( "L%d %.2f ", lagCnt, Lag[lagCnt] * 10);
}
}
``````

Maybe something like this.

``````const float LagAll = 3.0; // this is the lag allowed

void loop()
{
int i;

CheckLag(i);
}

void CheckLag(int &count)
{
// 1, 2, and 3
for (int i = 1, i < 4; i++)
{
if (Lag[i] >= LagAll)
{
Serial.print("L");
Serial.print(i);
Serial.print(" ");
Serial.println(Lag[i]);
return; // Failed the check
}
}

// 11, 12, and 13
for (int i = 11, i < 14; i++)
{
if (Lag[i] >= LagAll)
{
Serial.print("L");
Serial.print(i);
Serial.print(" ");
Serial.println(Lag[i]);
return; // Failed the check
}
}

count++; // Pased all the checks
}
``````

Great, That can work, Thanks a lot

consider

``````#define LagAll  3

byte list [] = { 1, 2, 3, 11, 12, 13 };
#define N_LIST  sizeof(list)

#define MAX_LAG 14
float lag [MAX_LAG];

int  i = 0;
char s [80];

// -----------------------------------------------------------------------------
void
dispLags (
float thresh)
{
char t [10];
for (unsigned n = 0; n < N_LIST; n++)  {
if (thresh <= lag [list [i]]) {
sprintf (s, "L%d %s, ", n, dtostrf(lag [n], 6, 2, t));
Serial.print (s);
}
}
Serial.println ("");
}

// -----------------------------------------------------------------------------
void
check (void)
{
for (unsigned n = 0; n < N_LIST; n++)  {
if (lag [ list [n]] > LagAll)  {
dispLags (LagAll);
return;
}
}

Serial.println (i++);
}

// -----------------------------------------------------------------------------
void
setup (void)
{
Serial.begin (115200);
}

void
loop (void)
{
Serial.println ("loop: ");
for (unsigned n = 0; n < N_LIST; n++)  {
if (95 < random (100))
lag [n] ++;
}
dispLags (0);

check ();
}
``````