# Thread: Why doesn't my loop work?

1. Member
Join Date
May 2008
Posts
12
Rep Power
0

## Why doesn't my loop work?

I am creating a program where it simulates a ball with default direction and position. By when it strikes the corners N and M, it changes directions accordingly. The problem i am facing right now is that it only processes 1 step in my loop.

I am not sure why my loop does not work. Can anyone comment/advice what i have done wrong in my codings?
These are the codes below. Thanks!

/**
* Create a simulation that makes the ball move within the board size
* 1--Create the driver to test the retrace path method
*/
import java.util.*;

public class prac3 {

public static void main(String[] args) {
//variables
Scanner input = new Scanner(System.in);

int startX, startY, N, M;

System.out.println("Please enter board dimensions N M");
String inWidth = input.next();
String inHeight = input.next();
System.out.println("Please enter the starting position X Y");
String inX = input.next();
String inY = input.next();

startX = Integer.parseInt(inX);
startY = Integer.parseInt(inY);
N = Integer.parseInt(inWidth);
M = Integer.parseInt(inHeight);

updatePos(startX, startY, N, M);
}

public static int updatePos(int startX, int startY, int N, int M) {
int xOrg = startX;
int yOrg = startY;
int xCur = startX;
int yCur = startY;
// default direction -- NE
int xCurDir = 1;
int yCurDir = 1;
int step = 0;

// update position
do {
// move default direction -- NE
// move position x & y
xCur = xCur + xCurDir;
yCur = yCur + yCurDir;
step++;
System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);

// direction at (N-1,M-1)-- SW
if(xCur == (N-1)) {
if(yCur == (M-1)) {
xCurDir = -1;
yCurDir = -1;
}
// direction at (N-1,yCur)-- SE
else {
xCurDir = 1;
yCurDir = -1;
}
}
// direction at (0,0) -- NE
else if(xCur == 0) {
if(yCur == 0) {
xCur = 1;
yCur = 1;
}
// direction at(0,yCur) -- SE
else {
xCur = 1;
yCur = -1;
}
}
}
/** condition -- if xCur & yCur equals original x & y pos,
* & xCurDir & yCurDir equals original direction,
* stop loop
************************************************** *****/
while(((xCur != xOrg) && (yCur == yOrg)) && ((xCurDir == 1) && (yCurDir == 1)));
return step;
}
}

2. What's the purpose of having a semi-colon there?

What's the original direction's value?
Last edited by sukatoa; 05-26-2008 at 02:22 AM.

3. Member
Join Date
May 2008
Posts
12
Rep Power
0
I am sorry, but which semi-colon are your referring to? Anyways i made several changes by making the object bounce once it hits the side walls. What mistake did i make to cause an endless loop? I am pretty sure my conditions are fine, but i would like to check them.

These are my new codes:
/**
* Create a simulation that makes the ball move within the board size
* 1--Create the driver to test the retrace path method
*/
import java.util.*;

