implemented nested loops