• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

basic code

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

please give a line by line explaination.
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Ankita!

When you post code, surround it with [code] and [/code] so that it appears nicely formatted. You can use the "Preview" button to check that your post looks okay before you submit it. I have added code tags for you this time.
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that the for-loop is confusing, because it performs an assignment in the loop condition expression. The following would be more clear:

I've renamed the array because the loop is more easily readable now.

Note that x starts with 1, thereby skipping the first element of the array. I don't know if this was the intention of the original program.
 
Rancher
Posts: 5146
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement in the tutorial about for loop execution isn't clear about when the middle expression is evaluated:

When the termination expression evaluates to false, the loop terminates.


It would be clearer if it said the expression was evaluated BEFORE the loop was started. It is stated in the language specifications.
That means that the ++x is executed BEFORE the loop starts giving x an initial value of 1.
 
Sheriff
Posts: 9059
667
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:That means that the ++x is executed BEFORE the loop starts giving x an initial value of 1.


Not sure I understood you correctly, but for now it doesn't sound right.

For loop contains statements, they are executed in order starting from left to right as they would be written from top to down.
Did I understand correctly, that you say line 2 gets executed before line 1?
 
Liutauras Vilda
Sheriff
Posts: 9059
667
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the reference what tutorials actually say:

Oracle Java Tutorials wrote:The initialization expression initializes the loop; it's executed once, as the loop begins.
When the termination expression evaluates to false, the loop terminates.
The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.


I might know what you mean, that it doesn't say explicitly that every time it gets evaluated when loop iterates. Well, probably authors imply that word "loop" already explains that. And not to get confused, the first statement says that initialization happens only once.
 
Norm Radder
Rancher
Posts: 5146
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Not sure I understood you correctly, but for now it doesn't sound right.


I meant to say that the termination expression is evaluated BEFORE any statements in the loop are executed.  That means the ++x is executed BEFORE the loop starts and gives x a value of 1 for the first time through the loop.

I was referring to the original code:
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to code ranch,

(I know that it's already a bit too late to ask, since people have already given an explanation for the code ...)

Why do you need an explanation for that piece of code ?
Why would you name a class "_" and have variables with names like "$", "__A_V_" ?

Although your topic states "Basic code", I would term it as horrible code It would be a nightmare for someone to maintain code that looks like this.
 
Ankita Mittal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:




Thank you

 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic