• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding input solution  RSS feed

 
Dr Robert Wilson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really new to Java are for that matter computer science period. I have been asked to cumpute the value of x after the following code is executed? int x = 10; for (int y = 5; y<20; y=y+5)
X=x+y
For me that means that means I have an invalid statement am I wrong?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you have an invalid statement?
 
Dr Robert Wilson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
X=10
Y= 5
Then 10=10+y then y= 0
10=10+ 0
10-10=0
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Drrobert Wilson, Welcome to Ranch.

If you use code tagsthen your code will glow like this,


Have you tried running you code? It would answer your question.

Two things come to my mind,
1. Every java statements ends with a semicolon.
2. Java is case sensitive, so A and a are different.

Drrobert Wilson wrote:X=10
Y= 5
Then 10=10+y then y= 0
10=10+ 0
10-10=0


Try writing your code in plane English. For example, int x = 0; will be Initializing integer x to 0.
 
Richard Warner
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm brand new to Java too, fellow Greenhorn! Since I've so recently learned it, maybe this can help: I find some symbols we may know from english have ever so subtle differences in Java. "=" here is not really "equals." "X=x+y" should read more like "X is now set to the value of x+y." Or "from now on, plug in x and y whenever I mention variable X." Though that's possibly backwards of the actual order or how something might be processed, it helps for sussing out later "what in the world was I doing with x."

Java does this value setting using the original value so much, folks often will even write this formula in a slightly abbreviated format (I scoffed at first, but it is way easier to type!) If you're using the same x both as your new value and also as part of how you got to this new value: "x += y" says the same thing as "x = x + y"

The double equals, "==" is closer to what might be anglicized as "equals." and seems to be used more for testing things (like in if statements) while the single equals is used primarily in "primitive" assignment. (Plugging values or other variables into the various different species of variables). At least, that's how I'm conceptualizing it myself so far, others more familiar with it will have clearer conceptualizations.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On entering the "for" loop
x = 10, y = 5
execute x = x + y --> x = 10 + 5
x = 15, y = 5
move on to next value of loop (y = y + 5)
x = 15, y = 10
execute x = x + y
...
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Drrobert Wilson wrote:For me that means that means I have an invalid statement am I wrong?

You do know that all statements have to be inside a method (or constructor, or initialization block) which in turn has to be inside a class, don't you?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does X come from? If you mean x you need to write x since X is different.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Drrobert Wilson wrote:I am really new to Java are for that matter computer science period. I have been asked to cumpute the value of x after the following code is executed? int x = 10; for (int y = 5; y<20; y=y+5)
X=x+y
For me that means that means I have an invalid statement am I wrong?


Drrobert, could you please post the full source? This will give us a good indication of on which part you need more explanation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!