user-1: I'm using multiple expander chips for my project and I was wondering if there is a way to shorten the loop code by using array? So that I don't have a super long code.
void setup() {
  mcp.begin(0);
  mcp1.begin(1);
  mcp.pinMode(0, OUTPUT);
  mcp.pinMode(1, OUTPUT);
  mcp.pinMode(2, OUTPUT);
  mcp.pinMode(3, OUTPUT);
  mcp.pinMode(4, OUTPUT);
  mcp.pinMode(5, OUTPUT);
  mcp.pinMode(6, OUTPUT)
  mcp.pinMode(7, OUTPUT);
  mcp1.pinMode(0, OUTPUT);
  mcp1.pinMode(1, OUTPUT);
  mcp1.pinMode(2, OUTPUT);
  mcp1.pinMode(3, OUTPUT);
  mcp1.pinMode(4, OUTPUT);
  mcp1.pinMode(5, OUTPUT);
  mcp1.pinMode(6, OUTPUT);
  mcp1.pinMode(7, OUTPUT);
}

void loop() {
  mcp.digitalWrite(0, LOW); 
  mcp.digitalWrite(1, LOW); 
  mcp.digitalWrite(2, LOW); 
  mcp.digitalWrite(3, LOW);
  mcp.digitalWrite(4, LOW); 
  mcp.digitalWrite(5, LOW); 
  mcp.digitalWrite(6, LOW); 
  mcp.digitalWrite(7, LOW);
  mcp1.digitalWrite(0, LOW); 
  mcp1.digitalWrite(1, LOW); 
  mcp1.digitalWrite(2, LOW); 
  mcp1.digitalWrite(3, LOW);
  mcp1.digitalWrite(4, LOW);
  mcp1.digitalWrite(5, LOW); 
  mcp1.digitalWrite(6, LOW); 
  mcp1.digitalWrite(7, LOW);
}


user4143: If you are using the Adafruit MCP23017 library there is a shortcut you can use for setting all the GPIO pins in one go:
mcp.writeGPIOAB(val);

val is a 16-bit value which defines the IO level of all 16 pins at once.
There is a similar function for reading:
val = mcp.readGPIOAB();

You can also read just one bank of GPIO pins (why they didn't have a similar write function I have no idea):
valA = mcp.readGPIO(0);
valB = mcp.readGPIO(1);

Unfortunately there aren't any shortcut functions for setting the pin modes.

user31105: Use a variable ( iterator) and count from 0 to 8 (excluding):
void setup() 
{
    mcp.begin(0);
    mcp1.begin(1);

    for (uint8 pin = 0; pin  < 8; pin ++)
    {
        mcp .pinMode(pin, OUTPUT);
        mcp1.pinMode(pin, OUTPUT);
    }
}

void loop() 
{
    for (uint8 pin = 0; pin < 8; pin++)
    {
        mcp .digitalWrite(pin, LOW); 
        mcp1.digitalWrite(pin, LOW); 
    }
}

If the order in which the digital pins are written to LOW matters than split them like in your example, (however, I do not think that is the case).
void loop() 
{
    for (uint8 pin = 0; pin < 8; pin++)
    {
        mcp .digitalWrite(pin, LOW); 
    }

    for (pin = 0; pin < 8; pin++)
    {
        mcp1.digitalWrite(pin, LOW); 
    }
}

Note : for clarity I would call the mcp variables mcp1 and mcp2 instead of mcp resp mcp2, or an even more intuitive name if there is a clear function for both mcp's.
Using uint8 creates an unsigned byte instead of an int which saves also one byte of stack memory.

-1: <p>I'm using multiple expander chips for my project and I was wondering if there is a way to shorten the loop code by using array? So that I don't have a super long code.</p>

<pre><code>void setup() {
  mcp.begin(0);
  mcp1.begin(1);
  mcp.pinMode(0, OUTPUT);
  mcp.pinMode(1, OUTPUT);
  mcp.pinMode(2, OUTPUT);
  mcp.pinMode(3, OUTPUT);
  mcp.pinMode(4, OUTPUT);
  mcp.pinMode(5, OUTPUT);
  mcp.pinMode(6, OUTPUT)
  mcp.pinMode(7, OUTPUT);
  mcp1.pinMode(0, OUTPUT);
  mcp1.pinMode(1, OUTPUT);
  mcp1.pinMode(2, OUTPUT);
  mcp1.pinMode(3, OUTPUT);
  mcp1.pinMode(4, OUTPUT);
  mcp1.pinMode(5, OUTPUT);
  mcp1.pinMode(6, OUTPUT);
  mcp1.pinMode(7, OUTPUT);
}

void loop() {
  mcp.digitalWrite(0, LOW); 
  mcp.digitalWrite(1, LOW); 
  mcp.digitalWrite(2, LOW); 
  mcp.digitalWrite(3, LOW);
  mcp.digitalWrite(4, LOW); 
  mcp.digitalWrite(5, LOW); 
  mcp.digitalWrite(6, LOW); 
  mcp.digitalWrite(7, LOW);
  mcp1.digitalWrite(0, LOW); 
  mcp1.digitalWrite(1, LOW); 
  mcp1.digitalWrite(2, LOW); 
  mcp1.digitalWrite(3, LOW);
  mcp1.digitalWrite(4, LOW);
  mcp1.digitalWrite(5, LOW); 
  mcp1.digitalWrite(6, LOW); 
  mcp1.digitalWrite(7, LOW);
}
</code></pre>
 4143: <p>If you are using the <a href="https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library" rel="nofollow noreferrer">Adafruit MCP23017 library</a> there is a shortcut you can use for setting all the GPIO pins in one go:</p>

<pre><code>mcp.writeGPIOAB(val);
</code></pre>

<p><code>val</code> is a 16-bit value which defines the IO level of all 16 pins at once.</p>

<p>There is a similar function for reading:</p>

<pre><code>val = mcp.readGPIOAB();
</code></pre>

<p>You can also read just one bank of GPIO pins (why they didn't have a similar write function I have no idea):</p>

<pre><code>valA = mcp.readGPIO(0);
valB = mcp.readGPIO(1);
</code></pre>

<p>Unfortunately there aren't any shortcut functions for setting the pin modes.</p>
 31105: <p>Use a variable ( iterator) and count from 0 to 8 (excluding):</p>

<pre><code>void setup() 
{
    mcp.begin(0);
    mcp1.begin(1);

    for (uint8 pin = 0; pin  &lt; 8; pin ++)
    {
        mcp .pinMode(pin, OUTPUT);
        mcp1.pinMode(pin, OUTPUT);
    }
}

void loop() 
{
    for (uint8 pin = 0; pin &lt; 8; pin++)
    {
        mcp .digitalWrite(pin, LOW); 
        mcp1.digitalWrite(pin, LOW); 
    }
}
</code></pre>

<p>If the order in which the digital pins are written to LOW matters than split them like in your example, (however, I do not think that is the case).</p>

<pre><code>void loop() 
{
    for (uint8 pin = 0; pin &lt; 8; pin++)
    {
        mcp .digitalWrite(pin, LOW); 
    }

    for (pin = 0; pin &lt; 8; pin++)
    {
        mcp1.digitalWrite(pin, LOW); 
    }
}
</code></pre>

<p>Note : for clarity I would call the mcp variables mcp1 and mcp2 instead of mcp resp mcp2, or an even more intuitive name if there is a clear function for both mcp's.</p>

<p>Using uint8 creates an unsigned byte instead of an int which saves also one byte of stack memory.</p>