public class prac3 {

public static void main(String[] args) {
//variables
Scanner input = new Scanner(System.in);

int startX, startY, N, M;

System.out.println("Please enter board dimensions N M");
String inWidth = input.next();
String inHeight = input.next();
System.out.println("Please enter the starting position X Y");
String inX = input.next();
String inY = input.next();

startX = Integer.parseInt(inX);
startY = Integer.parseInt(inY);
N = Integer.parseInt(inWidth);
M = Integer.parseInt(inHeight);

updatePos(startX, startY, N, M);
}

public static int updatePos(int startX, int startY, int N, int M) {
// original x,y coordinates
int xOrg = startX;
int yOrg = startY;
// current x,y coordinates
int xCur = startX;
int yCur = startY;
// previous x,y coordinates
int xPrev = 0;
int yPrev = 0;
// default direction movement -- NE
int xCurDir = 1;
int yCurDir = 1;
int step = 0;

// update position
do {
// move default direction -- NE
// move default position x & y
xCur = xCur + xCurDir;
yCur = yCur + yCurDir;
step++;
System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
xPrev = xCur;
yPrev = yCur;

//conditions
// xCur == (N-1)
if(xCur == (N-1)) {
// corner pos @ (N-1,M-1) -- direction SW
if(yCur == (M-1)) {
xCurDir = -1;
yCurDir = -1;
}
// corner pos @ (N-1,0) -- direction NW
else if(yCur == 0) {
xCurDir = -1;
yCurDir = 1;
}
// side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur < yPrev)) -- SW
else if((xCur > xPrev)&&(yCur < yPrev)) {
xCurDir = -1;
yCurDir = -1;
}
// side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur > yPrev)) -- NW
else if((xCur > xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = 1;
}
}
// xCur == 0
else if(xCur == 0) {
// corner pos @ (0,0) -- direction NE
if(yCur == 0) {
xCur = 1;
yCur = 1;
}
// corner pos @ (0,M-1) -- direction SE
else if(yCur == (M-1)) {
xCurDir = 1;
yCurDir = -1;
}
// side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur > yPrev)) -- NE
else if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = 1;
yCurDir = 1;
}
// side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur <yPrev)) -- SE
else if((xCur < xPrev)&&(yCur < yPrev)) {
xCurDir = 1;
yCurDir = -1;
}
}
// yCur == 0
// side pos @ (xCur,0)&&((xCur < xPrev)&&(yCur>yPrev)) -- NW
else if(yCur == 0) {
if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = 1;
}
// side pos @ (xCur,0)&&((xCur > xPrev)&&(yCur < yPrev)) -- NE
else if((xCur > xPrev)&&(yCur < yPrev)) {
xCurDir = 1;
yCurDir = 1;
}
}
// yCur == (M-1)
else if(yCur == (M-1)) {
// side pos @ (xCur,M-1)&&((xCur < xPrev)&&(yCur > yPrev)) -- SW
if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = -1;
}
// side pos @ (xCur,M-1)&&((xCur > xPrev)&&(yCur >yPrev)) -- SE
else if((xCur > xPrev)&&(yCur >yPrev)) {
xCurDir = 1;
yCurDir = -1;
}
}

xCur = xCur + xCurDir;
yCur = yCur + yCurDir;
step++;
System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
xPrev = xCur;
yPrev = yCur;

}
/** condition -- if xCur & yCur equals original x & y pos,
* & xCurDir & yCurDir equals original direction,
* stop loop
************************************************** *****/
while((xCur < N && xCur >= 0) && (yCur < M && yCur >= 0) &&
((xCur == xOrg) || (yCur == yOrg)) || ((xCurDir == 1) || (yCurDir == 1)));
return step;
}
}

4. Please use code tags posting codes next time pal. :) In this it's not easy to go through the code.

5. Member
Join Date
May 2008
Posts
12
Rep Power
0
Sorry, am new to this. I will re-post it in quote tags as you suggested

