
-----------------------------------
Token
Fri Sep 09, 2005 6:59 pm

The Basics and more
-----------------------------------
The basics 

This tutorial is designed to give you a quick look at programming in Turing. If you have programmed in other languages or are just a quick learner, then this should be all you need to get started, if not, here are some links to the various topics I mention in this tutorial. If those aren't enough or you'd like to expand check out this terrific walkthrough of Programming in Turing.
Many links are directed towards the same page.
put		variables		get		delay
if statements 	loop		for loop		math		draw		cls		animation with loops	file i/0		mousewhere	




 put

put displays information on the screen, this is in text format or string, so you would need to put it in quotations (" ")

this is an example of the put statement

put "This is an example of the put statement" 

Variables 

Variables are words that hold information, they can be changed and manipulated as you wish as
opposed to constants (const) there are 5 main types of variables.

String (string)
Holds textual information but must always be in quotations when outside the variable,

Integer (int)
Holds whole numbers, positive and negative

Real (real)
Holds numbers to decimal places.

Boolean (real)
Holds values of either true or false.

char (real)
Holds a character, a number, letter or symbol ex % .

before you can call on a variable you have to declare it like this

we will declare a variable to hold a name its a name which is text, so it will be a string

 var (variable name) : (variable type)

var name : string

okay so now we've created it, but we need to set it to a value, if the name will be the same every time then you just have to do this

 (variable) := (value)

name := "Matthew"

you can now  use this variable in a put statement, like shown below
put name

this returns Matthew on the screen, you could use a mix of text and variable, just separate them by a comma

put "Hello ", name

with string there are a few different little tricks you could use

 \n   Page break
 \t   Tab
 \"   Inserts quotations without closing the string

Just put them inside of the quotations, in the place where want them to take effect.

Get  

a get statement gets input from a user and enters it into a variable. It's pretty basic

Ex
get name
put "Hello ", name

there are also some extra tricks to it

 :*  - putting this after a variable allows spaces and it will take all of whatever is typed rather than just the first word.
 ..  - this joins any two lines together rather than starting on the next line like Turing does every time you start a put and a get statement.
It can be used to make a get statement get input right after the put statement above

ex
put "Please enter your full name: " ..
get name : *
put "Hello ", name


Delays 

delays are simply a pause in the program measured in milliseconds,
 delay (milliseconds here)

Ex. A delay of one second would be 
delay (1000)

If statements 

if statements help us to manipulate our program around user input, they are pretty straightforward


if (condition) then
              ...
            end if



var age : int

put "Enter your age: " ..
get age

if age = 16 then
    put "So its your first year driving?"
else
    put "You have been driving for more than one year or not at all"
end if


within one if statement you can have many different outcomes by using elsif, easiest to explain by example

put "Enter your age: " ..
get age

if age = 16 then
    put "First year driving"
elsif age = 19 then
    put "First year driving"
else
    put "Not a first? No problem."
end if

in place of = you could have < > =   the last two are less than and equal to and greater than and equal to.

ex
if age  70 then
    put "You probably shouldn't be driving "
elsif age > 16 and age < 80 then
    put "Have fun driving."
end if



loops 


loops are procedures that repeat whatever is inside until they are exited

Ex,
loop
    put "\nType exit as the name to exit"
    put "Enter your name: " ..
    get name : *
    exit when name = "exit"
end loop

To exit a loop you must have a condition, when it is true the loop will stop and the program will continue.

  exit when (condition) 

For Loops 

a for loop is a counted loop that each time it repeats it assigns the value of the count to a variable that only exists within the for statement.

for (variable):#..#
Code here
end for

Ex
var total, temp : int := 0  %% you must set it to zero because when you add its own value and its self it will crash

for i : 1 .. 5
    put "Mark ", i, " : " ..
    get temp
    total += temp             %+= is the same thing as saying total:= total + temp
end for
put "Average: ", total / 5 * 100

Math Functions

Simple to use, think of math functions as variables, as they are used in the same way, they can be separated from variables or strings by a comma, and they return a value. 

 *    Multiplication
 /    Divide
 +    Add
 -    Subtract
 **#  Exponent
