loop for 8x8 matrix

I am wondering how to loop a sketch through a specified # of times because I am multiplexing it to run an 8x8 led matrix. how do i put multiple loops in a single sketch? I learned that the led's are not on at the same time, but rather all at different times to make the illusion as if they were all on at the same time. So they would be on, off, on, off etc. How can i make an animated effect with the matrix? Thanks so much!

How many frames of animation? Do all frames display for the same amount of time?

How are up connecting?
You need typically 8 anode drivers, one for each bit in a column connected to all anodes in a row, and 8 current sinks, 1 per column to pull the cathodes in that column low.
The anodes for column 0 are driven as desired, and the cathode pulled low to light up column, then turned off.
Repeat for columns 1-7 - anodes, cathode low/high, anodes, cathode low high.

So you need 16 pins of I/O.
With a 328, you can drive the anodes directly, and use a ULN2803 as a buffer on outputs to drive the cathodes.
Makes the code a little tricky as only 1 port has 8 IO pins, and 2 of those are the serial lines.
What is typically done is use a shift register to hold the anode data, and then drive the cathode buffer from 8 pins, or use another with high current sink capability to sink the cathode current (160mA per cathode if all 8 anodes are on at one time).

Then you might do something like this:
check if 5mA have elapsed:
Yes:
turn off prior cathode drive
write current column data from columnArray
turn on current cathode drive

No:
read switches, buttons, whatever
update columnArray as needed

Repeat

If you had 2 shift registers and used SPI transfers for fast updates, then the code might look like this:

# include <SPI.h>   // use Sketch:Import Library: SPI to add this into your sketch
// pre-setup stuff
byte SSA = 9; // connects to 'latch' signal or Register Clock signal anode shift register
byte SSC = 10; // connects to 'latch' signal or Register Clock signal cathode shift register

// array to hold 8 bytes of data, test data stored to start
// assumes bit 7, 7 of matrix is at top left of the display, and array is updated left to right
byte columnArray [ ] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000,B00000000}; // 9 bytes
// array to hold walking 1's for turning on 1 cathode at a time, 1 = output goes low (TPIC6B595)
byte cathodeArray [ ] = {B10000000, B01000000, B00100000, B00010000, B00001000, B00000100, B00000010, B00000001};  // 8 bytes

byte column = 0; // counter for loops
byte cathode = 0; // counter for loops

unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
unsigned long duration = 5; // change to adjust refresh rate

unsigned long currentFrameMillis = 0;
unsigned long previousFrameMillis = 0;
unsigned long durationFrame = 500; // change to adjust display change rate


void setup(){
// setup IO pins
pinMode (SSA, OUTPUT);
digitalWrite (SSA, LOW);
pinMode (SSC, OUTPUT);
digitalWrite (SSC, LOW);
SPI.begin (); // hardware SPI - connect D13, SCK, to shift register Shift clock
                     //                        connect D11 , MOSI, to shift regiser Serial in
                      //                       D12, MISO, is not used
} // end setup

void loop(){
currentMillis = millis(); // capture the current 'time'
if (currentMillis >= (previousMillis + duration) ){ // enough time passed?
previousMillis = currentMillis; // store time for next pass
// turn off previous Cathode
digitalWrite (SSC, LOW);
SPI.transfer (0); // all cathodes off
digitalWrite (SSC, HIGH);
// write out column data
digitalWrite (SSA, LOW);
SPI.transfer (columnArray[column] );
digitalWrite (SSA, HIGH);
// turn on cathode
digitalWrite (SSC, LOW);
SPI.transfer (cathodeArray[column] );
digitalWrite (SSC, HIGH);

// set up for next column
column = column+1;
// reset to beginning if needed
if (column == 8){ column = 0;}
}
// that will run pretty fast, refreshing the matrix about 25 times a second
// now do whatever you're gonna to update the array

// array update code, simple code to move the columns from right to left

currentFrameMillis = millis(); // capture the current 'time'
if (currentFrameMillis >= (previousFrameMillis + duration) ){ // enough time passed?
previousFrameMillis = currentFrameMillis; // store time for next pass
columnArray[9] = columnArray[0];
columnArray[0] = columnArray[1];
columnArray[1] = columnArray[2];
columnArray[2] = columnArray[3];
columnArray[3] = columnArray[4];
columnArray[4] = columnArray[5];
columnArray[5] = columnArray[6];
columnArray[6] = columnArray[7];
columnArray[7] = columnArray[8];
columnArray[8] = columnArray[9];

} // end void loop

Haven't compiled or tested, should be pretty close.