Difference between revisions of "KTurtle/C3/Common-Errors-in-KTurtle/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(37 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
Title of the tutorial:
 +
 +
Author: Madhuri Ganapathi
 +
 +
Key words:  Errors, syntax errors, run time errors, logical errors, compilation, clear command, Video tutorial.
 +
 +
 +
 
{|border =1
 
{|border =1
 
!Visual Cue
 
!Visual Cue
Line 4: Line 12:
 
|-
 
|-
 
||Slide Number 1
 
||Slide Number 1
||
+
||Hello everybody.
  
Hello everybody.
+
Welcome to this tutorial on '''Common Errors''' in '''KTurtle.'''
 
+
Welcome to this tutorial on Common Errors  in KTurtle.
+
 
|-
 
|-
 
||Slide  Number 2
 
||Slide  Number 2
Learning Objectives
+
 
 +
'''Learning Objectives'''
 
||In this tutorial, we will learn about
 
||In this tutorial, we will learn about
Syntax errors  and
+
* '''Syntax errors'''  
Runtime errors
+
 
 +
* '''Runtime errors'''  and
 +
 
 +
* '''Logical errors'''
 
|-
 
|-
 
|| Slide Number 3
 
|| Slide Number 3
  
System Requirement
+
'''System Requirement'''
 
||To record this tutorial I am using,
 
||To record this tutorial I am using,
Ubuntu Linux OS v. 12.04.  
+
 
KTurtle v. 0.8.1 beta.
+
'''Ubuntu''' '''Linux''' OS version 12.04.  
 +
 
 +
'''KTurtle''' version. 0.8.1 beta.
 
|-
 
|-
 
||Slide  Number 4
 
||Slide  Number 4
Pre-requisites
+
 
||We assume that you have basic working knowledge of KTurtle   
+
'''Pre-requisites'''
 +
||We assume that you have basic working knowledge of '''KTurtle'''  
  
 
If not,  
 
If not,  
 +
 
for relevant tutorials, please visit our website.  
 
for relevant tutorials, please visit our website.  
http://spoken-tutorial.org
+
 
 +
'''http://spoken-tutorial.org'''
 +
|-
 +
||Slide Number 5
 +
 
 +
'''Definition of Error'''
 +
||Let's first define,  What is an error ?
 +
 
 +
'''Error''' is a mistake in a program that produces an '''incorrect''' or '''unexpected''' result.
 
|-
 
|-
 
||
 
||
||First I will explain about Types of errors.
+
||First I will explain about '''"Types of errors".'''
 
|-
 
|-
 
||Slide  Number 5
 
||Slide  Number 5
Syntax Errors
 
  
||A syntax error is a violation of  grammatical rules, of a programming language.
+
'''Syntax Errors'''
Compilation fails when a program has syntax errors.
+
 
Syntax errors are easy to find and fix.
+
||A '''syntax error''' is a '''violation of  grammatical rules''', of a '''programming language'''.
For Eg:   
+
 
Unmatched parentheses, square brackets and curly braces.  
+
'''Compilation''' fails when a program has '''syntax errors'''.
Use of variable that has not been declared.
+
 
Missing quotes in strings.
+
'''Syntax errors''' are easy to find and fix.
 +
 
 +
For Example:   
 +
 
 +
* '''Unmatched parentheses''', '''square brackets''' and '''curly braces'''.  
 +
 
 +
* Use of ''' variable''' that has not been '''declared'''.
 +
 
 +
* Missing '''quotes''' in '''strings.'''
 
|-
 
|-
 
||Switch to KTurtle Application
 
||Switch to KTurtle Application
Dash  home >>Search bar appears>>
 
In the Search bar type KTurtle
 
Click on the  KTurtle icon.
 
||Let's open a new KTurtle Application.
 
Click on  Dash  home. The Search bar appears.
 
  
In the Search bar, type KTurtle.
+
Dash  home >>In the Search bar type KTurtle
  
 
Click on the  KTurtle icon.
 
Click on the  KTurtle icon.
 +
||Let's open a new '''KTurtle''' Application.
 +
 +
Click on  '''Dash  home'''. In the Search bar, type '''KTurtle.'''
 +
 +
Click on the  '''KTurtle''' icon.
 
|-
 
|-
 
||
 
||
||Let's begin the tutorial with some types of syntax errors.
+
||Let's begin the tutorial with some types of''' syntax errors'''.
 
|-
 
|-
 
||
 
||
 
||I already have a program in a text editor.  
 
||I already have a program in a text editor.  
 
|-
 
|-
||reset
+
||'''Comment code'''
$a=ask "enter any number and click Ok"
+
||To explain the '''error''' in the program, I will comment part of the code.  
$r = ask "enter any number and click ok"
+
|-
$n = ask "enter any number and click ok"
+
||Highlight
$T = $a*($r^$n)*($r^-1)
+
print $T
+
||Let copy the program into Kturtle Editor.
+
  
Please pause the tutorial and type the program into KTurtle editor
+
'''$a=ask "enter any number and click Ok"'''
 +
||Here, I will comment the line
  
 +
'''$a=ask within double quotes "enter any number and click Ok"'''
 +
|-
 +
||Type #
 +
||I will use '''hash(#)''' sign to comment the line.
 +
|-
 +
||'''reset'''
 +
 +
'''#$a=ask "enter any number and click ok"'''
 +
 +
'''print $a^3''''
 +
 +
'''spritehide'''
 +
||I will copy the program from text editor and paste it into '''Kturtle's''' Editor.
 +
 +
Pause the tutorial here and type the program your into '''KTurtle''' editor
  
 
Resume the tutorial after typing the program.
 
Resume the tutorial after typing the program.
 
|-
 
|-
||Zoom text
+
||'''Run the program'''
||Let me zoom into the program text.
+
||Let's click on '''Run''' button to run the program  
It may possibly be a little blurred.
+
 
 +
'''Complier''' shows the following error,
 +
 
 +
''' variable "$a" was used without first being assigned  to a value.'''
 
|-
 
|-
||
+
||Highlight '''Line number 4'''
||To explain the error in the program, I will delete part of the code.
+
||Here the error is in line number 4.
 +
 
 
|-
 
|-
||Highlight
+
||'''Syntax error'''
$a=ask "enter any number and click Ok"
+
||This is a '''syntax error'''. It occured, as the variable 'a' was not declared.
||Here, I will deleted the line $a=ask "enter any number and click Ok"
+
 
|-
 
|-
||Run the program
+
||Highlight '''$a=ask "enter any number and click Ok"'''
||Let's Click on Run button to run the program
+
 
Complier shows the following error,
+
'''Remove the comment'''
The variable $a was used without first being assigned  to a value.
+
||So I will go to line number 2 remove the comment.
 
|-
 
|-
||Replace the  deleted line
+
||'''reset'''
||I will replace the deleted line  $a=ask "enter any number and click Ok"  
+
 
 +
'''$a=ask "enter a number and click ok" '''
 +
 
 +
'''print $a^3 '''
 +
 
 +
'''spritehide'''
 +
|| I will copy the program from text Editor and paste it into''' KTurtle's editor'''.
 +
|-
 +
||'''Run the program'''
 +
||Let's click on '''Run''' button to run the program
 +
 
 +
Enter '''6''' for '''a''' value and click OK
 +
 
 +
Program runs without errors.
 
|-
 
|-
||Run the program
+
||'''clear command'''
||Let's run again
+
||I will clear the current program from '''KTurtle''' editor.
press F5 key to run the program
+
  
We see that the program runs without errors.
+
Type '''clear''' command and '''Run''' to clean the canvas.
 
|-
 
|-
 
||
 
||
||Next let's look at another type of error.
+
|| let's next look about another error.
 
|-
 
|-
 
||
 
||
 
||I already have a program in a text editor.  
 
||I already have a program in a text editor.  
 
|-
 
|-
||reset
+
|| Highlight
$degree=ask "enter any angle value and click OK"
+
$radian=(pi/180)*$degree
+
go 50,50
+
print  "Value in radians is"+ "  " +$radian
+
spritehide
+
||Let me copy the program into Kturtle Editor
+
  
Please pause the tutorial and type the program into KTurtle editor  
+
'''$radian=(pi/180)*$degree'''
 +
||Here the value of "'''pi'''" is '''predefined''' in KTurtle.
 +
|-
 +
||Highlight $
 +
||Let's delete the "'''$'''" sign in the program.
 +
|-
 +
||'''reset'''
 +
 
 +
'''degree=ask "enter an angle value and click OK"'''
 +
 
 +
'''$radian=(pi/180)*$degree'''
 +
 
 +
'''go 50,50'''
 +
 
 +
'''print  "Value in radians is"+ "  " +$radian'''
 +
 
 +
'''spritehide'''
 +
||I will copy the program from text editor and paste it into ''' Kturtle's Editor'''
 +
 
 +
Pause the tutorial and type the program your into '''KTurtle editor'''
  
  
 
Resume the tutorial after typing the program.
 
Resume the tutorial after typing the program.
 
|-
 
|-
||Zoom text
+
||'''Run the program'''
||Let me zoom into the program text.  
+
||Let's Click on '''Run''' button to run the program.  
It may possibly be a little blurred.
+
 
 +
'''Complier''' shows the following error,
 +
 
 +
'''you cannot put “=” here'''
 
|-
 
|-
||Highlight $
+
||Highlight '''Line number 2'''
||Let's  delete $ signs in the program.
+
||This error is in line number 2.  
 
|-
 
|-
||
+
||'''Show error'''
Run the program
+
||This is a '''syntax error''' it occured, as there is no '''container  of variable.'''
||Let's Click on Run button to run the program.
+
Complier shows the following error,
+
you cannot put “=” here
+
 
|-
 
|-
||Replace $ signs
+
||'''Replace $ sign'''
||Let's replace the $ signs and run again.
+
||Let's go back to the program replace the $ sign.
 +
|-
 +
||'''reset'''
 +
 
 +
'''$degree=ask "enter an angle value and click OK" '''
 +
 
 +
'''$radian=(pi/180)*$degree '''
 +
 
 +
'''go 50,50'''
 +
 
 +
'''print "Value in radians is"+ " " +$radian'''
 +
 
 +
'''spritehide'''
 +
||I will copy the program from text editor and paste it into '''KTurtle's''' Editor
 +
|-
 +
||'''Run the program'''
 +
||let's click on '''Run''' button to run the program.
 +
 
 +
Enter '''45''' for angle value and click '''OK'''
  
Program runs properly
+
Program runs without errors.
 
|-
 
|-
||Highlight "Value in radians is"
+
||Highlight '''"enter an angle value and click OK" '''
||Let's remove the one of the quotes of the string.
+
||Let's remove one of the quotes of the string.  
 
|-
 
|-
||Run the program
+
||'''reset'''
||Let's click on  Run button to run the program
+
Complier shows the following error,
+
  
text string was not properly closed, expected a double quote “ ” to close the string.
+
'''$degree=ask "enter an angle value and click OK'''
 +
 
 +
'''$radian=(pi/180)*$degree'''
 +
 
 +
'''go 50,50'''
 +
 
 +
'''print "Value in radians is"+ " " +$radian'''
 +
 
 +
'''spritehide'''
 +
|| I will Copy the program from text editor and paste it into '''KTurtle's ''' Editor
 
|-
 
|-
||Repalce the quotes
+
||'''Run the program'''
||Let's replace the quotes and run again.
+
||Click on  '''Run''' button to run the program
press F5 key run the program.
+
 
program runs without errors.
+
'''Complier''' shows the following error,
 +
 
 +
'''Text string was not properly closed, expected a double quote “ ” to close the string.'''
 
|-
 
|-
||Slide Number 6
+
||Highlight '''Line number 2'''
Runtime errors
+
||Here the error is in line number 2.  
||Run-time error occurs during the execution of a program.
+
It may crash the program when you run it.
+
Runtime errors are commonly due to wrong input from the user.
+
Compiler cannnot find these errors.
+
For example:
+
* Trying to multiply or divide by a variable that contains no value.
+
*  Run a loop without a condition or increment.
+
 
|-
 
|-
||
+
||'''Replace the quotes'''
||Next we will look at errors related to print command
+
|| I will go to line number 2 and replace the quotes.
 
|-
 
|-
 +
||'''reset'''
 +
 +
'''$degree=ask "enter an angle value and click OK" '''
 +
 +
'''$radian=(pi/180)*$degree'''
 +
 +
'''go 50,50'''
 +
 +
'''print "Value in radians is"+ " " +$radian'''
 +
 +
'''spritehide'''
 
||
 
||
||I already have a program in a text editor.
+
I will copy the program from text editor and paste it into''' Kturtle's''' Editor
 
|-
 
|-
||reset
+
||'''Run the program'''
$C= ask "enter any number in celsius scale and click OK"
+
||Click on '''Run''' button to run the program
message "$F is Fahrenheit"
+
$F=(9/5*$C)+32
+
go 40,40
+
print "Value in Fahrenheit =" +" "+ $F+"F"
+
||Let me copy the program into Kturtle Editor
+
  
Please pause the tutorial and type the program into KTurtle editor
+
Enter '''45''' for angle value and click OK.
  
 +
Program runs without errors.
  
Resume the tutorial after typing the program.
+
This way you can find the line at which error has occured, and also correct it.  
 
|-
 
|-
||Zoom text
+
||
Let me zoom into the program text.
+
||Let's now learn about runtime errors.
It may possibly be a little blurred.
+
 
|-
 
|-
||Highlight "Value in Kelvin ="+ $K + "K"  and delete
+
||Slide Number 6
||Let's delete line after the keyword print "Value in Kelvin ="+ $K + "K"
+
 
 +
'''Runtime errors'''
 +
||'''Run-time error''' occurs during the '''execution''' of a program.
 +
 
 +
It may '''crash''' the program when you run it.
 +
 
 +
'''Runtime errors''' are commonly due to wrong input from the user.
 +
 
 +
'''Compiler''' cannnot find these '''errors'''.
 +
 
 +
For example:
 +
* Trying to divide by a '''variable''' that contains no value.
 +
 
 +
* Run a loop without a '''terminating condition''' or '''increment value'''.
 
|-
 
|-
||Run the program
+
||'''“clear” command''''
||Let's click on Run button to run the program.
+
||I will clear the current program from the KTurtle editor.
Complier does not shows any error.
+
  
Here we get an error as the program runs.
+
Type '''clear''' command and '''Run''' to clean the canvas.
The print command was called with 0 but it needs  1 parameter.
+
It is a runtime error.
+
 
|-
 
|-
||Replace the deleted line
+
||Highlight the program
Let's replace the deleted line and Run again
+
||I already have a program in a text editor.
 +
 
 +
This program '''divides''' two numbers.
  
program runs without runtime errors.
+
'a' is''' dividend''' and 'r' is '''divisor'''.  
|-
+
||Repalce the quotes
+
||Let's replace the quotes and run again.
+
press F5 key run the program.
+
program runs without errors.
+
 
|-
 
|-
 +
||'''reset'''
 +
 +
'''$a=ask "enter a number for 'a' and click Ok"''''
 +
 +
'''$r = ask "enter a number for 'r' and click OK"'''
 +
 +
'''$d=$a/$r'''
 +
 +
'''print $d'''
 
||
 
||
||Next let's look as run time errors in loops.
+
I will copy the program from text editor and paste it into '''KTurtle's''' Editor.
 +
 
 +
Pause the tutorial and type the program into your '''KTurtle''' editor
 +
 
 +
Resume the tutorial after typing the program.  
 
|-
 
|-
||
+
||'''Run the program'''
||I already have a program in a text editor.
+
||Let's click on  '''Run''' button to run the program.
|-
+
||#program to find multiples of 2
+
reset
+
$x=0
+
message"In while loop, code inside the loop repeats till boolean evaluates false"
+
while $x<20 {
+
$x=$x+2
+
$x=$x*1
+
fontsize 15
+
forward 20
+
print $x
+
}
+
||Let copy the program into Kturtle Editor
+
  
Please pause the tutorial and type the program into KTurtle editor
+
le'ts enter '''5''' for 'a' and click OK
  
 +
enter '''0''' for 'r' and click OK
  
Resume the tutorial after typing the program.
+
Here we get a ''' runtime error''' ,
  
 +
“'''you tried to divide by zero'''”
 
|-
 
|-
||Zoom text
+
||Highlight '''Line number 4'''
||Let me zoom into the program text.
+
||This error is in line number 4.
It may possibly be a little blurred.
+
 
|-
 
|-
||Highlight  $x=$x+2
+
||'''Run time error'''
$x=$x*1
+
||This error occurs as we cannot '''divide''' a number with''' zero'''.
||Let's delete  the lines $x=$x+2  and  $x=$x*1
+
 
|-
 
|-
||Run the program
+
||Run the program  
||Let's click on the Run button to run the program.
+
||Let's run the program again.  
Complier does not show any error.
+
  
We get an unexpected error runtime error.
+
Enter '''5''' for number and click OK
  
Loop become an infinite loop of zeros.
+
enter '''2''' for 'r' and click OK
|-
+
 
||Abort the process
+
Program without errors.
||I will click on Abort  button to abort the process.  
+
 
|-
 
|-
||Undo the deleted line
+
||'''“clear” command'''
||Let's undo the deleted line and Run again
+
||I will clear the current program from '''KTurtle''' editor.
  
program runs properly as expected.
+
Type '''clear''' command and '''Run''' to clean the canvas.
 
|-
 
|-
 
||
 
||
||Let's now try another example for runtime error.
+
||Next we will learn about ''' logical errors'''.
 +
|-
 +
||'''Logical errors'''
 +
 
 +
||'''Logical error''' is a mistake in a program's '''source code''' that results in '''incorrect''' or '''unexpected''' behavior.
 +
 
 +
For example,
 +
 
 +
'''Assigning''' a value to the wrong variable.
 +
 
 +
'''Multiplying''' two numbers instead of '''adding'''.
 
|-
 
|-
 
||
 
||
 
||I already have a program in a text editor.  
 
||I already have a program in a text editor.  
 
|-
 
|-
reset
+
||'''reset'''
$x=8
+
$r = 1
+
for $i = 1 to $x {
+
$r = $r * $i
+
forward 20
+
print $r
+
}
+
||Let copy the program into Kturtle Editor.
+
  
Please pause the tutorial and type the program into KTurtle editor
+
'''$x=30
  
 +
'''message"In while loop, code inside the loop repeats till boolean
  
Resume the tutorial after typing the program.
+
'''evaluates false"
 +
 
 +
'''while $x>20''' {
 +
 
 +
'''$x=$x+1'''
 +
 
 +
'''fontsize 15'''
 +
 
 +
'''forward 20'''
 +
 
 +
'''print $x''' }
 +
||I will copy the program from text editor and paste it into '''Kturtle's''' Editor
 +
 
 +
Pause the tutorial here and type the program into your '''KTurtle''' editor
 +
 
 +
Resume the tutorial after typing the program.  
 
|-
 
|-
||Zoom text
+
 
||Let me zoom into the program text.  
+
||'''Run the program'''
It may possibly be a little blurred.
+
||Now click on the '''Run''' button to run the program.  
|-
+
 
Highlight  $r = $r * $i  and
+
A dialog box pops-up, let's click OK.
print $r
+
 
||Let's delete the lines  $r = $r * $i  and  print $r
+
Loop goes into an '''infinite loop'''.
 
|-
 
|-
||Run the program
+
||Highlight '''“ while”  loop condition'''
||Let's click on the Run button to run the program.
+
||We see that '''“while”''' loop prints numbers from 31 and it is still printing.
Complier does not show any error.
+
  
We get an unexpected error runtime error.
+
This is a '''logical error'''.
  
We see that only the Turtle has moved forward.
+
In the “while” condition x is greater than 20,
  
“for” loop is not executed.
+
but the '''variable''' x is always greater than 20
 +
 
 +
So, the loop never '''terminates.'''
 +
|-
 +
||'''Abort the process'''
 +
||I will click on '''Abort''' button to abort the process.
 +
|-
 +
||Change '''$x=$x+1''' to '''$x=$x-1'''
 +
||Let's change $x=$x+1 to $x=$x-1.  
 
|-
 
|-
||Undo the deleted line
+
||'''reset'''
||Let's undo the deleted lines  and Run again.
+
  
program runs properly as expected.
+
'''$x=30'''
 +
 
 +
'''message"In while loop, code inside the loop repeats till boolean
 +
evaluates false" '''
 +
 
 +
'''while $x>20 { '''
 +
 
 +
''''$x=$x-1''''
 +
 
 +
'''fontsize 15'''
 +
 
 +
'''forward 20'''
 +
 
 +
'''print $x } '''
 +
|| I will copy the program from text editor and paste it into '''KTurtle's''' editor.
 +
|-
 +
||'''Run the program'''
 +
||Let's click on '''Run''' button to run the program.
 +
 
 +
A dialog box pop-up let's click OK.
 +
 
 +
Loop '''terminates''' after printing values from''' 29 to 20.'''
 
|-
 
|-
 
||
 
||
||With this we come to the end of this tutorial.
+
||With this we have come to the end of this tutorial.
let us summarise.
+
 
 +
Let us summarise.
 
|-
 
|-
 
||Slide Number 9
 
||Slide Number 9
Summary
+
 
||In this tutorial we have learnt, errors such as
+
'''Summary'''
Use of variable that has not been declared.
+
||In this tutorial we have learnt, errors and types of errors such as
Missing quotes in strings.
+
 
Errors related to print command
+
* Use of '''variable''' that has not been declared.
Runtime errors
+
 
 +
* Missing '''quotes''' in strings.
 +
 
 +
*''' Runtime errors''' and
 +
 
 +
* '''Logical errors'''
 
|-
 
|-
 
||Slide Number 10
 
||Slide Number 10
Assignment
+
'''Assignment'''
As an assignment I would like you to find errors in the given programs-
+
||As an assignment I would like you to find errors in the given programs
 
|-
 
|-
||Assignment 1
+
||'''Assignment 1'''
||reset  
+
||'''reset'''
$x=3  
+
 
$y=3  
+
'''$x=3'''
if $x==$y
+
 
print"they are equal"  
+
'''$y=3'''
else  
+
 
 +
'''if $x==$y'''
 +
 
 +
'''print"they are equal"'''
 +
 
 +
'''else'''
 
{  
 
{  
print "they are not equal"}  
+
 
 +
'''print "they are not equal"'''}  
 
|-  
 
|-  
||Assignment 2
+
||'''Assignment 2'''
||program to find the nth term and sum of n terms in GP
+
||'''program to find the nth term and sum of n terms in GP'''
reset  
+
 
$x=ask "enter value for x and click Ok"
+
'''reset'''
$y = ask "enter value for y and click Ok"
+
 
$g =sqrt ($x*$y)
+
'''$x=ask "enter value for x and click Ok"'''
print "Geometric mean is ="+
+
 
 +
'''$y = ask "enter value for y and click''' Ok"
 +
 
 +
'''$g =sqrt ($x*$y)'''
 +
 
 +
'''print "Geometric mean is ="+'''
 
|-
 
|-
||Assignment 3
+
||'''Assignment 3'''
||reset  
+
||'''reset'''
learn sum $x {  
+
 
{$r=0  
+
'''learn sum $x {'''
for $i= 1 to $x{  
+
 
return $r}  
+
'''{$r=0'''
}  
+
 
print   
+
'''for $i= 1 to $x{'''
 +
 
 +
'''return $r}'''
 +
 
 +
'''}'''
 +
 
 +
'''print'''  
 
|-
 
|-
 
||Slide number 11
 
||Slide number 11
Acknowledgement
+
 
||Watch the video available at  
+
'''Acknowledgement'''
 +
||Watch the video available at this URL
 
http://spoken-tutorial.org/What is a Spoken Tutorial  
 
http://spoken-tutorial.org/What is a Spoken Tutorial  
 
It summarises the Spoken Tutorial project  
 
It summarises the Spoken Tutorial project  
Line 362: Line 532:
  
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India  
 
It is supported by the National Mission on Education through ICT, MHRD, Government of India  
More information on this Mission is available at http://spoken-tutorial.org/NMEICT-Intro ]
+
More information on this Mission is available at this link
 +
http://spoken-tutorial.org/NMEICT-Intro ]
  
This is Madhuri Ganpathi from IIT Bombay
+
This is Madhuri Ganpathi from IIT Bombay signing off.
 
Thank you  for joining
 
Thank you  for joining
 
|-
 
|-
}
 

Latest revision as of 15:57, 18 December 2013

Title of the tutorial:

Author: Madhuri Ganapathi

Key words: Errors, syntax errors, run time errors, logical errors, compilation, clear command, Video tutorial.


Visual Cue Narration
Slide Number 1 Hello everybody.

Welcome to this tutorial on Common Errors in KTurtle.

Slide Number 2

Learning Objectives

In this tutorial, we will learn about
  • Syntax errors
  • Runtime errors and
  • Logical errors
Slide Number 3

System Requirement

To record this tutorial I am using,

Ubuntu Linux OS version 12.04.

KTurtle version. 0.8.1 beta.

Slide Number 4

Pre-requisites

We assume that you have basic working knowledge of KTurtle

If not,

for relevant tutorials, please visit our website.

http://spoken-tutorial.org

Slide Number 5

Definition of Error

Let's first define, What is an error ?

Error is a mistake in a program that produces an incorrect or unexpected result.

First I will explain about "Types of errors".
Slide Number 5

Syntax Errors

A syntax error is a violation of grammatical rules, of a programming language.

Compilation fails when a program has syntax errors.

Syntax errors are easy to find and fix.

For Example:

  • Unmatched parentheses, square brackets and curly braces.
  • Use of variable that has not been declared.
  • Missing quotes in strings.
Switch to KTurtle Application

Dash home >>In the Search bar type KTurtle

Click on the KTurtle icon.

Let's open a new KTurtle Application.

Click on Dash home. In the Search bar, type KTurtle.

Click on the KTurtle icon.

Let's begin the tutorial with some types of syntax errors.
I already have a program in a text editor.
Comment code To explain the error in the program, I will comment part of the code.
Highlight

$a=ask "enter any number and click Ok"

Here, I will comment the line

$a=ask within double quotes "enter any number and click Ok"

Type # I will use hash(#) sign to comment the line.
reset

#$a=ask "enter any number and click ok"

print $a^3'

spritehide

I will copy the program from text editor and paste it into Kturtle's Editor.

Pause the tutorial here and type the program your into KTurtle editor

Resume the tutorial after typing the program.

Run the program Let's click on Run button to run the program

Complier shows the following error,

variable "$a" was used without first being assigned to a value.

Highlight Line number 4 Here the error is in line number 4.
Syntax error This is a syntax error. It occured, as the variable 'a' was not declared.
Highlight $a=ask "enter any number and click Ok"

Remove the comment

So I will go to line number 2 remove the comment.
reset

$a=ask "enter a number and click ok"

print $a^3

spritehide

I will copy the program from text Editor and paste it into KTurtle's editor.
Run the program Let's click on Run button to run the program

Enter 6 for a value and click OK

Program runs without errors.

clear command I will clear the current program from KTurtle editor.

Type clear command and Run to clean the canvas.

let's next look about another error.
I already have a program in a text editor.
Highlight

$radian=(pi/180)*$degree

Here the value of "pi" is predefined in KTurtle.
Highlight $ Let's delete the "$" sign in the program.
reset

degree=ask "enter an angle value and click OK"

$radian=(pi/180)*$degree

go 50,50

print "Value in radians is"+ " " +$radian

spritehide

I will copy the program from text editor and paste it into Kturtle's Editor

Pause the tutorial and type the program your into KTurtle editor


Resume the tutorial after typing the program.

Run the program Let's Click on Run button to run the program.

Complier shows the following error,

you cannot put “=” here

Highlight Line number 2 This error is in line number 2.
Show error This is a syntax error it occured, as there is no container of variable.
Replace $ sign Let's go back to the program replace the $ sign.
reset

$degree=ask "enter an angle value and click OK"

$radian=(pi/180)*$degree

go 50,50

print "Value in radians is"+ " " +$radian

spritehide

I will copy the program from text editor and paste it into KTurtle's Editor
Run the program let's click on Run button to run the program.

Enter 45 for angle value and click OK

Program runs without errors.

Highlight "enter an angle value and click OK" Let's remove one of the quotes of the string.
reset

$degree=ask "enter an angle value and click OK

$radian=(pi/180)*$degree

go 50,50

print "Value in radians is"+ " " +$radian

spritehide

I will Copy the program from text editor and paste it into KTurtle's Editor
Run the program Click on Run button to run the program

Complier shows the following error,

Text string was not properly closed, expected a double quote “ ” to close the string.

Highlight Line number 2 Here the error is in line number 2.
Replace the quotes I will go to line number 2 and replace the quotes.
reset

$degree=ask "enter an angle value and click OK"

$radian=(pi/180)*$degree

go 50,50

print "Value in radians is"+ " " +$radian

spritehide

I will copy the program from text editor and paste it into Kturtle's Editor

Run the program Click on Run button to run the program

Enter 45 for angle value and click OK.

Program runs without errors.

This way you can find the line at which error has occured, and also correct it.

Let's now learn about runtime errors.
Slide Number 6

Runtime errors

Run-time error occurs during the execution of a program.

It may crash the program when you run it.

Runtime errors are commonly due to wrong input from the user.

Compiler cannnot find these errors.

For example:

  • Trying to divide by a variable that contains no value.
  • Run a loop without a terminating condition or increment value.
“clear” command' I will clear the current program from the KTurtle editor.

Type clear command and Run to clean the canvas.

Highlight the program I already have a program in a text editor.

This program divides two numbers.

'a' is dividend and 'r' is divisor.

reset

$a=ask "enter a number for 'a' and click Ok"'

$r = ask "enter a number for 'r' and click OK"

$d=$a/$r

print $d

I will copy the program from text editor and paste it into KTurtle's Editor.

Pause the tutorial and type the program into your KTurtle editor

Resume the tutorial after typing the program.

Run the program Let's click on Run button to run the program.

le'ts enter 5 for 'a' and click OK

enter 0 for 'r' and click OK

Here we get a runtime error ,

you tried to divide by zero

Highlight Line number 4 This error is in line number 4.
Run time error This error occurs as we cannot divide a number with zero.
Run the program Let's run the program again.

Enter 5 for number and click OK

enter 2 for 'r' and click OK

Program without errors.

“clear” command I will clear the current program from KTurtle editor.

Type clear command and Run to clean the canvas.

Next we will learn about logical errors.
Logical errors Logical error is a mistake in a program's source code that results in incorrect or unexpected behavior.

For example,

Assigning a value to the wrong variable.

Multiplying two numbers instead of adding.

I already have a program in a text editor.
reset

$x=30

message"In while loop, code inside the loop repeats till boolean

evaluates false"

while $x>20 {

$x=$x+1

fontsize 15

forward 20

print $x }

I will copy the program from text editor and paste it into Kturtle's Editor

Pause the tutorial here and type the program into your KTurtle editor

Resume the tutorial after typing the program.

Run the program Now click on the Run button to run the program.

A dialog box pops-up, let's click OK.

Loop goes into an infinite loop.

Highlight “ while” loop condition We see that “while” loop prints numbers from 31 and it is still printing.

This is a logical error.

In the “while” condition x is greater than 20,

but the variable x is always greater than 20

So, the loop never terminates.

Abort the process I will click on Abort button to abort the process.
Change $x=$x+1 to $x=$x-1 Let's change $x=$x+1 to $x=$x-1.
reset

$x=30

message"In while loop, code inside the loop repeats till boolean evaluates false"

while $x>20 {

'$x=$x-1'

fontsize 15

forward 20

print $x }

I will copy the program from text editor and paste it into KTurtle's editor.
Run the program Let's click on Run button to run the program.

A dialog box pop-up let's click OK.

Loop terminates after printing values from 29 to 20.

With this we have come to the end of this tutorial.

Let us summarise.

Slide Number 9

Summary

In this tutorial we have learnt, errors and types of errors such as
  • Use of variable that has not been declared.
  • Missing quotes in strings.
  • Runtime errors and
  • Logical errors
Slide Number 10

Assignment

As an assignment I would like you to find errors in the given programs
Assignment 1 reset

$x=3

$y=3

if $x==$y

print"they are equal"

else {

print "they are not equal"}

Assignment 2 program to find the nth term and sum of n terms in GP

reset

$x=ask "enter value for x and click Ok"

$y = ask "enter value for y and click Ok"

$g =sqrt ($x*$y)

print "Geometric mean is ="+

Assignment 3 reset

learn sum $x {

{$r=0

for $i= 1 to $x{

return $r}

}

print

Slide number 11

Acknowledgement

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

Slide Number 12

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

Slide number 13 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 ]

This is Madhuri Ganpathi from IIT Bombay signing off. Thank you for joining

Contributors and Content Editors

Arya Ratish, Madhurig