Java Code:
```/**
*	Create a simulation that makes the ball move within the board size
*	1--Create the driver to test the retrace path method
*/
import java.util.*;

public class prac3 {

public static void main(String[] args) {
//variables
Scanner input = new Scanner(System.in);

int startX, startY, N, M;

System.out.println("Please enter board dimensions N M");
String inWidth  = input.next();
String inHeight = input.next();
System.out.println("Please enter the starting position X Y");
String inX = input.next();
String inY = input.next();

startX 	= Integer.parseInt(inX);
startY 	= Integer.parseInt(inY);
N	= Integer.parseInt(inWidth);
M 	= Integer.parseInt(inHeight);

updatePos(startX, startY, N, M);
}

public static int updatePos(int startX, int startY, int N, int M) {
//	original x,y coordinates
int xOrg = startX;
int yOrg = startY;
//	current x,y coordinates
int xCur = startX;
int yCur = startY;
//	previous x,y coordinates
int xPrev = 0;
int yPrev = 0;
//	default direction movement -- NE
int xCurDir = 1;
int yCurDir = 1;
int step = 0;

//	update position
do {
//	move default direction -- NE
//	move default position x & y
xCur = xCur + xCurDir;
yCur = yCur + yCurDir;
step++;
System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
xPrev = xCur;
yPrev = yCur;

//conditions
//	xCur == (N-1)
if(xCur == (N-1)) {
//	corner pos @ (N-1,M-1) -- direction SW
if(yCur == (M-1)) {
xCurDir = -1;
yCurDir = -1;
}
//	corner pos @ (N-1,0) -- direction NW
else if(yCur == 0) {
xCurDir = -1;
yCurDir = 1;
}
//	side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur < yPrev)) -- SW
else if((xCur > xPrev)&&(yCur < yPrev)) {
xCurDir = -1;
yCurDir = -1;
}
//	side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur > yPrev)) -- NW
else if((xCur > xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = 1;
}
}
//	xCur == 0
else if(xCur == 0) {
//	corner pos @ (0,0) -- direction NE
if(yCur == 0) {
xCur = 1;
yCur = 1;
}
//	corner pos @ (0,M-1) -- direction SE
else if(yCur == (M-1)) {
xCurDir = 1;
yCurDir = -1;
}
//	side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur > yPrev)) -- NE
else if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = 1;
yCurDir = 1;
}
//	side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur <yPrev)) -- SE
else if((xCur < xPrev)&&(yCur < yPrev)) {
xCurDir = 1;
yCurDir = -1;
}
}
//	yCur == 0
//	side pos @ (xCur,0)&&((xCur < xPrev)&&(yCur>yPrev)) -- NW
else if(yCur == 0) {
if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = 1;
}
//	side pos @ (xCur,0)&&((xCur > xPrev)&&(yCur < yPrev)) -- NE
else if((xCur > xPrev)&&(yCur < yPrev)) {
xCurDir = 1;
yCurDir = 1;
}
}
//	yCur == (M-1)
else if(yCur == (M-1)) {
//	side pos @ (xCur,M-1)&&((xCur < xPrev)&&(yCur > yPrev)) -- SW
if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = -1;
}
//	side pos @ (xCur,M-1)&&((xCur > xPrev)&&(yCur >yPrev)) -- SE
else if((xCur > xPrev)&&(yCur >yPrev)) {
xCurDir = 1;
yCurDir = -1;
}
}

xCur = xCur + xCurDir;
yCur = yCur + yCurDir;
step++;
System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
xPrev = xCur;
yPrev = yCur;

}
/**	condition -- if xCur & yCur equals original x & y pos,
*	& xCurDir & yCurDir equals original direction,
*	stop loop
*******************************************************/
while((xCur < N && xCur >= 0) && (yCur < M && yCur >= 0) &&
((xCur == xOrg) || (yCur == yOrg)) || ((xCurDir == 1) || (yCurDir == 1)));
return step;
}
}```

6. Thanks buddy, :)

You get the endless loop, that your while loop to fall into false. Better to check that condition. I'm not clear exactly what you want to do in that case.

7. Member
Join Date
May 2008
Posts
12
Rep Power
0
You get the endless loop, that your while loop to fall into false. Better to check that condition. I'm not clear exactly what you want to do in that case.
Thanks for the help:)
I want the loop to work in such that:
- the red text codes are conditions that only allow the program to process within the given values
- the blue text codes are conditions that only when the values are met, then the program exits the loop

Java Code:
```/**
*	Create a simulation that makes the ball move within the board size
*	1--Create the driver to test the retrace path method
*/
import java.util.*;

public class prac3 {

public static void main(String[] args) {
//variables
Scanner input = new Scanner(System.in);

int startX, startY, N, M;

System.out.println("Please enter board dimensions N M");
String inWidth  = input.next();
String inHeight = input.next();
System.out.println("Please enter the starting position X Y");
String inX = input.next();
String inY = input.next();

startX 	= Integer.parseInt(inX);
startY 	= Integer.parseInt(inY);
N	= Integer.parseInt(inWidth);
M 	= Integer.parseInt(inHeight);

updatePos(startX, startY, N, M);
}

public static int updatePos(int startX, int startY, int N, int M) {
//	original x,y coordinates
int xOrg = startX;
int yOrg = startY;
//	current x,y coordinates
int xCur = startX;
int yCur = startY;
//	previous x,y coordinates
int xPrev = 0;
int yPrev = 0;
//	default direction movement -- NE
int xCurDir = 1;
int yCurDir = 1;
int step = 0;

//	update position
do {
//	move default direction -- NE
//	move default position x & y
xCur = xCur + xCurDir;
yCur = yCur + yCurDir;
step++;
System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
xPrev = xCur;
yPrev = yCur;

//conditions
//	xCur == (N-1)
if(xCur == (N-1)) {
//	corner pos @ (N-1,M-1) -- direction SW
if(yCur == (M-1)) {
xCurDir = -1;
yCurDir = -1;
}
//	corner pos @ (N-1,0) -- direction NW
else if(yCur == 0) {
xCurDir = -1;
yCurDir = 1;
}
//	side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur < yPrev)) -- SW
else if((xCur > xPrev)&&(yCur < yPrev)) {
xCurDir = -1;
yCurDir = -1;
}
//	side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur > yPrev)) -- NW
else if((xCur > xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = 1;
}
}
//	xCur == 0
else if(xCur == 0) {
//	corner pos @ (0,0) -- direction NE
if(yCur == 0) {
xCur = 1;
yCur = 1;
}
//	corner pos @ (0,M-1) -- direction SE
else if(yCur == (M-1)) {
xCurDir = 1;
yCurDir = -1;
}
//	side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur > yPrev)) -- NE
else if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = 1;
yCurDir = 1;
}
//	side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur <yPrev)) -- SE
else if((xCur < xPrev)&&(yCur < yPrev)) {
xCurDir = 1;
yCurDir = -1;
}
}
//	yCur == 0
//	side pos @ (xCur,0)&&((xCur < xPrev)&&(yCur>yPrev)) -- NW
else if(yCur == 0) {
if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = 1;
}
//	side pos @ (xCur,0)&&((xCur > xPrev)&&(yCur < yPrev)) -- NE
else if((xCur > xPrev)&&(yCur < yPrev)) {
xCurDir = 1;
yCurDir = 1;
}
}
//	yCur == (M-1)
else if(yCur == (M-1)) {
//	side pos @ (xCur,M-1)&&((xCur < xPrev)&&(yCur > yPrev)) -- SW
if((xCur < xPrev)&&(yCur > yPrev)) {
xCurDir = -1;
yCurDir = -1;
}
//	side pos @ (xCur,M-1)&&((xCur > xPrev)&&(yCur >yPrev)) -- SE
else if((xCur > xPrev)&&(yCur >yPrev)) {
xCurDir = 1;
yCurDir = -1;
}
}

xCur = xCur + xCurDir;
yCur = yCur + yCurDir;
step++;
System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
xPrev = xCur;
yPrev = yCur;

}
/**	condition -- if xCur & yCur equals original x & y pos,
*	& xCurDir & yCurDir equals original direction,
*	stop loop
*******************************************************/
while([COLOR="Red"](xCur < N && xCur >= 0) || (yCur < M && yCur >= 0)[/COLOR] ||
([COLOR="Blue"](xCur == xOrg) || (yCur == yOrg)) || ((xCurDir == 1) || (yCurDir == 1))[/COLOR]);
return step;
}
}```

8. I am sorry, but which semi-colon are your referring to?
this,
Java Code:
`while(((xCur != xOrg) && (yCur == yOrg)) && ((xCurDir == 1) && (yCurDir == 1)));`

9. Member
Join Date
May 2008
Posts
12
Rep Power
0
Java Code:
`while(((xCur != xOrg) && (yCur == yOrg)) && ((xCurDir == 1) && (yCurDir == 1)));`
It is a do-while loop:-
If i took the ';' away, there would be a compilation error. I tried adding curly brackets. Not working too.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•