ex
put "9 x 9 = ", 9 * 9
put "And the square of 9 is ", 9 ** 2

Draw Functions 

drawdot- Draws a dot of one pixel at a certain location
        drawdot(x,y,color:int)

drawline- Draws a line from one point to another
         drawline (x1,y1, x2,y2,color:int)

draw(fill)box- draws a box of color, filled or unfilled, the first point being the bottom left corner, the second being the top right
                draw(fill)box (x1,y1,x2,y2,color:int)

draw(fill)oval- Draws an oval, the co-ordinates being the center point of the circle.
   draw(fill)oval (x,y,xradius,yradius,color:int)

check help (F10) for many more draw commands



cls 

   cls clears the screen to whatever the background color is. you should place these strategically

       After delays when animating to ensure the picture being visible longer
      Before the delay to leave a time for the user to relax before entering the next piece of information



Animation with for loops 

you can set the range of a for statement to anything and you can use decreasing to make it run backwards,
   Ex.
for x : 5 .. maxx - 5 %%maxx returns the screen size for x
    drawfilloval (x, maxy div 2, 5, 5, red) %div 2 is used to make it half the screen's y
    delay (10)
    cls
end for

for decreasing x : maxx - 5 .. 5
    drawfilloval (x, maxy div 2, 5, 5, red)
    delay (10)
    cls
end for

this draws the circle move to the right and then return


File I/O 

There is a certain order of operations you need to follow when doing file I/O

Declare the Var   

The variable used to call on the file. this variable must be an int

Open the file
  you have to decide what you will be doing to the file, putting the info into the file, or getting the info from the file

 open :(file var), (file name:string), (put or get)

Interacting With the file

  putting
put : (filevar:int), (info, any type)

   getting
 This is more complicated because if you have more than one piece of data you would have to loop it and then tell it to exit the loop when the file has ended. to get one line from a file you need to use the following syntax
get:(filevar:int), (var: string/int, depending on the info in the file, string to be safe.)

To get a lot of info from a file you would have to loop it and then exit that loop at the end of the file using the eof command  

      exit when eof (filevar)

ex
loop
    get : filevar, info
    exit when eof
end loop

closing a file
 close: (filevar)


Mousewhere 

mousewhere returns the co-ordinates of the mouse in relation to the output screen and also whether or not the button is being held down or not. it then assigns all of this information to a variable.

ex
var x, y, b : int
loop
    mousewhere (x, y, b)
    locate (1, 1)
    put "X: ", x, " Y: ", y, " B: ", b

end loop

this program outputs the co-ordinates of the mouse onto the screen, this is a very useful procedure when drawing.


I made this tutorial for a friend of mine who didn't take grade 10 compsci and is now going into grade 11 compsci, I gave him a copy of Turing and this tutorial to try and get him up to speed. Let me know if anyone has anything to add? I hope this will help a lot of beginners to Turing because it starts from the very basics. 

-Jordan Willis (Token)

-----------------------------------
Mr. T
Fri Sep 09, 2005 7:16 pm

Alex's Opinion
-----------------------------------
w00t now I can be 1337 like Cervantes!!!

-----------------------------------
MihaiG
Fri Sep 09, 2005 7:19 pm


-----------------------------------
preatty good *applause*... i learned  a few things (/") with tabs indents etc... cool

-----------------------------------
[Gandalf]
Fri Sep 09, 2005 7:37 pm


-----------------------------------
It's good, mainly for the reason that those are the exact things that most beginner CompSci classes cover.  Actually, I think you effectively cover a whole course of Turing.  I need'nt remind you though, that there are numerous other beginner tutorials here already.

If you are planning on doing more Turing in the future, you should use commands like "Mouse.Where" instead of "mousewhere" to introduce you to modules.

-----------------------------------
Cervantes
Sat Sep 10, 2005 6:35 am


-----------------------------------
"]
I need'nt remind you though, that there are numerous other beginner tutorials here already. 

Indeed, and they cover the topics in much greater detail.  This may sound like I am putting yours down, but in fact, it is the opposite.  For the many who know other programming languages and are taking compsci courses at school and are therefore forced to learn Turing, a quick run through the language is what they need.  

This could use some cleaning up, mind you.  There's a code tag missing at one point, some text that snuck inside a code tag.  You say there's 4 main types of variables then list 3 (and there are 5: add boolean and char to your list).  Various capitalization and general appearance.  Headers could use some colour and size.  Little things, though they can make a pretty big difference.

Well done on the tutorial.  I would suggest, however, changing the tutorial to something more along the lines of "A Quick Run-Through, for Experienced Programmers".

I'll add it to the walkthrough in its own section.

+50 bits, whenever Dan fixes the donating/modding bits issue.  I owe you. :)

-----------------------------------
Token
Sat Sep 10, 2005 8:29 am


-----------------------------------
Wow, thankyou everyone for the feedback. I'm happy to hear that everyone pretty much liked it, and thanks Cervantes for the bits :D  and for the idea's for formatting. I know I probibly missed a few things like he said, and I'll go through it tommorow and fix them up, but if anyone finds anything else, I'd really appreciate them telling me. The code tags and stuff are messed up in a few places, but I took it all from the .t file I had originally wrote it in, removed about a half billion % signs, and tried to add the code tags, if you see anything else like that give me specific refrences, or even quote that area for me. I see where you're talking about Cervantes

Ex 
var total, temp : int := 0 %% you must set it to zero because when u add its own value and its self it will crash 
and 
Code: 
loop 
    get : filevar, info 
    exit when eof 
end loop 

closing a file 
    Syntax close: (filevar) 
I'll fix them the next chance I get, but I'm off, thanks again for the input, and thanks for the bits Cervantes!

-----------------------------------
[Gandalf]
Sat Sep 10, 2005 9:39 am


-----------------------------------
You say there's 4 main types of variables then list 3 (and there are 5: add boolean and char to your list).
Ah, I was going to say that you missed natural numbers ;).  Though I just noticed the word "main".  Still, there is nat for natural numbers.

"cerevents "
Hehe, Cervantes, you sure are being called quite a few things.

-----------------------------------
Cervantes
Sat Sep 10, 2005 10:06 am


-----------------------------------
"]You say there's 4 main types of variables then list 3 (and there are 5: add boolean and char to your list).
Ah, I was going to say that you missed natural numbers ;).  Though I just noticed the word "main".  Still, there is nat for natural numbers.

Good point.  And if you want to take it further, there's int1, int2, int4, which corresponds to 1-byte, 2-byte, and 4-byte integers.  Same goes for natural numbers: nat1, nat2, and nat4.

"]
"cerevents "
Hehe, Cervantes, you sure are being called quite a few things.

Indeed.  Perhaps I should change my account name to some garbled version of Cervantes, such as "Cevernuts", and see if people can turn it back to its original form. :lol:

-----------------------------------
Delos
Sat Sep 10, 2005 3:28 pm


-----------------------------------
Hmm...not too bad.

A couple of pointers.  As mentioned, please include all of your 
Syntax- var (variable name) : (variable type) 

syntax if (condition) then
...
end if 


Please stay consistent.  Might I offer the suggestion of instead:
syntax:

if (condition) then
   ...
end if


Another minor, but important, point (ha! try figure that one out).  Please stay consistent with your capitalization.  In one header you have 'Get' while in another you have 'cls', though both exist as lowercase commands in Turing.  Choose one or the other.


**# Exponent 

Yes, this works, but no, you don't need the '#'.  In fact, you've just done something a little odd right there.
I'm not entirely certain what the '#' actually does, I've played around a bit with it but its usage is still a little nebulous.
Anyone know what it means/does?  Perhaps *ahem* Clevernuts knows something about it? :lol:  Gotta love that name!  (Expose text at your own risk.)

-----------------------------------
[Gandalf]
Sat Sep 10, 2005 3:37 pm


-----------------------------------
The # returns everything after it in a certain base, for example:
put 2#1100
put #
puts what 1100 as base 2 would be at base 10.  So, 1100 in binary is 12.  I have no idea how that would work used with **, probably nothing?

