breaking loop when x[n] == x[n+1]