# Thread: How to get a for loop to stop with break/return

1. Member
Join Date
Jul 2013
Location
Texas
Posts
13
Rep Power
0

## How to get a for loop to stop with break/return

Hi! I had such success last time that instead of beating my head against my keyboard and yelling profanity at my chinchillas, I decided to see if anyone can guide me through this.

Ok. The project is: Based on an initial (x, y) position and initial velocity (vx, vy) of an object given by the user, calculate and draw the position of a falling object before it hits the ground.

So far I have everything working according to the rubric except my ball keeps being drawn below my ground. I've tried numerous things to end the for loop but am not having any luck. Here is what I have:

Java Code:
```public static void drawBall( int x, int y, int vx, int vy, Graphics g, Color c )
{
// time step calc
for( double i = 0; i < 100 ; i = i + .3)
{

// next X = Initial Position X + Initial Velocity X * Time Step
// next Y = Initial Position Y + Initial Velocity Y * Time Step + .5 * 9.8 * Time Step * Time Step

// calc x and y

double nextX = x + vx * i;
double nextY = y + vy * i + .5 * 9.8 * i * i;

//draw ball
g.setColor( c );
g.fillOval( (int)nextX, (int)nextY, 10, 10 );

//stops ball from going below horizon
double ballStop = ((double)HORIZON - 20.0);

if ( nextY > ballStop )
{
return;
}

}```

HORIZON is a class constant of 705, and is the beginning of my ground on the drawing panel. I've tried using the equation, having HORIZON instead of ballStop in the if statement, subtracting, adding, casting everything as an int...I'm at a loss. Any guidance would help. Thanks!!
Last edited by shaeway; 07-23-2013 at 07:05 PM.

2. ## Re: How to get a for loop to stop with break/return

Drawing Concentric Circles with For Loops Assignment

db

3. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: How to get a for loop to stop with break/return

Originally Posted by shaeway
Hi! I had such success last time that instead of beating my head against my keyboard and yelling profanity at my chinchillas, I decided to see if anyone can guide me through this.
I do believe programming is a threat to your personal health. Perhaps you should consider seeking counseling to help you with your rage problems.

4. ## Re: How to get a for loop to stop with break/return

kind regards,

Jos

5. Member
Join Date
Jul 2013
Location
Texas
Posts
13
Rep Power
0

## Re: How to get a for loop to stop with break/return

@ JosAH, the enchilada thing is actually quite common. Why I do not know, but you aren't the only one by any means. Thank you for having a sense of humor.

6. Member
Join Date
Jul 2013
Location
Texas
Posts
13
Rep Power
0

## Re: How to get a for loop to stop with break/return

Originally Posted by DarrylBurke
Drawing Concentric Circles with For Loops Assignment

db
Not quite sure where you get that idea, since I watched them like a hawk the entire time I was finishing my last project. I thought that this was similar to other forums and when a new issue arrived, and you couldn't find anything that really answered your question via searching, you simply asked your question and the members either gave advice or guided you to an already posted thread that you didn't find yourself. I was obviously mistaken.

7. ## Re: How to get a for loop to stop with break/return

You're drawing 'snapshots' of that ball w.r.t. a time 'i'. If nextY happens to be below ground level you still draw it ...

kind regards,

Jos

8. ## Re: How to get a for loop to stop with break/return

Originally Posted by shaeway
Not quite sure where you get that idea
If you had clicked the link I posted in this thread at #2 you would know exactly where I got that idea.

db

9. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,425
Rep Power
9

## Re: How to get a for loop to stop with break/return

Perhaps I can spell it out for you. Did you read the Forum guides and rules? Your code is still not posted within code tags.

Regards,
Jim

10. Member
Join Date
Jul 2013
Location
Texas
Posts
13
Rep Power
0

## Re: How to get a for loop to stop with break/return

Ah, Jim you are correct, I did forget that. My apologies. Thank you for pointing that out.

And JosAH, that is exactly what I was not thinking about. Testing AFTER the ball was drawn was allowing it to proceed past the horizon. I needed to test the value before I drew the ball so that it would stop at the appropriate time.

11. ## Re: How to get a for loop to stop with break/return

Originally Posted by shaeway
And JosAH, that is exactly what I was not thinking about. Testing AFTER the ball was drawn was allowing it to proceed past the horizon. I needed to test the value before I drew the ball so that it would stop at the appropriate time.
Don't stop drawing one step before because the ball would stop moving above the horizon.

kind regards,

Jos

12. Member
Join Date
Jul 2013
Location
Texas
Posts
13
Rep Power
0

## Re: How to get a for loop to stop with break/return

I ended up setting my ballStop as HORIZON - 10, and my test remaining the same. My thinking behind that is anything beyond that point would draw the ball below the horizon level, since my ball has a diameter of 10. However, you bring up a very valid point.

I'm honestly not sure if we needed it to touch the ground or not. There isn't anything in the rubric other than making sure the ball does not go past the horizon. I will double check though and run through the program several times, seeing what I come up with. I may need to add an extra drawing command that draws the last ball at the set y-coordinate with a new calculated x-coordinate.

13. Member
Join Date
Jul 2013
Location
Texas
Posts
13
Rep Power
0

## Re: How to get a for loop to stop with break/return

Java Code:
```// draws the ball in various positions
public static void drawBall( int x, int y, int vx, int vy, Graphics g, Color c )
{
// time step calc
for( double i = 0; i < 100 ; i = i + .3)
{

// next X = Initial Position X + Initial Velocity X * Time Step
// next Y = Initial Position Y + Initial Velocity Y * Time Step + .5 * 9.8 * Time Step * Time Step

// calc x and y

double nextX = x + vx * i;
double nextY = y + vy * i + .5 * 9.8 * i * i;

//stops ball from going below horizon
double ballStop = ((double)HORIZON - 10);

if ( nextY > ballStop )
{
g.setColor( c );
g.fillOval( (int)nextX, 695, 10, 10 );
return;
}

//draw ball
g.setColor( c );
g.fillOval( (int)nextX, (int)nextY, 10, 10 );

}```
This is what I came up with as my final and so far it's done what I needed it to do.

14. ## Re: How to get a for loop to stop with break/return

Note that the ball runs over a parabola shaped path; if your ball hits the horizon, the parabola hits the horizon and you should be able to find the x coordinate of that intersection point. Draw the ball at that location and you're in business. You only have to do the calculations if the ball would end up below the horizon (your original problem).

kind regards,

Jos

#### Posting Permissions

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