Or perhaps it’s more universal to use a function to map from consecutive integers to any repeating sequence of integers:
#include <stdio.h> int mapModIntToSequence (int i, int mod, int x[]) { return x[i%mod]; } int main () { int i; int x[] = {2,7}; for (i = 0; i < 10; i++) { printf ("%d\n",mapModIntToSequence(i,2,x)); } }
This approach has the bonus that it also works for sequences of any length, and not just for switching between two integers.
source share