Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Code trouble  RSS feed

 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!
Please don't be rude, just help the beginer to understand Java ))
So I have a following code:



After compilation in command line the result is next:

00 11 21 32 42

What does line 8 means? x = x+1? Why it stands after command, which already prints the result?
Thanks!



 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x = x + 1 means take the value that is stored in x and add one to it, then set x to this new value. This is done so that the while loop will have a counter. The loop continues while x is less than 5.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch! Most people will not be rude; this is a forum for beginners. Here are a few tips to write a better post:

* Write a good subject line "Code Trouble" doesn't explain what your quest is. Maybe "What does x= x + 1 mean?" would be a better subject line.

* Thanks for trying to use code tags. I edited your post to correct the what you did wrong. You don't need to add line numbers; this will be done automatically if you use the code tags correctly. The code needs to go between the opening and closing tags. This link will help you with that.
 
Yerlan Toleibergen
Greenhorn
Posts: 9
Java MySQL Database Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum, thanks a lot for your feedback! it's because of my first post here, next time i'll try to keep all requirments for a better post.
Thanks again!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in slightly more detail...

any time you see a single "=" sign, it means "figure out what the right-hand side (rhs) evaluates to, then put that into whatever is on the left-hand side (lhs)".

so when you see



you first need to look at the rhs. In this case, it is "x + 1". So we figure out what x currently is, and add one to it. Once we have that value, the line basically becomes:

x = <the value we just calculated>

Doesn't matter HOW we calculated it, or what was used...we have a value, so now we put it into the "x" variable.

In this case, we have ultimately just increased the value of x by 1. There are many ways to do this, but what you have here is a common idiom.

Why is it after the print statement? because you are in a loop. That print statement is getting executed five times. Each time, it prints one set of those pairs of numbers. and unless you somehow change the value of x from 0, you'll stay in the loop forever.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!