
-----------------------------------
Cervantes
Sat Feb 14, 2004 11:21 am

[Tutorial] LOOPs and FOR loops
-----------------------------------
LOOPs and FOR loops


LOOPs

Loops are a very basic and necessary part of Turing, as well as any programming language.  To be able to use loops, first we must understand the syntax.

loop - insert that into your code for where you want the loop to begin
end loop - insert that into your code where you want the loop to end.

Simple enough?  I thought so :).  Now that we know the basic syntax we can understand what a loop does.
A loop simply executes the code between the "loop" and the "end loop" forever.  
Question A.)  Try making your own loop that outputs "Turing rocks!" over and over.
(answers are found at the bottom of the tutorial)

So what's the point of making something repeat over and over forever?  Well, it doesn't have to go on forever.
A loop simply executes the code between the "loop" and the "end loop" forever.  
I'm sorry I kinda lied :?
the loop will only exit if there is an exit statement inside it.

EXIT statements
SYNTAX:
exit
difficult huh?  well, what's it do already?!  Agreed, what does it do?  By looking at the syntax of the word you can assume that it exits something.  You'd be right if you assumed that!  But what does it exit?  What have we been talking about all this time?  That's right!! It exits a loop!  Of course, there is little point to simply putting "exit" in a loop if it has no parameters.  In other words, 'exit' is usually accompanied by the command "when"

exit when number = 1

that code will, if placed inside a loop, exit the loop when the variable called number is equal to 1.  Where does it go when it exits the loop?  It goes 1 line after wherever the closest "end loop" is.  
For this next problem you're going to need a counter.  A counter is a variable that you make that increases by a set incremiment every time something happens.
Question B.) Write a program that counts to 10. (No, that doesn't involve 10 put statements)

So we now know a good deal about the structure of loops.  
"But Cervantes!" you ask, "when would we ever use a loop?!"
I hear you!  Loops are used in pretty much every program.  Think of a game, for example.  You want to run the game until someone dies.  That would involve a loop!  Very few programs don't use loops... or for statements.


FOR loops
a for loop is very similar to a loop.  The difference is that a for loop exits itself after a set number of repititions.  You can, however, exit a for statement prematurely using the same "exit" line as in loops.

SYNTAX:  the syntax of a for statement is more complicated then that of a loop.

for counter : 1 .. 10
that is the code that will begin your for loop.  However, many things can be changed about that.  the "counter" can be changed to whatever you want.  So can the two numbers.  

end for
simple enough ending, yes?
"What does this all mean?  What's that colon and 2 periods for?  I don't understand?!"
Allow me to explain:  The above for loop, when put together, will look like this

for counter : 1 .. 10

end for

This for loop will execute the code between the "for counter : 1 .. 10" and the "end for" 10 times.  Unfortunately, there is no code there :|.  It will also keep track of howmany times it has gone repeated, using the variable "counter".  You don't have to declare "counter" with a previous "var counter : blah blah blah" line.  
Question C.) Write a program using a for loop that will count from 1 to 15.
NOTE: you don't have to have a counter like you had in question B.).  the for loop does the counting for you :D

There are some other funky things that you can do with the for loops.  You can make them count backwards, or by a different incriment.  to do this:
COUNT BACKWARDS
for decreasing counter : 10 .. 1

end for

COUNT by a different INCRIMENT
for counter : 1 .. 11 by 2

end for

neat huh?  You can even combine these two!
for decreasing counter : 11 .. 1 by 2

end for

"When would you ever use a for loop?!"
For loops are often found inside a loop.  For loops work very well with arrays, but we don't want to go there just yet.  In due time :wink:
Now for some problems:
Question D.) Write a program that will count backwards from 64 to 1 by 3

Question E.) Write a simple game where you move a circle around the screen.

"Cervantes!! are you nuts?!" you scream.
Not really.  Now that you have an understanding of loops and for loops there's only one thing holding you back from making that game!  (check out the answer below)




ANSWERS

A.)
loop
    put "Turing Rocks!"
end loop


B.)
var counter : int
counter := 0
loop
    counter := counter + 1
    put counter
    exit when counter = 10
end loop




C.)
for counter : 1 .. 15
    put counter
end for





D.)
for decreasing counter : 64 .. 1 by 3
    put counter
end for



E.)  The only thing holding you back from making this game is 'Input.KeyDown" .  its not hard to learn.  My advice: don't try to understand why the variable for the keys is "array chars of boolean".  Simply use it.  You'll understand soon enough :)

http://www.compsci.ca/v2/viewtopic.php?t=114
          ^^^ = tony's super duper Input.KeyDown tutorial!

