Computer Science Canada

how do i get 2 or more for loops to work at the same time

Author:  coltss [ Fri May 15, 2009 1:43 pm ]
Post subject:  how do i get 2 or more for loops to work at the same time

What is it you are trying to achieve?
<to get 2 for loops to work at the same time. i am trying t get 2 boxes moving in different directions and different co -ordinates>


What is the problem you are having?
i want them to work in sequence


Describe what you have tried to solve this problem
I tried putting the loops together but that does not work since the distance they travel is different.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)


Turing:


for decreasing x : maxx  .. maxx - maxx
drawfillbox (x, maxy div 2-60, x + 40, maxy div 2 - 80, green)
delay(5)

end for

for x : maxx - maxx .. maxx div 2
drawfillbox ( x, maxy div 2-120, x + 40,maxy div 2 - 140, blue)
delay (5)


end for



Please specify what version of Turing you are using
4.1

Author:  BigBear [ Fri May 15, 2009 1:56 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

Turing:
for h : 1 .. 10
    for i : 1 .. 3
        put i
    end for
end for


Think about the input of this before you run it

Author:  coltss [ Fri May 15, 2009 2:09 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

input? well the out put will be 1 2 3 - 10 times

Author:  Kharybdis [ Fri May 15, 2009 2:14 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

You would require a regular Loop command with the value increasing up to a certain increment in your code.

You can also use Nested For Loops.

And the output for bigbear's code would be 123 repeated 10 times...

Author:  coltss [ Fri May 15, 2009 2:19 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

mind giving me an example?

Author:  BigBear [ Fri May 15, 2009 2:56 pm ]
Post subject:  Re: RE:how do i get 2 or more for loops to work at the same time

coltss @ Fri May 15, 2009 2:09 pm wrote:
input? well the out put will be 1 2 3 - 10 times


Right it repeats the for loop so it starts and enters the first for loop 1st time

then it enter the second loop and repeats that for 3 times and repeats it 9 more times

Author:  coltss [ Fri May 15, 2009 3:00 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

^^ i get the idea of what its doing but i cant get these lines to move

Author:  BigBear [ Fri May 15, 2009 3:01 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

It will done then the other unless you use a process which is bad news bears
Turing:

var columns, rows : int
put "Enter number of rows"
get rows
put "Enter number of columns"
get columns
for i : 1 .. rows
    for j : 1 .. columns
        put "*" ..
    end for
    put ""
end for

Author:  BigBear [ Fri May 15, 2009 3:04 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

What is the difference between
for i : 1.. 10
end for

and

loop
count+=1
exit when count = 10
end loop

Author:  coltss [ Fri May 15, 2009 3:16 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

nothing they will both go through that loop 10 times

Author:  tjmoore1993 [ Fri May 15, 2009 3:19 pm ]
Post subject:  Re: RE:how do i get 2 or more for loops to work at the same time

BigBear @ Fri May 15, 2009 3:04 pm wrote:
What is the difference between
for i : 1.. 10
end for

and

loop
count+=1
exit when count = 10
end loop


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The for statement speaks for itself. You are setting a counter on a single line for example

for x : 1 .. 3

x is the counter in this case and is predefined so you do not need to declare as for a loop you need to declare your counter
which can get pretty boring because you don't want to have like several counters through out the program.

The difference is efficiency and the time it takes to load. The For statement has an upper advantage and it performs better. Although loops can be good
at anytime Smile

Author:  BigBear [ Fri May 15, 2009 3:19 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

So you could just have a loop that changes the values of two different objects.

with += and -= or have the position of one be the variable time -1 to be

try this and post your new code

Author:  coltss [ Fri May 15, 2009 3:28 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

i tried didnt work ugh i 'v got to run to work i'll ponder on it more when i get back. i feell stupid lol

Author:  Kharybdis [ Fri May 15, 2009 7:16 pm ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

I would also suggest that you look at the graphics tutorial in the Turing Help Section, it gives a few interesting things...

Author:  Dusk Eagle [ Fri May 15, 2009 11:34 pm ]
Post subject:  Re: how do i get 2 or more for loops to work at the same time

First of all thank you for using the correct format to post your question.

Now, I don't know why your for loops include a "maxx - maxx". This is always the same as zero, so changing it to zero would make your code clearer. Now, for our main problem, I'm going to use a for loop from 0 to maxx, but you use whatever you want. First, you take both your lines of code and stick them in the same for loop. Then, notice that when you say x, what you are really saying is 0 + x. Therefore, to make a line come from the other direction, we simply call the opposite: maxx - x. To make one line go half as fast as the other, we can simply use a div 2 statement. I made a couple of other modifications to your code, which make it work well. Since this is only one small snippet of code, I don't feel it's cheating for me to give you the code here, especially if you study it to learn for future use. Don't just use my code - understand it and tinker with it. So here it is:
Turing:
for x : 0 .. maxx
    drawline (x, maxy div 2 - 60, x, maxy div 2 - 80, green)
    drawline (maxx - x div 2, maxy div 2 - 120, maxx - x div 2 , maxy div 2 - 140, blue)
    delay (5)
end for


Edit: Notice how I used drawline instead of drawbox? Unless there's some performance change I'm unaware of, that shouldn't make much of a difference. I did it to show that there are multiple ways of approaching this problem.

Author:  coltss [ Sat May 16, 2009 9:29 am ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

thx guys i got it to work last night so now how would i make something go along the x axis and the y axis at the same time?

Author:  BigBear [ Sat May 16, 2009 11:31 am ]
Post subject:  RE:how do i get 2 or more for loops to work at the same time

just draw your object

lets say drawfillbox (10, 10, 20, 20, 5)

then have a loop controlled variable so either a for loop or a counted loop with the value always changing then

add the value to the x coordinate on one object and add the same variable to the y coordinate on the other

Author:  Dusk Eagle [ Sat May 16, 2009 3:19 pm ]
Post subject:  Re: how do i get 2 or more for loops to work at the same time

Just to clarify what BigBear said, you wouldn't actually use numbers like "10" and "20", but you would use variables in those locations that you change the value of each time through the loop.


: