Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
[Tutorial] LOOPs and FOR loops
Author Message
Cervantes

Posted: Sat Feb 14, 2004 11:21 am   Post subject: [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.

 code: loop
- insert that into your code for where you want the loop to begin
 code: 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.
Cervantes wrote:
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:
 code: 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"

 code: 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.

 code: 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.

 code: 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
 code: 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

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
 code: for decreasing counter : 10 .. 1 end for

COUNT by a different INCRIMENT
 code: for counter : 1 .. 11 by 2 end for

neat huh? You can even combine these two!
 code: 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
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)

A.)
 code: loop     put "Turing Rocks!" end loop

B.)
 code: var counter : int counter := 0 loop     counter := counter + 1     put counter     exit when counter = 10 end loop

C.)
 code: for counter : 1 .. 15     put counter end for

D.)
 code: 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

Posted: Sat Feb 14, 2004 2:23 pm   Post subject: (No subject)

A very nice tutorial And it's awesome how you got questions/answers to solidify understanding and links to other related tutorials +100Bits
Tony's programming blog. DWITE - a programming contest.
AsianSensation

Posted: Sat Feb 14, 2004 4:46 pm   Post subject: (No subject)

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

Posted: Sat Feb 14, 2004 7:32 pm   Post subject: (No subject)

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

Posted: Sat Feb 14, 2004 10:34 pm   Post subject: (No subject)

GJ, i like the formaqt you put it in. +BITS
TheLostSoul

Posted: Mon Mar 15, 2004 11:47 pm   Post subject: (No subject)

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

Posted: Tue Mar 16, 2004 2:50 am   Post subject: (No subject)

well, in order to do two tasks at once, you use a loop and a process, and then fork the process...something like this

 code: process openDoor code to open and close the door end openDoor loop fork openDoor code for the flashing sign end loop
Cervantes

Posted: Tue Mar 16, 2004 8:12 am   Post subject: (No subject)

To answer the original question of how to put a loop inside a loop: As nike would say, just do it!

 code: 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.

 code: 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

 code: 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

TheLostSoul

Posted: Tue Mar 16, 2004 2:15 pm   Post subject: (No subject)

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

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

Posted: Tue Mar 16, 2004 5:47 pm   Post subject: (No subject)

This is getting off topic so in future post those kind of questions in General Discussion.

I'll answer it here this once though
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).
Geocities
Tripod (UK)
EDIT: Better yet, check the good free hosting thread in the Off Topic forum.

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.
TheLostSoul

Posted: Tue Mar 16, 2004 7:13 pm   Post subject: (No subject)

Yea, Sorry Bout That Wasn't Quite Thinking Was Just Rushing To Get This Long Thing Done

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

Posted: Wed Oct 05, 2005 3:51 pm   Post subject: (No subject)

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

Posted: Sat Oct 08, 2005 5:05 pm   Post subject: (No subject)

Cervantes wrote:
This is getting off topic so in future post those kind of questions in General Discussion.

I'll answer it here this once though
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).
Geocities
Tripod (UK)
EDIT: Better yet, check the good free hosting thread in the Off Topic forum.

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.

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]

Posted: Sat Oct 08, 2005 5:21 pm   Post subject: (No subject)

Quote:
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

Posted: Sat Oct 08, 2005 5:27 pm   Post subject: (No subject)

theguru wrote:
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
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: