• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about book example / for loop issue  RSS feed

 
Simon Penders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i am in a next chapter of a book, and i thought i truly understood the for loop, so i don't understand why i am stuck. I am learning about Array's and came across this code:



I was thinking, how does this work? Because I've learned from the past, a for loop with one statement keeps looping until it's false while executing only that one statement. So in the first for loop, 9 indexes are 'called' or so it seems, before the program goes to the second loop. But then it should print 9 and not 0 until 9. The program however does print 0 until 9. I don't get it, how is the index called each time of sample? And it looks like it's looping both for loops, and looping both again until it reaches 9, but this is different from what I've learned about for loops with one statement only. I tested some stuff myself and wrote my own version:



Funny thing is.. it works the same and the output is the same. Only I've used only 1 for loop. Could someone please explain why 2 for loops were used in the book example and how this exactly works? Because I'm confused right now lol.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the example had two separate loops because programs tend to initialize an array in one place in a program, and print the array (usually for debugging purposes) in another place. From a functioning point of view your 1-loop example yields the same output.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What output were you expecting? In your example from the book the first loop loops through each index in the array and assigns a value to it. The second loops loops through the array again and prints each value out.

In the example you wrote the program only loops through the array once, but assigns the value and prints it straight away.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such code comes from.

That is a standard format for a for loop to iterate an array. You may have an old edition there; since Java5, most people would use a for‑each loop for the second loop. you should also query the formatting of that code. Have a look at our formatting suggestions. While you do that, I shall quote the code you posted and correct its formatting, along with additional {}s.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the code would look like with all the indentation in.
There is still a stylistic problem and a potential logic problem. I shall correct them in the next post. I have maintained the K&R indentation you had, though the Ranch suggestions use BSD/Allman indentation. . . .
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One stylistic problem is that you usually use i++ rather than i = i + 1
Another stylistic problem is that the type of your variable is int‑array and the book shows it as int. Put the square brackets on the left, not on the right:-
int[] numbers = new int[10];
not int numbers[]…

There is a potential logic problem in that you are using the String literal 10 in the loop header. Don't use number literals like that, just in case you make a mistake and the numbers are different. Use myArray.length
I would not have chosen sample as a name for an array myself.

They are right to say it is a one‑dimensional array, since Java® only supports 1D arrays. I know you think you have seen 2D arrays, but you haven't. You have seen arrays of arrays which are different from (and better than) 2D arrays. . . .
In the first example you set up a 10‑element array. The loops all work the same way. You start from 0 which corresponds to the 1st element of the array, then increment i as long as i is less than sample.length. So i can be 0 1 2 3 4 5 6 7 8 9 but not 10. If you count those numbers you will see that you are running the loop 10× and the last number 9 is exactly right for the last element in your 10‑element array.
I presume you can work out the contents of the loops yourself; they are pretty straightforward. The {} should be included in all loops so as to delimit the extent of the loop.

I don't like leaving arrays “empty”. I would prefer to fill them immediately, if at all possible. So you can use an array initialiser
int[] sample = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
… or even in Java8, try a Stream
int[] myArray = IntStream.range(0, 10).toArray();
Look up all the links in that previous line if you are on Java8. Also look at the range method and rangeClosed and see what the difference is.

The reason for filling the array as soon as possible is more important with Object[]s; if you don't fill them, they are full of nulls and nulls are nasty horrible dangerous things.

[edit]Move the square brackets to the left.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to Campbell:
How for loop works
It first do assignment work
Then checks the condition
Then executes the body of for loop
Then increments the value of variable
 
Simon Penders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the answers. Sorry about forgetting some rules. The source is from: java A Beginner's Guide, 4th - Herbert Schildt. The book is from 2007, so a little older. So some answers here are a little too much yet But i think i understand it better now. My for loop thinking was right, but i was confused by the array. Just starting to learn about them.

So if I'm correct in the first loop the 10 elements of the array called sample are stored with the numbers 0 until 9? I was a little confused about: sample[i] = i; and how this works. So when you start using an Array it basically starts counting from 0 until the last element (in this case 10, because that's the size in this case) and the i between [] gives the element a number value each count, so it can be printed in the next loop. Correct thinking this way?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The loop, as it is written, counts from 0 to 9. The last element is No 9. That is why you use the < operator. Yes, the ten elements in your array are numbered 0…9, and the loop written like that will always give the correct figures for your array indices, never mind how large or small the array is.
There are 10 values in your array and each value can be assigned with the = operator. So if you write
array[5] = 5;
you set 5 as the value of the (5 + 1)th = 6th value of the array. The different values of i in that loop mean you go from
array[0] = 0;
to
array[9] = 9;
in order.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!