Hello everybody.
Welcome to this tutorial on Grammar of TurtleScript in KTurtle.
In this tutorial, we will learn about
Grammar of Turtle script and 'if'-'else' condition
To record this tutorial I am using, Ubuntu Linux OS version. 12.04. KTurtle version. 0.8.1 beta.
We assume that you have basic working knowledge of KTurtle
If not, for relevant tutorials, please visit our website. http://spoken-tutorial.org
Let's open a new KTurtle Application.
Click on Dash home.
In the Search bar, type KTurtle.
Click on the KTurtle icon.
We can also open KTurtle using Terminal.
Press CTRL+ALT+T simultaneously to open the Terminal.
Type KTurtle and press enter to open the KTurtle Application.
Let's first look at TurtleScript
TurtleScript is a programming language
It has different types of words and symbols used for various purposes
It instructs Turtle what to do
Grammar of TurtleScript in KTurtle includes-
Comments
Commands
Numbers
Strings
Variables and
Boolean values
Now we will see where to store numbers
Numbers can be stored in
Mathematical operators
Comparison operators and
Variables
I will zoom the program text for clear view.
First let's look at variables.
Variables are words that start with ‘$’ sign, for example $a.
Variables are highlighted in purple color.
Using the assignment, equal to (=), a variable is given its content.
Variables can contain numbers $a=100.
strings $a=hello or
boolean values that is true or false $a=true
Variable keeps the content until program finishes execution or until it is reassigned to something else.
For example, consider the code.
Let us type,$a = 2004
$b = 25
print $a + $b
Variable 'a' is assigned a value 2004.
Variable 'b' is assigned a value 25
print command, commands Turtle to write something on the canvas.
print command, takes numbers and strings as input.
print $a + $b commands Turtle to add two values and display them on the canvas.
Let's Run the code in slow speed.
Value 2029 is displayed on the canvas
Let us next see the Mathematical Operators.
Mathematical operators include, + (Addition) - (Subtraction) * (Multiplcation) and / (Division)
I will clear the current code from editor and type clear command and RUN to clean the canvas
I already have a program in a text editor.
I will Explain the code now
“reset” command sets Turtle to its default position
canvassize 200,200 fixes the width and height of the canvas to 200 pixels each.
value 1+1 is assigned to the variable $add,
Value 20-5 is assigned to variable $subtract,
value 15 * 2 is be assigned to the variable $multiply.
30/30 is assigned to the variable $divide.
go 10,10 commands to Turtle to go 10 pixels left of canvas and 10 pixels from top of canvas
print command displays the varible on the canvas
I will copy the code from text editor and paste it into KTurtle editor.
Pause the tutorial and type the program into KTurtle editor.
Resume the tutorial after typing the program
Let us click on Run button to run the program
Command which is getting executed is highlighted on the editor.
Turtle displays the values on the canvas at the specified positions.
Let us consider a simple example for using comparison operator .
I will clear the current code from editor and type clear command and RUN to clean the canvas
I will zoom the progrm text to have clear view
Let's type
$answer = 10 > 3
print $answer
Here 10 is compared to 3 with the ’greater than’ operator.
The result of this comparison, the boolean value true is stored in the
variable $answer and the value true is displayed on the canvas.
Let's run the code now
Turtle displays Boolean value true on the canvas.
Now lets see how Strings work in this application –
Strings can be put in variables like numbers
Strings cannot be used in mathematical or comparison operators
Strings are highlighted in red color
KTurtle identifies a line in double quotes as a string
I will clear the current code from editor.type clear command and Run to clean the canvas
Now I will explain about Boolean values.
There are only two boolean values: true and false.
For example Let's type the code
$answer = 7<5
print $answer
Boolean value false is assigned to variable $answer because 7 is greater than 5
Let's Run the code now
Turtle diplays Boolean value false on the canvas.
Let's next learn about “if-else” conditon.
‘if’ condition is executed only if the boolean value evaluates ‘true’
‘else’ condition is executed only if the ‘if’ condition is ‘false’ .
I will clear the current code from editor.type clear command and Run to clean the canvas
I already have a code in a text file.
This code compares numbers 4 , 5 and 6 and displays the results accordingly on the canvas.
I will copy the code from text editor and paste it into KTurtle editor.
Pause the tutorial and type the program into your KTurtle editor.
Resume the tutorial after typing the program
Let's Run the code now
the Turtle has compared the values 4 and 5.
and has displayed the result 4 is smaller than 6 on the canvas .
With this we come to the end of this tutorial.
Let's summarize.
In this tutorial, we have learnt about
Grammar of Turtle script and
‘if-else’ condition
Now to the assignment part.
Solve an equation using
if - else condition
Mathematical and comparision operators
Display the results using “print” and “go” commands.
To solve the assignment
Choose any four random numbers
Multiply two sets of random numbers
Compare the results using the comparison operators
Display both the results
Display greater result at the center of the canvas
You can choose any equation which you like.
Watch the video available at this URL http://spoken-tutorial.org/What is a Spoken Tutorial
It summarises the Spoken Tutorial project
If you do not have good bandwidth, you can download and watch it
The Spoken Tutorial Project Team :
Conducts workshops using spoken tutorials
Gives certificates to those who pass an online test
For more details, please write to contact@spoken-tutorial.org
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India
More information on this Mission is available at this link http://spoken-tutorial.org/NMEICT-Intro ]
The Script is contributed by ITfC Bangaluru.
This is Madhuri Ganpathi from IIT Bombay signing off Thank you for joining.