http://www.compsci.ca/v2/viewtopic.php?p=1586
          ^^^ = tony's super duper Loop tricks tutorial (faking goto line)

If you don't understand loops, for loops, or Input.KeyDown, PM me and I'll try to help you.

-----------------------------------
Tony
Sat Feb 14, 2004 2:23 pm


-----------------------------------
A very nice tutorial :D And it's awesome how you got questions/answers to solidify understanding and links to other related tutorials :D +100Bits

-----------------------------------
AsianSensation
Sat Feb 14, 2004 4:46 pm


-----------------------------------
good for newbies, and for people with questions that requires simple manipulation of loops and for loops, here, have some bits, extra bits for questions and answers

+50 bits

-----------------------------------
Cervantes
Sat Feb 14, 2004 7:32 pm


-----------------------------------
wow thanks guys :)
When I was writing it I thought I'd try to make it as much like you're in a compsci class as possible, so that means writing your own programs :)  Compsci really is problem solving, showing new programmers the answer only helps with the syntax and ideas not the problem solving ability.

-----------------------------------
Maverick
Sat Feb 14, 2004 10:34 pm


-----------------------------------
GJ, i like the formaqt you put it in. +BITS

-----------------------------------
TheLostSoul
Mon Mar 15, 2004 11:47 pm


-----------------------------------
1 Question, How Do I Put A Loop Inside Of A Loop...?

Like Put A House With A Door Opening, With A Sign On The House Flashing At The Same Time? I'm Pretty New At This, Thx Guys.

-----------------------------------
Thuged_Out_G
Tue Mar 16, 2004 2:50 am


-----------------------------------
well, in order to do two tasks at once, you use a loop and a process, and then fork the process...something like this


process openDoor
code to open and close the door
end openDoor

loop
fork openDoor
code for the flashing sign
end loop


-----------------------------------
Cervantes
Tue Mar 16, 2004 8:12 am


-----------------------------------
To answer the original question of how to put a loop inside a loop: As nike would say, just do it!


loop
    anycody
    loop
       anycode
    end loop
    anycode
end loop


If at all possible you should avoid using processes.  Generally, the only thing that proceses should be used for is playing music.  
In this case, we don't need to use processes.  We don't even need two loops.

All we need to do is draw the door at different angles inside the loop, as well as flashing the sign.  It won't ACTUALLY be at the same time, (the door will open a small amount and then the sign will flash once, then the door will open another small amount .... etc.) but the whole animation would be going at once.  


loop
  open door a small amount
  flash sign
end loop


To open the door, I would have a counter to keep track of how far it is already opened.  That would look like


loop
   counter := counter + 1
   open door a small amount past its previous amount.  We know what its previous amount was from the counter value.
   flash sign
end loop


I hope that answers your question.

-----------------------------------
TheLostSoul
Tue Mar 16, 2004 2:15 pm


-----------------------------------
Thx A Lot Man...You Guys Here Are Great And Very Helpful. (And Me Is Slow And Just Starting, So I Might Have A Few More Questions Down The Road).

Like Right Now :P

I Was Just Wondering If There Is Any Way To Get My Programs On A Site, So That You Can Watch It Play On The Site...Or If I Can Only Put The "Code" On The Site And Must Use Turing To View It?

Thx A Lot.

-----------------------------------
Cervantes
Tue Mar 16, 2004 5:47 pm


-----------------------------------
This is getting off topic so in future post those kind of questions in General Discussion.  

I'll answer it here this once though :wink:
You can get your programs on a site easily enough.  Just get a free site at a variety of plaes including geocities and tripod (uk).  
Here are the links:
Better yet, check the 

As for posting the program, I don't think you can post the program run window on your site (you can do it easily with Java, but alas, we're not using Java :| ) but you can post your program as an .exe.  All you have to do is compile the program and then upload it to your site.  To compile a program, go to the Run Menu at the top of the Turing window and click on Generate Stand-Alone Program.  When you do this, people won't need Turing to run the program.

Hope that answers your question.  Remeber to try to post in the right section next time.  :wink:

-----------------------------------
TheLostSoul
Tue Mar 16, 2004 7:13 pm


-----------------------------------
Yea, Sorry Bout That Wasn't Quite Thinking Was Just Rushing To Get This Long Thing Done :P

Btw Everyone..I Will Gladly Post A Link To My Program When I Upload It So You Can See How I Put These Skills I've Read About To Use. 

Thx For All The Help And Ideas As Well, And I Will Try To Remember Where I Am Posting Non-Topic Posts Next Time..Heh

-----------------------------------
nmr123321
Wed Oct 05, 2005 3:51 pm


-----------------------------------
you were a better help than the turing textbook. I could have given you bits but i have only 2. Thanks  for the tutorial

