This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

array assignment within the FOR statement

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

The code above has following output:
0:0
1:0
2:0
3:0
4:0
5:0
6:0
7:0
8:0
9:0
However, if the code is changed to this:
(print out the result in another for-loop

The output is:
0:1
1:2
2:3
3:4
4:5
5:6
6:7
7:8
8:9
9:10
Why???
Thanks
kawaii
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The last part of the for loop ( the one after the second semicolon ) is executed after each iteration of the loop, not before.
In the first example the assignment to i and the comparison happen before the println statements and the assignment to the array happen after each iteration.
In the second example the print statements are executed in a separate for loop, that executes after the first one is complete and therefore all the assignments to the array are complete.
HTH
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kawaii
In your first example keep in mind that the increment part of the for loop is executed at the end of the loop.

When the first println is reached i is still 0, then at the end of the for loop, right after the println, the value of nArray[0] is set to 1 then it loops again. Now i is 1 and the value of nArray[1] is still 0 (the default), and so on. You are always printing the value of the element you ahven't reached yet.
In your second example you first loop through and assign all of the array elements a value then you print out the values.

hope that helps
[ March 11, 2002: Message edited by: Dave Vick ]
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic