This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Just a Basic Question  RSS feed

Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone... I've just started java programing a couple days ago and i really love it so far... but i just want to ask somebody a real quick and easy question just to see if im tracking... My question has to do with the "while" loop command i made small app called Loopy and this is what the code is.. its real small

public class Loopy {
public static void main (String[] args) {
int x = 1;
System.out.println("Before the loop");
while (x < 4) {
System.out.println("In the Loop");
System.out.println("Value of x is " + x); //--THIS IS WERE MY QUESTION IS
x = x +1;

Ok i hope you see were my question is... ok so far x is 1 and the loop will run because 1>4.. got it.. now.. the part that i need to know if im tracking is were it says "value of x(meaning=1) is "(what does this mean?does is mean its recalling the value of x?) + x)

at the end does it mean all together that x + x then? which would be 2 right?

then on the next command

x = x +1;

ok so that first x on the line is going to be the 'new' value of x right?
and the secound x is going to be the current value which is 2 so its 2 + 1 new value = 3?

so i guess the loop ends because of the } that stops the loop right?
because the while loop ran because of the boolean test was true to begin with... but the loop can stop anytime with the '}' curly brace?

please comment on this...
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, the + operator is overloaded to concatenate Strings. The expression "Value of x is " + x is a String literal (the part inside the quotation marks) being concatenated with the value of x as a String. This does not change the value of x at all.

(Behind the scenes, the primitive int value is first converted to an Integer object, which is then converted to type String, which is then contatented to produce a new String. See JLS - 15.18.1 and

The line x = x + 1 is a simple assignment. The expression on the right side of the equals sign is evaluated, and the result is assigned to the variable on the left side. So if x is 1, then the right side evaluates to 1 + 1 = 2, and this result is assigned back to x.

Within a while loop, the test is performed only at the "top" (beginning) of the loop. If the condition is true, then the body of the loop executes. The condition might change to false while the body is executing, but the while loop won't "know" that until it tests again before the next iteration.
[ May 03, 2007: Message edited by: marc weber ]
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!