2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Why are nested loops considered bad practice?

what is main problem when we use  nested loop?
userimage

Nested loops are fine as long as they describe the correct algorithm.Nested loops have performance considerations but sometimes it's exactly the correct algorithm, e.g. when you need to access every value in a matrix.

example for nested loop:-

public class MatrixMultiplication {

   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       System.out.print("Enter number of rows in A: ");
       int rowsInA = s.nextInt();
       System.out.print("Enter number of columns in A / rows in B: ");
       int columnsInA = s.nextInt();
       System.out.print("Enter number of columns in B: ");
       int columnsInB = s.nextInt();
       int[][] a = new int[rowsInA][columnsInA];
       int[][] b = new int[columnsInA][columnsInB];
       System.out.println("Enter matrix A");
       for (int i = 0; i < a.length; i++) {
           for (int j = 0; j < a[0].length; j++) {
               a[i][j] = s.nextInt();
           }
       }
       System.out.println("Enter matrix B");
       for (int i = 0; i < b.length; i++) {
           for (int j = 0; j < b[0].length; j++) {
               b[i][j] = s.nextInt();
           }
       }
       int[][] c = multiply(a, b);
       System.out.println("Product of A and B is");
       for (int i = 0; i < c.length; i++) {
           for (int j = 0; j < c[0].length; j++) {
               System.out.print(c[i][j] + " ");
           }
           System.out.println();
       }
   }

   public static int[][] multiply(int[][] a, int[][] b) {
       int rowsInA = a.length;
       int columnsInA = a[0].length; // same as rows in B
       int columnsInB = b[0].length;
       int[][] c = new int[rowsInA][columnsInB];
       for (int i = 0; i < rowsInA; i++) {
           for (int j = 0; j < columnsInB; j++) {
               for (int k = 0; k < columnsInA; k++) {
                   c[i][j] = c[i][j] + a[i][k] * b[k][j];
               }
           }
       }
       return c;
   }
}

A three level nested loop is used to perform the multiplication.









Answer is