How to create a loop where loop changes every post?

The PHP modulus operator gives you the remainder from dividing 2 numbers. The remainder from the division of any number by two is either 0 or 1, so we can use that to provide an “alternator”. We use the current_post var as a counter, which is available in any WP_Query object:

while( have_posts() ){
    the_post();

    if( $wp_query->current_post % 2 ){
        echo 'right';
    } else {
        echo 'left';
    }

}