Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 The Basics and more
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Token




PostPosted: Fri Sep 09, 2005 6:59 pm   Post subject: 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.
http://www.compsci.ca/v2/viewtopic.php?t=8808#93255






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

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

Syntax:
var (variable name) : (variable type)


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

Syntax:
(variable) := (value)


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

code:
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
code:
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
code:
put "Please enter your full name: " ..
get name : *
put "Hello ", name



Delays

delays are simply a pause in the program measured in milliseconds,
Turing:
delay (milliseconds here)


Ex. A delay of one second would be
code:
delay (1000)


If statements

if statements help us to manipulate our program around user input, they are pretty straightforward

Syntax:

if (condition) then
              ...
            end if



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

code:
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
code:
if age <= 16 then
    put "you can't drive yet"
else
    put "congratulations you can drive."
end if


if you were to need 2 conditions to be true then you would use 'and'. If you needed one or the other to be true use 'or'.

ex
code:
var age : int

put "Enter your age: " ..
get age

if age < 16 or 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,
code:
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.

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

Syntax:
for (variable):#..#
Code here
end for


Ex
code:
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
code:
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
Syntax:
drawdot(x,y,color:int)


drawline- Draws a line from one point to another
Syntax:
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
Syntax:
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.
Syntax:
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.
code:
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

Syntax:
open :(file var), (file name:string), (put or get)


Interacting With the file

putting
Syntax:
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
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

Syntax:
exit when eof (filevar)


ex
code:
loop
    get : filevar, info
    exit when eof
end loop


closing a file
Syntax:
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
code:
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)
Sponsor
Sponsor
Sponsor
sponsor
Mr. T




PostPosted: Fri Sep 09, 2005 7:16 pm   Post subject: Alex's Opinion

w00t now I can be 1337 like Cervantes!!!
MihaiG




PostPosted: Fri Sep 09, 2005 7:19 pm   Post subject: (No subject)

preatty good *applause*... i learned a few things (/") with tabs indents etc... cool
[Gandalf]




PostPosted: Fri Sep 09, 2005 7:37 pm   Post subject: (No subject)

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




PostPosted: Sat Sep 10, 2005 6:35 am   Post subject: (No subject)

[Gandalf] wrote:

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. Smile
Token




PostPosted: Sat Sep 10, 2005 8:29 am   Post subject: (No subject)

Wow, thankyou everyone for the feedback. I'm happy to hear that everyone pretty much liked it, and thanks Cervantes for the bits Very Happy 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

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




PostPosted: Sat Sep 10, 2005 9:39 am   Post subject: (No subject)

Cervantes wrote:
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 Wink. 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




PostPosted: Sat Sep 10, 2005 10:06 am   Post subject: (No subject)

[Gandalf] wrote:
Cervantes wrote:
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 Wink. 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.

[Gandalf] wrote:

"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. Laughing
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Sat Sep 10, 2005 3:28 pm   Post subject: (No subject)

Hmm...not too bad.

A couple of pointers. As mentioned, please include all of your [code] or [syntax] tags. I don't particularly like the way you've done the 'syntax ...' thing. For example:

Token wrote:

Syntax- var (variable name) : (variable type)

syntax if (condition) then
...
end if


Please stay consistent. Might I offer the suggestion of instead:
syntax:
Turing:

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.

Token wrote:

**# 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? Laughing Gotta love that name! (Expose text at your own risk.)
[Gandalf]




PostPosted: Sat Sep 10, 2005 3:37 pm   Post subject: (No subject)

The # returns everything after it in a certain base, for example:
code:
put 2#1100
put <base>#<value>

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:
code:
put 16#F


Perhaps we should make a list of these names? Laughing
Cervantes




PostPosted: Sat Sep 10, 2005 3:53 pm   Post subject: (No subject)

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




PostPosted: Sat Sep 10, 2005 10:28 pm   Post subject: (No subject)

[Gandalf] wrote:
The # returns everything after it in a certain base, for example:
code:
put 2#1100
put <base>#<value>

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:
code:
put 16#F


Perhaps we should make a list of these names? Laughing


Methinks that if you don't put anything before the # it considers it as base 10. Try these:
Turing:

put #'u'
put #'U'
put #'uU'
rizzix




PostPosted: Sat Sep 10, 2005 10:57 pm   Post subject: (No subject)

yea... like my sig..
Cervantes




PostPosted: Sun Sep 11, 2005 6:40 am   Post subject: (No subject)

Another thing: Try putting no number in front, and a negative number after:
code:

put #-1

Produces maxnat + 1.
code:

put #-2

Produces maxnat
code:

put #-maxint

Produces maxint + 2
Token




PostPosted: Sun Sep 11, 2005 10:30 am   Post subject: (No subject)

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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: