Pablito
Pablito

Reputation: 11

How to do this task with `while` loop?

I don't know how to convert from for-loop to while-loop all my attempts have failed. I have the following code:

public static void main(String[] args) {
    int[][] arr = new int[4][];
    arr[0] = new int[4];
    arr[1] = new int[2];
    arr[2] = new int[1];
    arr[3] = new int[3];

int counter = 1;

How can I convert this for-loops to while-loops?

 //fill arr numbers from 1 to 10
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = counter++;
            }
        }

    for (int i = 0; i < arr.length; i++) {
            System.out.print(" arr [ " + i + " ] = ");
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println(" ");
        }

    }
}

Output:

arr [ 0 ] = 1 2 3 4  
arr [ 1 ] = 5 6  
arr [ 2 ] = 7 
arr [ 3 ] = 8 9 10

Upvotes: 0

Views: 99

Answers (4)

LuisFerrolho
LuisFerrolho

Reputation: 417

for loop and while loop just differ in their syntax. The equivalent of a for loop:

for(int i = 0; i < arr.length; i++) {
    ...
}

is

int i = 0;
while(i < arr.length) {
    ...
    i++;
}

Below is your code but with while loops:

public static void main(String[] args) {
    int[][] arr = new int[4][];
    arr[0] = new int[4];
    arr[1] = new int[2];
    arr[2] = new int[1];
    arr[3] = new int[3];

    int counter = 1;
    int i = 0, j = 0; // Initialize vars for while loops

    //fill arr numbers from 1 to 10     
    while(i<arr.length){
        while(j<arr[i].length){
            arr[i][j] = counter++;
            j++; // Increment as in for loops
        }
        i++; // Increment as in for loops
        j = 0; // Restart j to the next outer loop step
    } 

    i = 0; // Reset i for outer loop
    // No need to j=0 since when the above loop ends, j will reset to 0

    while(i<arr.length){
        System.out.print(" arr [ " + i + " ] = ");
        while(j<arr[i].length){
            System.out.print(arr[i][j] + " ");
            j++; // Increment as in for loops
        }
        i++; // Increment as in for loops
        j = 0; // Same objective as in the loop above
        System.out.println(" ");
    }
}

Explanations in comments, hope it helped!

Upvotes: 0

nafas
nafas

Reputation: 5423

for loop is basically a compact way of writing while loop.

for (int i = 0; i < arr.length; i++) { 
  ... 
}

is same as :

int i=0;
while(i<arr.length){
  ....
  i++;

}

Upvotes: 2

Sasi Kumar
Sasi Kumar

Reputation: 13348

int i = 0;
while ( i < arr.length ) {
int j = 0;
    while ( j < arr[i].length;)
       {
        arr[i][j] = counter++;
       j++;
       }
i++;
}

Then print value

int i = 0;
while ( i < arr.length ) {
    System.out.print(" arr [ " + i + " ] = ");
    int j = 0;
    while ( j < arr[i].length) {
        System.out.print(arr[i][j] + " ");
     j++;
    }
    System.out.println(" ");
i++;
}

Upvotes: 0

Akshay Bahadur
Akshay Bahadur

Reputation: 517

while(int i <arr.length)
{
   while(int j<arr[i].length)
   {
     arr[i][j] = counter++;
    j++;
   }
i++;
}

 while(int i <arr.length)
{
   System.out.print(" arr [ " + i + " ] = ");
   while(int j<arr[i].length)
   {
     System.out.print(arr[i][j] + " ");
    j++;
   }
 System.out.println(" ");
i++;
}

This might help you.

Upvotes: 0

Related Questions