# Not sure whether I'm using a loop correctly and whether what I'm doing is possible

• November 9th, 2010, 11:33 AM
Not sure whether I'm using a loop correctly and whether what I'm doing is possible
Hi,

I've currently written a program that reads in two values from the keyboard and stores them. The next part of my program is where its confusing me.

I'm trying to repeatedly ask a further two more questions which then does some simple calculations with the data that is read in from the keyboard until a value of zero is met which kills the loop. Now I understand the concept behind what I'm trying to do requires a loop which I'm assuming would be a Do While loop.

Its when I try to print every calculation, I want to also print a ratio between the previous and the present values except the first iteration which I've really know clue about doing.(I know how to work out the ratio)

But I dont know how I would print the ratio missing out the first run? Because in a loop a message prints constantly regardless, so is there a way I can get a message from out side the loop or something?
• November 9th, 2010, 11:51 AM
javapenguin
Re: Not sure whether I'm using a loop correctly and whether what I'm doing is possibl
Quote:

Hi,

I've currently written a program that reads in two values from the keyboard and stores them. The next part of my program is where its confusing me.

I'm trying to repeatedly ask a further two more questions which then does some simple calculations with the data that is read in from the keyboard until a value of zero is met which kills the loop. Now I understand the concept behind what I'm trying to do requires a loop which I'm assuming would be a Do While loop.

Its when I try to print every calculation, I want to also print a ratio between the previous and the present values except the first iteration which I've really know clue about doing.(I know how to work out the ratio)

But I dont know how I would print the ratio missing out the first run? Because in a loop a message prints constantly regardless, so is there a way I can get a message from out side the loop or something?

It would be nice if you could show your code.

How to repeatedly ask for a value until a value is reached is

static Scanner console = new Scanner(System.in);
int x = 0;

while (x !=0)
{
int x = console.nextInt();

// do calculations.
}

if you have the calculated result that's supposed to be 0, have

while (calculationResultVariable != 0)
• November 9th, 2010, 12:13 PM
javapenguin
Re: Not sure whether I'm using a loop correctly and whether what I'm doing is possibl
A do...while loop did come to my mind even before I noticed that part in your post. However, that may not be the best format now that I think of it.

A do....while merely executes a condition at least once, even if the while value is going to return false or whatever from the first iteration.

The reason you're skipping the first iteration is because there isn't, at least I don't think so, I don't have your code, a previous value.

You'll either get 0 or an Arithmetic Exception
• November 9th, 2010, 12:19 PM