Another example for hex would be:
put 16#F

Perhaps we should make a list of these names? :lol:

-----------------------------------
Cervantes
Sat Sep 10, 2005 3:53 pm


-----------------------------------
I think he meant for a number to take the place of the '#', not actually put thr number sign in the code.

I've never experimented with that before.  Neat, though it appears to be still rather unimplimented.

I say it is rather unimplimented because of two reasons.  First, base zero is not error trapped: Turing crashes.  Second, you cannot use a variable or constant in the expression.

-----------------------------------
Delos
Sat Sep 10, 2005 10:28 pm


-----------------------------------
"]The # returns everything after it in a certain base, for example:
put 2#1100
put #
puts what 1100 as base 2 would be at base 10.  So, 1100 in binary is 12.  I have no idea how that would work used with **, probably nothing?

Another example for hex would be:
put 16#F

Perhaps we should make a list of these names? :lol:

Methinks that if you don't put anything before the # it considers it as base 10.  Try these:

put #'u'
put #'U'
put #'uU'


-----------------------------------
rizzix
Sat Sep 10, 2005 10:57 pm


-----------------------------------
yea... like my sig..

-----------------------------------
Cervantes
Sun Sep 11, 2005 6:40 am


-----------------------------------
Another thing: Try putting no number in front, and a negative number after:

put #-1

Produces maxnat + 1.

put #-2

Produces maxnat

put #-maxint

Produces maxint + 2

-----------------------------------
Token
Sun Sep 11, 2005 10:30 am


-----------------------------------
hah, sorry Cervantes, I got it right the other 2 times, but its all fixed now  :wink: I'm working on fixing up the Tutorial, thanks for all the sudgestions, if I'll edit this post when I'm done and please let me know if I miss somthing. oh and by **# I meant **8 for example, or **9, as I did in the example.

-----------------------------------
Cervantes
Sun Sep 11, 2005 1:04 pm


-----------------------------------
Ah, colour! :)
Your syntax tags aren't working.  I believe the problem is the qutoation marks you're using.  You edited that in some text editor, then copy and paste, aye?  In the text box where you ordinarily type your messages, replace all the quotation marks.  That is, remove the old one and type a new " in.

Also, it seems you have cleaned most of it up, though the For Loops section remains the same.

Lastly, if you are interested in adding to this (or anyone), you can look at the Walkthrough for ideas as to the order to put things in.

-----------------------------------
Token
Sun Sep 11, 2005 1:50 pm


-----------------------------------
THANK YOUUUUUUUU!

Jesus! I was trying to get those to work for a half hour, maybe even more! hah well thanks for the help Cervantes  (note how it is correctly spelt). I made it all pretty looking now, I think what I'll do is add to it slowly from both that sudgestion thread you mentioned and from what I learn in school (concidering how far ahead I was last year, that wont be a great source). Also if there were tutorials written on the things that I covered, I'm going to link them to the other tutorial so they have a more in depth explaination... all in good time  :lol:  but I've got a few hours of homework I've been putting off so I'll leave that for another day. Thanks to everyone for their help.

-----------------------------------
Cervantes
Sun Sep 11, 2005 2:47 pm


-----------------------------------
Also if there were tutorials written on the things that I covered, I'm going to link them to the other tutorial so they have a more in depth explaination... all in good time  :lol:
That's good.  Those who choose to go with the "runthrough" as opposed to the "walkthrough" may still have difficulty with a particular topic, even if they are okay with the others.  For that topic, they could switch over and read the more long-winded explanation. :)

Oh, and it appears that you forgot a [/syntax] in the "putting to files" section, and the "end if" in the explanation of if structures is horribly off.  I hope I'm not annoying you with this attention to detail.

-----------------------------------
Token
Sun Sep 11, 2005 4:48 pm


-----------------------------------
I fixed that thing with the putting to files, and I'm starting to compile links right now, 

I hope I'm not annoying you with this attention to detail.

Definately not, this way I know what to watch out for next time, that way the next one I write will be better and will take less time to perfect.
