Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop

 
jake kodak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jake kodak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "while" loop is not a "for" loop.
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A while loop takes a single boolean expression.
 
jake kodak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    for (initialization; condition; update) statement;
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 59
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 12203
    35
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic