• Post Reply Bookmark Topic Watch Topic
  • New Topic

basic code  RSS feed

 
Ankita Mittal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

please give a line by line explaination.
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • 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.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • 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.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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: 2240
28
  • Mark post as helpful
  • send pies
  • 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:
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:



Thank you
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!