# Tell me how this loop works.

• 05-13-2010, 02:00 AM
hydride
Tell me how this loop works.
So what i have in my head is this:
Step 1) arr1[i] = arr1[1-1] + 1
Step 2) arr1[i] = arr1[0] + 1
Step 3) ???

what is most troubling is that there is arr1[0] which i dont know how it existed from the above steps. Next is how arr1[0] is 0 when arr1[0] doesn't even exist. Shouldn't it have started out with arr1[1]? Since i = 1?

Code:

```                for (i = 1;i <= max;i++)                         arr1[i] = arr1[i-1] + i;;```
the whole code is this:
Code:

```System.out.print("Please enter the max number:");                 int max = input.nextInt();                 int[]arr1 = new int[max+1];                 int[]arr2 = new int[max+1];                 int[]arr3 = new int[max+1];                 int i = 1;                 // For-loop to calculate                 for (i = 1;i <= max;i++)                         arr1[i] = arr1[i-1] + i;                                 for (i = 1;i <= max;i++)                         arr2[i] = arr2[i-1] + i;                                 for (i = 1;i <= max;i++)                         arr3[i] = arr3[i-1] + i;                                 for (i = 0; i <= max; i++)                         System.out.println("Arr1 " + arr1[i] + " Arr2 " + arr2[i] + " Arr3 " + arr3[i]);                 System.out.println("Sum of All is " + arr1[max]);```
what it prints out:
Code:

```Please enter the max number:5 Arr1 0 Arr2 0 Arr3 0 Arr1 1 Arr2 1 Arr3 1 Arr1 3 Arr2 3 Arr3 3 Arr1 6 Arr2 6 Arr3 6 Arr1 10 Arr2 10 Arr3 10 Arr1 15 Arr2 15 Arr3 15 Sum of All is 15```
Any help would be appreciated. Thanks!:)
• 05-13-2010, 02:39 AM
gcalvin
Java implicitly initializes all uninitialized int variables to 0, including elements of int[] arrays. It's sloppy practice to let the compiler do that, though. It's much better to initialize variables yourself, for the sake of clarity and readability. And arrays always start at index 0. If you create an int[max + 1] array, it will have max + 1 elements (obvious, isn't it?), ranging from 0 to max.

-Gary-
• 05-13-2010, 04:00 AM
hydride
Thanks.
But can you explain how the program runs it? Similar to how I wrote down the steps previously.
• 05-13-2010, 04:30 AM
gcalvin
Quote:

Originally Posted by hydride
Thanks.
But can you explain how the program runs it? Similar to how I wrote down the steps previously.

I'll try, but the program as a whole doesn't make a lot of sense to me.
Code:

```System.out.print("Please enter the max number:");                 int max = input.nextInt();```
You get an int from the user, and save it as max. In your sample run, it was 5.
Code:

```                int[]arr1 = new int[max+1];                 int[]arr2 = new int[max+1];                 int[]arr3 = new int[max+1];```
You create three int[] arrays, all of which go from arrX[0] to arrX[max] (arrX[5] in our sample run). The compiler implicitly initializes all array elements to 0.
Code:

`                int i = 1;`
This line is pretty much useless, except that it saves declaring i in each of the for loops below. The initialization to 1 is completely meaningless, because i is re-initialized in each for loop.
Code:

```                // For-loop to calculate                 for (i = 1;i <= max;i++)                         arr1[i] = arr1[i-1] + i;```
1st time through loop: arr1[1] = arr1[0] + 1 = 0 + 1 = 1
2nd time through loop: arr1[2] = arr1[1] + 2 = 1 + 2 = 3
3rd time through loop: arr1[3] = arr1[2] + 3 = 3 + 3 = 6
4th time through loop: arr1[4] = arr1[3] + 4 = 6 + 4 = 10
5th time through loop: arr1[5] = arr1[4] + 5 = 10 + 5 = 15
Code:

```                                for (i = 1;i <= max;i++)                         arr2[i] = arr2[i-1] + i;                                 for (i = 1;i <= max;i++)                         arr3[i] = arr3[i-1] + i;```
You do the exact same thing with the other two arrays, for no reason I can guess, and of course you get the same results in those arrays.
Code:

```                        for (i = 0; i <= max; i++)                         System.out.println("Arr1 " + arr1[i] + " Arr2 " + arr2[i] + " Arr3 " + arr3[i]);                 System.out.println("Sum of All is " + arr1[max]);```
You print out the results, but "Sum of All" seems kind of meaningless -- what you're reporting is just the contents of the last element of the first array.

Hope that helps.

-Gary-
• 05-13-2010, 04:38 AM
hydride
awesome explanation!:D
But when you run it, why does it print out 0 first and then 1, 3, 6...?

I.E.
1st time through loop: arr1[1] = arr1[0] + 1 = 0 + 1 = 1
2nd time through loop: arr1[2] = arr1[1] + 2 = 1 + 2 = 3
3rd time through loop: arr1[3] = arr1[2] + 3 = 3 + 3 = 6
4th time through loop: arr1[4] = arr1[3] + 4 = 6 + 4 = 10
5th time through loop: arr1[5] = arr1[4] + 5 = 10 + 5 = 15
• 05-13-2010, 04:40 AM
gcalvin
Quote:

Originally Posted by hydride
awesome explanation!:D
But when you run it, why does it print out 0 first and then 1, 3, 6...?

I.E.
1st time through loop: arr1[1] = arr1[0] + 1 = 0 + 1 = 1
2nd time through loop: arr1[2] = arr1[1] + 2 = 1 + 2 = 3
3rd time through loop: arr1[3] = arr1[2] + 3 = 3 + 3 = 6
4th time through loop: arr1[4] = arr1[3] + 4 = 6 + 4 = 10
5th time through loop: arr1[5] = arr1[4] + 5 = 10 + 5 = 15

Because your reporting loop starts at i = 0, not at i = 1.

-Gary-
• 05-13-2010, 04:50 AM
hydride
Ohhh. Wow, i did not see that. Thank you so much!