# Basic help here please....for each loop

• 12-28-2008, 02:16 AM
selam
Basic help here please....for each loop
Hallo All;

I have a question please regarding For-Each style for loop, just puzzeled i'm new to java and i'm reading a very wonderful book "Java a Beginner's guide" by Herbert Schildt. here goes the code and my question:

Code:

```class ForEach2{         public static void main(String args[]){                 int sum = 0;                 int nums[] [] = new int [3] [5];             for(int i = 0; i < 3; i++)                 for(int j = 0; j < 5; j++)                   nums[i][j] = (i+1)*(j+1);             for(int x[] : nums) {                 for(int y : x)  {                   System.out.println("value is : " + y);                   sum += y;                   }                 }                 System.out.println("Summation: " +sum);       }     }```
the out put from this program is

value is: 1
value is: 2
value is: 3
value is: 4
value is: 5
value is: 2
value is: 4
value is: 6
value is: 8
value is: 10
value is: 3
value is: 6
value is: 9
value is: 12
value is: 15

Summation: 90

we have here four 'for' loops, i am just lost. can any way explain to me please. what value does exactly has 'y' here? i know for some of you it is very easy but i cann't get it.

• 12-28-2008, 02:55 AM
hardwired
Pick things apart a little bit and play with it to see what's going on.
Like this:
Code:

```public class Test {     public static void main(String[] args) {         int sum = 0;         int[][] nums = new int [3] [5];         for(int i = 0; i < 3; i++)             for(int j = 0; j < 5; j++)                 nums[i][j] = (i+1)*(j+1);         print(nums);         for(int[] x : nums) {             print(x);             for(int y : x)  {                 System.out.println("value is : " + y);                 sum += y;             }         }         System.out.println("Summation: " +sum);     }     private static void print(int[][] n) {         for(int i = 0; i < n.length; i++) {             System.out.printf("num[%d] = ", i);             print(n[i]);         }     }     private static void print(int[] n) {         System.out.print("[");         for(int i = 0; i < n.length; i++) {             System.out.print(n[i]);             if(i < n.length-1) {                 System.out.print(", ");             } else {                 System.out.print("]\n");             }         }     } }```
• 12-28-2008, 05:58 AM
diggitydoggz
i'm confused about that syntax... newbie here too. when the for loop says for (int x[] : nums) does that mean its going through each value of the two dimensional array? i've never yet encountered that syntax of int x[].
• 12-28-2008, 08:39 AM
tghn2b
what is this for(int x[] : nums)
& what is this for(int y : x)
what does the ':' do?

and you can read more here h t t p : / /
java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

I think what I wrote below is a waste of time

Because we have 15 "value is: "
I take it, it is only using the i++ and J++
value is: 1 // i = 0 j = 0
value is: 2 // i = 0 j = 1
value is: 3 // i = 0 j = 2
value is: 4 // i = 0 j = 3
value is: 5 // i = 0 j = 4
value is: 2 // i = 1 j = 0
value is: 4 // i = 1 j = 1
value is: 6 // i = 1 j = 2
value is: 8 // i = 1 j = 3
value is: 10 // i = 1 j = 4
value is: 3 // i = 2 j = 0
value is: 6 // i = 2 j = 1
value is: 9 // i = 2 j = 2
value is: 12 // i = 2 j = 3
value is: 15 // i = 2 j = 4
• 12-28-2008, 09:03 AM
hardwired
Syntax of the enhanced for loop, aka, the for-each loop. For more about this see The For–Each Loop.
• 12-28-2008, 09:11 AM
tghn2b
OK it seems to be also called "for-in loop."

so there is not
Quote:

we have here four 'for' loops,
There is two "for loops" and two "for-in loops."

bugger://leepoint.net/notes-java/flow/loops/foreach.html
• 12-28-2008, 12:50 PM
selam
Ohh thanks All!

hardwired let me try ur way and let me read the links i'll be back with result
hopefully will get it all.

thanks again
• 12-28-2008, 01:57 PM
selam
Hi again

so far so good,

Code:

`print(nums);`
prints these three arrays, am i right?

num[0] = [1, 2, 3, 4, 5]
num[1] = [2, 4, 6, 8, 10]
num[2] = [3, 6, 9, 12, 15]

and the
Code:

`print(x)`
does this
[1, 2, 3, 4, 5]

and
Code:

`sum += y;`
does this

[2, 4, 6, 8, 10]

and last what i got is
Code:

`for(int y : x)`
the value of x is in y right?

thanks again for the links and info.
i will be back with more puzzels, just want to understand how java is hopefully i'm in a good hand here :)
• 12-28-2008, 02:45 PM
Nicholas Jordan
don't go buggy at Whip and Buggy Software Company
for(int y : x) would be read, for each int in x, which to answer tghn2b and diggitydog, the elements may in fact be ints, arrays, could even be SomeObsucreClassWroteToBeABigObscureClass. or an array of them. If the elements of an array are arrays, you may use the variable[index] code to "talk" to each element in the sub-array.

We see some bizzare looking sytax in Artificial Intelligence that looks like:
Code:

`variable = first[index_1][index_2].getValue();`
Or one which I use all the time:
Code:

`System.out.println(array[index].toString());`
which I use for de-bugging, so called because you go buggy trying to use de-buggers.

de buggers are full of bugs, an ......

oh, well. sorta gets re-cursive.....
• 12-28-2008, 04:44 PM
selam
thx Nicholas Jordan