This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes while loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop question" Watch "while loop question" New topic

while loop question

Gary Wilson

Joined: Jan 01, 2007
Posts: 3
Good morning

I am a complete beginner. As my main text I am using Head First Java supplemented by the web, and a couple of other books that help me along.

One of the test questions in Head First Java is to predict the output for this program.

public class Main {
public static void main(String[] args) {
int x = 0;
int y = 0;
while(x < 5) {
y = x - y;
System.out.println(x + "" + y +" ");
x = x + 1;

There are a couple of lines I just do not understand.
(and I have really tried to work this out via examples on the web etc):
1. y = x - y;
2. System.out.println(x + "" + y +" ");

Could someone please give me a really simple explanation for these:

I can run it and get the correct output but I cannot understand it.

Help much appreciated
Tony VanHorn
Ranch Hand

Joined: Dec 13, 2006
Posts: 74
First off, welcome to the Ranch. Everyone here seems to be very helpful especially to us beginners. As for the questions at hand, this is my understanding of them.

1. y = x - y

This is a basic math operation. You are taking what is stored y and subtracting it from what is stored in x and then reassigning it to y. So the first pass would be y = 0 - 0. The equal sign is the assignment operator. After that line of code is done, x still has a zero stored in it and y still has a zero in it. That will change however when you continue through the while loop.

2. System.out.println(x + "" + y +" ");

This is a basic out put method. Depending on how far you are in your studies, methods at some point will become old hat. Basically, the println() method takes in various amounts of arguments and prints them to the screen with a newline character. In this case it takes in the int values of x and y, combines them with a couple of spaces and then prints them to the screen. Check out the link on the forum post here. It seems to expain the print() and println() methods clearly.

so as you go through the while loop here is what it should kind of look like:

Hopefully that helps. Thats the way I learned to understannd working through programming examples. Good luck with your studies and enjoy it.

[ January 07, 2007: Message edited by: Tony VanHorn ]
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11143

You really don't have a question about a "while-loop" - I'd suggest reading this.

in any case, this isn't that hard. "y = x - y". the single "=" is the assignment operator. that mean "evaluate the stuff on the right, and store it in the variable on the left". so, if x = 7 and y = 2 when you get to this line, we evaluate the right. 7 - 2 = 5. Now, we store that value in what is on the left... in this case, y. so, y now becomes 5.

System.out.println(x + "" + y +" ");
the "System.out.println" is how you print something to the screen. Usually, you want to print a String, which is just a sequence of printable characters. x and y in your program are ints, and are not printable, so we use a little trick here. you are allowed to concatenate strings by using the "+" operator. so you could do "fred " + "is cool" and you end up with the string "fred is cool".

with , you are adding an int to a string. Java is kind enough to automatically convert the int to a string, in this case, changing the value of, say, 5, to the CHARACTER 5, or technically, a string that contains the character 5.

So basically, it's a quick, cheap and easy way to convert numbers to characters for printing.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Gary Wilson:
There are a couple of lines I just do not understand.
(and I have really tried to work this out via examples on the web etc):


The first subtacts y from x, then stores the result in y.

The second uses the System to print a line on what is called a command line interface - what you know as a DOS box. The machine first tries to figure out what is inside the round braces before trying to print the line. x and y in there are called variables. The plus sign continues the printing through the variables and string literals.....the quotes are called string literals because Java just prints out whatever is between the quotes without trying to figure it out. You probably need a space between the first two quotes.

The final semicolon terminates the statement of this instruction to Java.

I have made some simplifications of wording here for the sake of getting going. Just stay with the book until you are through it at least one time.

Usually two or three readings of any computer book are necessary to become effiective at it's subject matter.

Also, there is a button marked code where you post the quesition, use it and it gives you two sets of the word code with square braces around them. The second one has a slash in it. Put your code between these tags.

[ January 07, 2007: Message edited to use code tags]
[ January 07, 2007: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Gary Wilson

Joined: Jan 01, 2007
Posts: 3
You guys are great. 3 responses, thank you. I am going to study them today and will let you know how I get on.
thank you!
Gary Wilson

Joined: Jan 01, 2007
Posts: 3
Just wanted to say thank you again for the replies. I have worked through them all and studied the excellent doc on concatenation. I am now studying more basics before I continue.
I agree. Here's the link:
subject: while loop question
Similar Threads
Probably simple code needed translated into English for me
Head First page 92 Pool Puzzle
Head First Java - Can't understand the output