-----------------------------------
theguru
Sat Oct 08, 2005 5:05 pm


-----------------------------------
This is getting off topic so in future post those kind of questions in General Discussion.  

I'll answer it here this once though :wink:
You can get your programs on a site easily enough.  Just get a free site at a variety of plaes including geocities and tripod (uk).  
Here are the links:
Better yet, check the 

As for posting the program, I don't think you can post the program run window on your site (you can do it easily with Java, but alas, we're not using Java :| ) but you can post your program as an .exe.  All you have to do is compile the program and then upload it to your site.  To compile a program, go to the Run Menu at the top of the Turing window and click on Generate Stand-Alone Program.  When you do this, people won't need Turing to run the program.

Hope that answers your question.  Remeber to try to post in the right section next time.  :wink:

i don't think a lot of hosts will host .exe files because they think that it'll spread viruses and such so it'll probably be hard to find a host that can support your programs.

-----------------------------------
[Gandalf]
Sat Oct 08, 2005 5:21 pm


-----------------------------------
so in future post those kind of questions in General Discussion.
Then just zip your file.  It'll allow you to host more anyway.  Actually, quite a few hosts allow .exe's as long as they are yours.

-----------------------------------
Tony
Sat Oct 08, 2005 5:27 pm


-----------------------------------
i don't think a lot of hosts will host .exe files because they think that it'll spread viruses and such so it'll probably be hard to find a host that can support your programs.
Most hosts are *nix based, so .exe's wont execute. A bigger problem would be uploading .php files that execute server-side, but oh-wait! everybody allows those :lol:

-----------------------------------
evildaddy911
Mon Oct 17, 2011 3:03 pm

Re: [Tutorial] LOOPs and FOR loops
-----------------------------------
is there any other way to make
 
for i:1..infinity
end for


other than

var count:int
count:=0
loop
count:=count+1
%code%
end loop


-----------------------------------
Aange10
Mon Oct 17, 2011 4:53 pm

RE:[Tutorial] LOOPs and FOR loops
-----------------------------------
Well, I'm pretty sure infinity, even for a dynamic array is impossible (hence the maxint command). Though I could be wrong (floating points or some other mumbo jumbo I don't know a lot about.)


But to answer your question, yes. They are called flexible arrays. For an explanation, please read [url=http://compsci.ca/v3/viewtopic.php?t=14333] Arrays. Note that it has three different tutorials in the one page. (Arrays, Multi-Dementional arrays, and flexible arrays.) The posts are just separated.


Also, please check the time stamps on these topics. The last post was in 2005. Resurrecting a 6 year old topic is generally looked down upon.

-----------------------------------
Tony
Mon Oct 17, 2011 4:58 pm

Re: [Tutorial] LOOPs and FOR loops
-----------------------------------
is there any other way to make
 
for i:1..infinity
end for


Sure. How about
[code]
for j:2..infinity
   i := j - 1
   ...
end
[/code]
For some value of "infinity". What are you _trying_ to do?

@Aange10 -- not sure where arrays came from.

-----------------------------------
Aange10
Mon Oct 17, 2011 5:01 pm

Re: [Tutorial] LOOPs and FOR loops
-----------------------------------

@Aange10 -- not sure where arrays came from.

Ahh, oops. Sorry, I though he said array, not for  8-)

-----------------------------------
evildaddy911
Tue Oct 18, 2011 2:47 pm

Re: [Tutorial] LOOPs and FOR loops
-----------------------------------
im trying to put a counter on my game so that every 10 seconds or so you can do a special attack, but i think ill stick to the 
var counter:int
counter:=0
loop
counter:=counter+1

if counter>=1000 then

end if
end loop


-----------------------------------
Aange10
Tue Oct 18, 2011 4:40 pm

RE:[Tutorial] LOOPs and FOR loops
-----------------------------------
That can be done. If you need help please post in the help section.

-----------------------------------
Zren
Tue Oct 18, 2011 4:47 pm

RE:[Tutorial] LOOPs and FOR loops
-----------------------------------
Don't forget to reset counter after a special attack. Otherwise it's just a wait period from the start of the game.

Also, I suggest dealing in Time, not a frame counter.

The specific term for what you're wanting is called a cooldown.


% Time between attacks. In milliseconds.
var cooldown := 10 * 1000

% Time last attack occured
% Initialized as a negative so we can attack right at the start.
var lastAttack := -cooldown

loop
    var now := Time.Elapsed
    if now - lastAttack >= cooldown then % Check cooldown
        put "Hadoken"
        lastAttack := now
    end if
end loop


If your framerate is fixed, then it should be fine to substitute a frame count instead.
