Input, Output and Variables
Author |
Message |
michaelp
|
Posted: Mon Mar 03, 2008 8:55 pm Post subject: Input, Output and Variables |
|
|
Link to original tutorial is [url="http://www.punbb-hosting.com/forums/programvillage/viewtopic.php?id=112"]here.[/url] Please visit!
[quote=michaelp]Turing Tutorial #1: The Basics
Hi, and welcome to michaelp's Turing tutorials. These are going to be a set of tutorials that will help you learn the programming language Turing. There will be exercises at the end of each tutorial for you to complete. It will hopefully help you understand what you are doing more.
If you would ever like to suggest something for a tutorial or if there is a mistake in a tutorial that is already up, private message me on www.programvillage.co.cc or e-mail me at baseball.mjp@gmail.com. Thank you for your support.
Now, if have not programmed before, Turing will be a good introductory language because its easy to understand syntax. To put text on the screen, you use a statement called "put". Simple, eh?
What is Turing?
Wikipedia Article
Holtsoft Site
http://compsci.ca/v3/
Turing used to be a programming language that was not free to program in. A company named Holtsoft would decide what happened with the language and how often they updated the Turing IDE. But not too long ago, they decided to make the Turing IDE free, and the language free to use for commerical, personal, and educational use. It is no longer being developed by Holtsoft though. You can download the IDE here. If you are wondering, it is version 4.1.1a.
First of all, this tutorial will be covering:
-Comments
-Text Output
-Variables
-Input
I will try to help you understand these concepts as well as I can throughout the tutorial.
Now that you know what Turing is, we can ALMOST start writing our code. But before that, you are going to need to get the Turing IDE. Find the Turing download you got before.
Extract the folder somewhere on your computer, such as "C:\", C: being the main hard drive. Now, start up the Turing executable and let's get coding!
Hello, World!
Our first program is going to be a simple "Hello, World!" program using Turing's "put" statement. Type this into the IDE:
code: | put "Hello, World!" |
Now, how to run this program.
If you would like to create an exe for this (I doubt you would since it's such a simple program), find the run option. From there, go to "Generate stand-alone program". Choose your preferences for your program, then press okay and choose the name and where you want to save your program. Now, whenever you click on that program, it will run that code.
An easier way (in my opinion) to run your code is to click the large run button in the IDE. This will immediately execute your code. For saving, click "File" and choose "Save". Choose where you would like to save the file. Use a ".t" extension for the file you are saving. Now, you can open up that file with Turing whenever you want.
When you run the program, you should see a line of text that says:
Quote: Hello, World!
Whatever you put in between the quotes is what will be printed out. Why does it need to be inside quotes? Because it is considered a "string" which is a computer way of saying "characters mixed together". Strings are always enclosed in quotes.
First exercise: Write a program that instead of printing "Hello, World!" it prints "Hello, " and then your name.
Solutions are at the end of the tutorial.
Done? Let's move onto the more complicated things you can do with the "put" statement.
To put "Hello" and "World" on different lines:
code: | put "Hello,"
put "World!" |
Now, run the program. You should see:
Quote: Hello,
World!
When you use a new put statement in Turing, it goes to the next line of output. But if you wanted to move 2 lines down the output? You could not just move the put statement down another line.
code: | put "Hello,"
put "World!" |
That produces the same output as the last program. Now, you can move the "World!" text down another line, and make that same program with only 1 put statement.
code: | put "Hello,\n\nWorld!" |
Same program with one line:
put "Hello,\nWorld!"
You're probably wondering right now: "What the heck is that '\n' thing?"
That is called an escape sequence. There are only a few escape sequences in Turing, which are:
code: | \n - Skips a line, as you saw
\t - A tabs worth of space
\" - Inserts a quotation without closing the string |
The last one may seem a little funny to you. You might be wondering: "Why can't I just put in a quotation mark and it will appear?" Remember what I said earlier about quotes? Since strings are always enclosed in quotes, if you put in a plain quotation mark, Turing will think that you want to end the string, even if you don't. So by putting a "\" mark in front of it, it tells the compiler that "I would like the insert a quotation mark, not end the string." An easier way may be by just using a single quotation mark, or aprostrophe. (')
Exercise 2: Make a program that prints "Hello," on the first line and on the second, prints "World!" a tabs worth of space away from the side of the Turing output window. All with one put statement.
One more quick thing that I should teach you before going into the subject of variables is comments. Comments are parts of your code that you can type in by yourself to help explain to yourself or someone else what your code is doing. They are ignored by the compiler, so you don't need to worry about putting in lots of comments. They are included in pretty much every single programming language. In Turing, you use the symbo Here is a quick program with comments.
code: | %Hello World.t
%Simply prints out "Hello, World!" to the screen
put "Hello, World!" %This comment goes on for the rest of the line
%There only 1 line comments in Turing, so from each "%" it extends to
%the end of the line |
Variables!
A variable holds the value of something. For example "computerPrice" could hold the value of how much your computer cost. Or "yourName" could hold, well, your name. There are an extrememly useful thing in programming. In fact, I don't know how many useful programs you could actually make without them. So, before I show you how to make a variable, there are 4 different types of variables in Turing, which are:
Quote: bool - Stands for boolean. Can hold a true or false value.
real - Is a number that has decimals. 3.14 is a 'real' number in Turing.
int - Stands for integer. Is a number that does not have any decimals.
string - Used to hold things like a name of a person, etc. Holds characters.
Now, that you know what kind of variables there are, let me show you how to define one.
var variableName : variable type
So, if I wanted a string type variable named yourName, I would do this:
code: | var yourName : string |
Easy enough? Yup. But right now, you're probably wondering: "Okay, I have this variable here. What can I do with it?" Well, some things you can do with it are print it (which you will see in a sec), recieve input from the user into it (later in the tutorial), use it to keep track of where a sprite may be in a game, keep track of how much money you have, and lots more. But, to actually put a value in the variable, here is what you do:
code: | %One way
var myName : string %Variable is declared
myName := "michaelp" %myName now contains the string value "michaelp"
%Second way
var myName := "michaelp" |
Now, here is what happens in each of those code snippets. In the first way, the first thing you do is called a declaration. You are declaring the variable and announcing its existence. You then use the assignment operator ( := ) to put the value "michaelp" into myName.
The second way is called initialization. In one line, you can delcare the variables existence and assign it a value. But here, you don't see any variable type. Why? When you use this way for a variable, it automatically detects the type you need depending on what is to the right of the assignment operator. Now, if you want to print my name...
code: | var myName := "michaelp"
put "Hello", myName, "!" |
First, I initialize myName to michaelp. Then, in quotes, I put "Hello". I then put a comma after the last quote because that shows I am going to put something new after it, such as a variable or expression. Then, I put another comma after myName, because I plan to put something else, which turns out to be an exclamation mark. If you only wanted one thing, such as the variable or a string that is in quotes, you wouldn't need any commas.
Exercise 3: Write a program that outputs just your name (using variables), and nothing else. Use both ways shown. 2 programs.
Input!
We are now going to learn how to get input from a user in Turing. It is very easy. You have to use something called "get". Ususally, you will use the get statement and then type the variable you want to read information into. Here's an example of getting a number and then printing it.
code: | put "Please enter an integer: "
var number : int
get number
put number |
All this program does is ask you to enter an integer and then you enter one, then it displays the integer you entered. Now, you may be wondering why I said enter an 'integer' and not just any number. If the user had entered a number such as 5.4, you get a "run-time error" that says there was invalid integer input. Why? Because 5.4 is not an integer, it is a 'real' number in Turing. You have to make sure that you specify what you want the user to enter because if you don't, something like what I just mentioned can happen. If you had made number a 'real' type, then you probably could have said any number.
Exercise 4: Rewrite the previous program with the real data type, and changing 'integer' to 'any number'.
Exercise 5: Make a program that request a users name, then says "Hello, " and then their name.
Note: You need to complete Exercise 5 to continue.
So, I'm assuming you have tried out your program. If you haven't, go do that now. One thing that you may or may not have noticed is that the program would not take your full name if you tried to enter it. It would only display your first name. There are 2 ways you can get around this. The first way is requesting the first name, then the last name like so:
code: | var firstName : string
var lastName : string
put "Please enter your first name: "
get firstName
put "Please enter your last name: "
get lastName
put "Hello, ", firstName, " ", lastName, "!" |
Or, you can do something much simpler:
code: | var fullName : string
put "Please enter your full name: "
get fullName : *
put "Hello, ", fullName, "!" |
See? Both ways work in printing your full name.
Now, one last thing on variables to finish this tutorial off:
code: | const PI := 3.141592
var radius : real
put "Please the raidus of a circle: "
get radius
var circleArea := radius ** 2 * PI
put "The area of the circle is: ", circleArea, "." |
You see the first line and how put the keyword const? That means the value is going to be "constant" and it is not allowed to be changed. If you try to change it, you will get an error. Go ahead and try to assign PI a value. And are you wondering why that constant is in capital letters? It's basically how constants are. Almost all programmers use capital letters for constants and underscores for a new word. Example:
code: | const MY_NAME := "michaelp" |
Now, the rest should make sense except for the circleArea line.
What this does is assign the area of the circle to raidus "to the power of" ( ** ) 2 and then mulitply ( * ) that by PI.
Hope you learned some more things about Turing. Look forward to my next tutorial, which will not cover as many topics and hopefully be shorter.
Solutions:
#1:
code: | put "Hello, michaelp!" %Replace michaelp with your name. |
#2:
code: | put "Hello,\n\tWorld!" %You may have also done this
put "\tHello,\n\tWorld!" %Either way works fine, I was thinking of the first way though |
#3:
code: | var myName := "michaelp" %Again, replace michaelp with your name
put myName
%or
var myName : string
myName := "michaelp"
put myName |
#4:
code: | put "Please enter any number: "
var anyNumber : real
get anyNumber
put anyNumber |
#5:
code: | put "What is your name?"
var userName : string
get userName
put "Hello, ", userName, "!" |
Visit www.programvillage.co.cc for updates and a great place for programmers! :laugh:[/quote] |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TokenHerbz
|
Posted: Tue Mar 04, 2008 12:47 am Post subject: RE:Input, Output and Variables |
|
|
Wrong thread, but good try
effort is good to see... |
|
|
|
|
|
Tony
|
|
|
|
|
michaelp
|
Posted: Tue Mar 04, 2008 3:54 pm Post subject: RE:Input, Output and Variables |
|
|
I didn't put it in tutorials?
Where did I put it then? |
|
|
|
|
|
Tony
|
|
|
|
|
michaelp
|
|
|
|
|
|
|