• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

while loop question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Gary
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Tony
[ January 07, 2007: Message edited by: Tony VanHorn ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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):

Gary



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 ]
 
Gary Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Gary Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Gary
 
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic