# Thread: Counting with loops up, down, up, ... ets

1. ## Counting with loops up, down, up, ... ets

I am trying to find out how I can count with a loop in a range from lowest to highest then highest to lowest...maybe a loop is not the best thing to use?

something like

1 2 3 4 5

then 5 4 3 2 1

then 1 2 3 4 5 again and this keep repeating until i choose to stop it..which i know how to do

any help would be appreciated!

I would like for the number to be stored in a single variable.

2. A straightforward way is to use two inner loops nested in an outer loop. It is possible to use a single inner loop if you use a formula, or handle two cases.

3. would u mind giving me an example with the 1-5 then back again?

4. What have you tried?

5. well I can give u the exact code I am trying to use this for but its not as simple as counting from 1-5 then from 5-1 again....this is just the simplest concept of what I must accomplish. I am drawing a square with 4 lines from 1 side of a drawing area to another, once the edge touches the side I want it to go back. In essence this is accomplished the same way I would do from 1-5 and 5-1 but in the while loops i must have arguments ofcourse

Code:
```			//Asteroid 1 Redrawing
while(a1_line1x2<=490){
SetDrawColor(win, 1.0, 1.0, 1.0);
Line(win, a1_line1x1, a1_line1y1, a1_line1x2, a1_line1y2);
Line(win, a1_line2x1, a1_line2y1, a1_line2x2, a1_line2y2);
Line(win, a1_line3x1, a1_line3y1, a1_line3x2, a1_line3y2);
Line(win, a1_line4x1, a1_line4y1, a1_line4x2, a1_line4y2);

a1_line1x1+=update2;
a1_line1x2+=update2;

a1_line2x1+=update2;
a1_line2x2+=update2;

a1_line3x1+=update2;
a1_line3x2+=update2;

a1_line4x1+=update2;
a1_line4x2+=update2;

SetDrawColor(win, 0.0, 0.0, 0.0);
Line(win, a1_line1x1, a1_line1y1, a1_line1x2, a1_line1y2);
Line(win, a1_line2x1, a1_line2y1, a1_line2x2, a1_line2y2);
Line(win, a1_line3x1, a1_line3y1, a1_line3x2, a1_line3y2);
Line(win, a1_line4x1, a1_line4y1, a1_line4x2, a1_line4y2);

}

while(a1_line3x2>=10) {
SetDrawColor(win, 1.0, 1.0, 1.0);
Line(win, a1_line1x1, a1_line1y1, a1_line1x2, a1_line1y2);
Line(win, a1_line2x1, a1_line2y1, a1_line2x2, a1_line2y2);
Line(win, a1_line3x1, a1_line3y1, a1_line3x2, a1_line3y2);
Line(win, a1_line4x1, a1_line4y1, a1_line4x2, a1_line4y2);

a1_line1x1-=update2;
a1_line1x2-=update2;

a1_line2x1-=update2;
a1_line2x2-=update2;

a1_line3x1-=update2;
a1_line3x2-=update2;

a1_line4x1-=update2;
a1_line4x2-=update2;

SetDrawColor(win, 0.0, 0.0, 0.0);
Line(win, a1_line1x1, a1_line1y1, a1_line1x2, a1_line1y2);
Line(win, a1_line2x1, a1_line2y1, a1_line2x2, a1_line2y2);
Line(win, a1_line3x1, a1_line3y1, a1_line3x2, a1_line3y2);
Line(win, a1_line4x1, a1_line4y1, a1_line4x2, a1_line4y2); }```
this is my code for one of my boxes, right now it keeps jumping back and forth with "while" loops but if i put an if there the box will go to the edge...redraw itself backwards once ... then redrawn itself forwards. the whole box redrawing is done in a body of a bigger event loop.

6. imagine you have two functions:
Code:
```int count_up(int from, int distance); //returns the ending position
int count_down(int from, int distance); //returns the ending position```
call these in a for loop (one after the other) and run them as many times as you want.
inside these functions, you can do whatever you want as long as its done counting when it returns.

you could even combine the two functions into one and pass a direction. you will need to change the direction appropriately though. Right now do you want it to change directions when it hits a boundary? When it hits a boundary, you can return the direction and pass it back into the function when it goes through the loop again.