aspose file tools*
The moose likes Beginning Java and the fly likes while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "while loop" Watch "while loop" New topic
Author

while loop

jake kodak
Greenhorn

Joined: Sep 26, 2005
Posts: 6
int up = 1;
while (int up = 1, dn = 99; up <= dn; up++, dn--) {
System.out.println("up = " + up + ", dn = " + dn);
up++
}
System.out.println("Processing complete");
}
}

I can't figure this while loop out.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by jake kodak:

I can't figure this while loop out.


There is nothing to figure out... this code shouldn't compile.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
jake kodak
Greenhorn

Joined: Sep 26, 2005
Posts: 6
public class Lab11A {
public static void main(String[] args) {
int up = 1;
while (int up = 1, dn = 99; up <= dn; up++, dn--) {
System.out.println("up = " + up + ", dn = " + dn);
up++
}
System.out.println("Processing complete");
}
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

A "while" loop is not a "for" loop.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

A while loop takes a single boolean expression.
jake kodak
Greenhorn

Joined: Sep 26, 2005
Posts: 6
I know I have to change it from a for loop to a while loop and I have tried a lot of ways this was one attemp.
Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
I guess it looks more of a for loop rather than a while loop.While loop requires its condition to evaluate to a boolean and since in this case it doesnt,compiler will throw error.
jake kodak
Greenhorn

Joined: Sep 26, 2005
Posts: 6
public class Lab11A {
public static void main(String[] args) {
int up = 1;
while (up = 1) {
int dn = 99;
while (dn = 99) {
if (up <= dn; up++, dn--) {
System.out.println("up = " + up + ", dn = " + dn);
up++
}
System.out.println("Processing complete");
}
}
does this look better
[ September 26, 2005: Message edited by: jake kodak ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by jake kodak:
...does this look better

Uh, no. It's apparent that you're taking shots in the dark.

There are basically 3 parts to your "for" loop:
  • int up = 1, dn = 99
  • up <= dn
  • up++, dn--

  • But a "while" loop only has 1 of these. So you need to build the other 2 into (or around) the while loop so that it works the same way.

    Can you explain how a "for" loop works? In particular, when do each of the 3 parts execute?
    [ September 26, 2005: Message edited by: marc weber ]
    jake kodak
    Greenhorn

    Joined: Sep 26, 2005
    Posts: 6
    for (initialization; condition; update) statement;
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by jake kodak:
    for (initialization; condition; update) statement;

    Okay... But when exactly do each of these execute? Try talking your way through a few iterations, step by step. If you can do this, you'll see how your while loop needs to be structured.
    jake kodak
    Greenhorn

    Joined: Sep 26, 2005
    Posts: 6
    public class Lab11A {
    public static void main(String[] args) {
    int up = 1;
    while (int up = 1, dn = 99, up <= dn up++, dn--) {
    System.out.println("up = " + up + ", dn = " + dn);
    up++
    }
    System.out.println("Processing complete");
    }
    }
    how is this
    Kenneth Albertson
    Ranch Hand

    Joined: Sep 18, 2005
    Posts: 59
    Originally posted by jake kodak:
    how is this


    What are you asking?

    Does it compile? Why don't you find out!

    Has it got any chance of achieving what you are trying to do? You haven't said what you are trying to do! If you are trying to write a program, you should start by stating what the program is meant to do. Then someone can help you work towards that goal.

    But I doubt that is the case. This looks like the sort of mental puzzle that instructors set to teach students to read and understand code. The point of such an exercise is to predict the outcome from reading the code, before running it to confirm your answer. But the instructions seem to have become garbed.

    My guess is that the problem you have been set is to work out what your code would print out, if it ran. It has two variables, which it initializes and then repeatedly increments and decrements as indicated until they cross over. Look at the logic and work out what the output should be. If you need to, manually execute each line of the loop over and over, recording the value of each variable at every line, until the loop exits. If you can't do it in your head, write it down.

    Once you think that you know the answer (assuming that I have correctly guessed the problem that you have been set), you should take the code you originally posted, and:
  • Delete the unnecessary first declaration of up;
  • Change the while loop to the for loop that it is obviously meant to be;
  • Add the required semi-colon after the second up++

  • Your program should then compile. Run it and see if the output is what you expect.

    I hope this helps. If my guess about your problem is not correct, perhaps you could respond with the correct details.
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11411
        
      16

    Jake,

    Your questions are very vague... "how is this?" doesn't give anyone much information on what kind of help you need.

    You state "i know i need to change it from a for loop to a while loop". does this mean you HAVE a working for loop? Is this an exercise where you have a hunk of working code, and the sole purpose is to re-write it?

    If that is the case, the for starters, post the working for loop. state the exact text of the exercise, and then show what you've tried.

    Now, assuming my assumptions are correct about the problem, it appears you don't quite understand a while-loop. a while loop looks like this:

    while (<boolean expression)
    {
    //code goes here
    }

    the ONLY thing that can go in the parens is a boolean expression. you CAN'T do things like "int 1 = 1" inside those parens.

    marc gave you a good idea. explain - and i mean actually write down - exactly when which part of the for loop executes. go through it a couple of times...

    first THIS happens,
    then we test THAT
    if the test passes, we do this
    then we do this...

    Write that down, and post it here. once we're sure you got that part down, we'll help you change it around to write it as a while loop... But you have to understand the code you HAVE before you can re-write it.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: while loop