rotary encoder limiting

I always get nervous when people who have not mastered the intricacies of the "%" operator (modulus) and are sensible enough to use a nice library which hides some complications are given long code to do interrupts.

Try

for ( int i = 0; i<20; i++ )
Serial.print(i%5) ;

To get rid of negative numbers use the abs() function