B.A. (Mahayana Studies) 000-209 Introduction to Computer Science November 2005 - March 2006 13. Logo (Part 2) More complex procedures using parameters,

Slides:



Advertisements
Similar presentations
First of all – lets look at the windows you are going to use. At the top you have a toolbar, with all your various tools you can use when customising your.
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
ABNIAC The following slide presentation is to acquaint the student with ABNIAC. The version used for presentation is the Java version, which can be found.
Microsoft Word 2010 Lesson 1: Introduction to Word.
Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Computer Science 1620 Loops.
CSC Intro. to Computing Lecture 17: Even More Robotran!
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Logo Lesson 4 TBE Fall 2004 Farah Fisher. Prerequisites Create basic and complex shapes using Logo procedures Create Logo procedures that use variables.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Logo Lesson 3 TBE 540 Fall 2004 Farah Fisher. Prerequisites for Lesson 3 Before beginning this lesson, the student must be able to… Use simple Logo commands.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Logo Lesson 5 TBE Fall 2004 Farah Fisher. Prerequisites  Given a shape, use basic Logo commands and/or a procedure to draw the shape, with and.
The If/Else Statement, Boolean Flags, and Menus Page 180
Logo Lesson 2 Logo Procedures
CIS101 Introduction to Computing Week 12 Spring 2004.
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 1) An introduction to Logo: drawing, moving,
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Mini Project II – Drawing Machine
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Week 11 DO NOW QUESTIONS. An ask turtles block is a set of instructions that is issued to every turtle. Even though computers can do things very quickly,
LOGO SOFTWARE BY: SAVE 9S. INTRODUCTION Logo is a software that can be found at : Shared area> High School > ICT > take home software > LOGO32. This is.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Recursion, Complexity, and Sorting By Andrew Zeng.
1 Logo -- A Language for Learning Liping Cai 11/21/2005.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Scratch Programming Lesson 2 First glance to programming logic.
A SIMPLE COMPUTER LANGUAGE LOGO. LOGO Introduction Logo is the simplest programming language. It.
MSW Logo By Awin 9s.
Logo For beginners By Dali Matthews 9S What is logo?
An introduction to Logo Mike Warriner Engineering Director, Google Note: This course is not an endorsement of Logo by Google. All views in this document.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CONTROL SYSTEMS Control Systems A command is a directive that performs a specific task An argument is a variable that can be used by the function reveiving.
The Hare Raising Experience of Logo in the Classroom
Mathematical Expressions, Conditional Statements, Control Structures
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Algorithms Writing instructions in the order they should execute.
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Balancing the scales: Inequalities By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Copyright 2002, Tony Gauvin, UMFK
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
24 Background Building 25 Computing Terminology, and Speed/Velocity Module 4 Notes: Sensing, Or Operator, Conditional Logic 28 Module 4 Algorithms,
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Getting started with the turtle Find the latest version of this document at
Logo for Beginners By Chris 9S.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Search for it on your computer
LOGO WHAT IS IT? HOW TO USE IT AND HOW USEFUL CAN IT BE?
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Computer Programming.
LOGO BY Kaotip 9S.
LOGO 32 By: Xenon 9S.
Coding Concepts (Sub- Programs)
Looping Topic 4.
Logical Operations In Matlab.
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your.
The structure of programming
Presentation transcript:

B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 2) More complex procedures using parameters, arguments, and recursion. Local variables and conditional statements.

Intro to CS. 13/logo2 2 Overview 1. Shorter Commands 2. Parameters 3. Rectangle 4. Fun with Squares 5. Fun with Triangles 6. Printing continued

Intro to CS. 13/logo Local Variables 8. Procedures that Output 9. Conditional Statements 10. Stopping Recursive Calls 11. Fractals 12. More Logo Commands

Intro to CS. 13/logo Shorter Commands Most commands have 2-letter short forms. Some of them:  FD = FORWARDBK = BACK  RT = RIGHTLT = LEFT  PU = PENUPPD = PENDOWN I'll start using the short forms from now, since it means less typing.

Intro to CS. 13/logo Parameters The numbers following command names are called parameters. Examples:  fd 50  setpencolor 15  setpos [ 50 25] parameters

Intro to CS. 13/logo2 6 Parameters and Procedures Parameters can be passed into a procedure and used by its commands. The parameters are stored in arguments. Example:  to sized_forward :size forward :size end command using the argument argument holding a parameter

Intro to CS. 13/logo2 7 An Argument is a Memory Box An argument is a memory box inside the procedure. Any commands inside the procedure can use the memory box value. sized_forward :size forward :size value the name of the box use :size's value continued

Intro to CS. 13/logo2 8 A memory box is a location in RAM. It has a name and may contain a value A command accesses the box's value by using the box's name. The memory box only exists until the end of the procedure  when the procedure finishes, the memory box is deleted

Intro to CS. 13/logo2 9 Using sized_forward Two calls to the sized_forward procedure with different parameters.

Intro to CS. 13/logo2 10 Parameters and Memory Boxes A procedure's parameter is copied into the memory box for the argument  the parameter becomes the box's value sized_forward :size forward :size sized_forward use :size's value, 100

Intro to CS. 13/logo2 11 Many Arguments Procedures can have many arguments. Draw any size of rectangle:  to rectangle :width :height fd :height right 90 fd :width right 90 fd :height right 90 fd :width end two arguments, for the width and height of the rectangle rectangle :width code using box values :height

Intro to CS. 13/logo Rectangle

Intro to CS. 13/logo2 13 Rectangles Parameters For the first call to rectangle: For the second call to rectangle: rectangle :width code using box values :height rectangle rectangle :width code using box values :height rectangle

Intro to CS. 13/logo2 14 rectangle is called twice At the end of the first call to rectangle, its width and height memory boxes (arguments) are deleted. They are newly created again when rectangle is called the second time. continued

Intro to CS. 13/logo Fun with Squares A square procedure that has a size argument:

Intro to CS. 13/logo2 16 A Squares4 Procedure

Intro to CS. 13/logo2 17 Procedure Calls squares4 :size square :size square :size + 20 square :size + 40 square :size + 60 squares square :size repeat command 70 There are two :size parameters: one in squares4, the other in square. square is called 4 times

Intro to CS. 13/logo2 18 A Tables Procedure

Intro to CS. 13/logo2 19 A Mirror Procedure

Intro to CS. 13/logo2 20 A Mirrors Procedure

Intro to CS. 13/logo2 21 Procedure Calls mirrorsmirror calls tables squares4 :size square :size

Intro to CS. 13/logo Fun with Triangles

Intro to CS. 13/logo2 23 A Hexagon Procedure

Intro to CS. 13/logo2 24 Procedure Calls hexagon :sz repeat 6 [ triangle :sz rt 60 ] hexagon triangle :size repeat command 90 triangle is called 6 times

Intro to CS. 13/logo2 25 A Spider Web Procedure (1)

Intro to CS. 13/logo2 26 Procedure Calls hexagon :sz repeat 6 [ triangle :sz rt 60 ] spiderWeb triangle :size repeat command 70 spiderWeb :sz hexagon :sz hexagon :sz+20 hexagon :sz+40 hexagon is called 3 times

Intro to CS. 13/logo2 27 A Spider Web Procedure (2)

Intro to CS. 13/logo2 28 spiderWeb :sz hexagon :sz hexagon :sz + :step hexagon :sz + (:step*2) :step spiderWeb Procedure Call

Intro to CS. 13/logo2 29 Too Many Spider Webs a problem continued

Intro to CS. 13/logo2 30 I had to press the "Halt" button to stop the execution  spiderWebR will never stop on it's own The spiderWebR procedure is recursive  it calls itself Recursion is a great technique, but I've used it incorrectly  I didn't include code in spiderWebR to tell it how to stop

Intro to CS. 13/logo2 31 Recursion in Picture Form spiderWebR 20 spiderWebR 40 draws hexagon and calls spiderWebR 60 draws hexagon and calls spiderWebR 80 draws hexagon and calls spiderWebR 100 draws hexagon and calls forever...

Intro to CS. 13/logo Printing print  print the number or text in the command window.  e.g. print 60 print 60 in the command window A word must start with a quotation mark  e.g. print "Andrew  print Andrew in the command window continued

Intro to CS. 13/logo2 33 For several words, put them in square brackets  e.g. print [My name is Andrew]

Intro to CS. 13/logo Local Variables A local variable is a memory box inside a procedure for storing numbers (or text). Each variable has a name :foo90 namevalue a variable A local variable is just like a procedure argument, but can be created anywhere inside the procedure.

Intro to CS. 13/logo2 35 Creating a Local Variable You create a local variable by giving it a name and a value:  to proc localmake "foo 90 : end This creates a local variable called :foo, with the value 90, inside the procedure proc proc :foo90

Intro to CS. 13/logo2 36 Using a Local Variable A variable must be created before it can be used.

Intro to CS. 13/logo2 37 Procedure Call :x:y add2 :z add localmake "z :x + :y 713

Intro to CS. 13/logo Procedures that Output A procedure can output (return) an answer to the procedure that called it by using the output command.

Intro to CS. 13/logo2 39 Procedure Calls :x :x + :y :y adder :sum :a:b adder3 :c :ab adder output :sum localmake "ab (adder :a :b) 6 72

Intro to CS. 13/logo Conditional Statements A conditional statement is a command (or commands) which is only carried out if a test succeeds. Examples:  if (it is raining) then put up your umbrella  if (7 > 5) then print a "bigger" message The test part (e.g. 7 > 5) is called a condition. The part after the "then" is called the then part.

Intro to CS. 13/logo2 41 Conditional Statements if ( ) [ ] Example:

Intro to CS. 13/logo2 42 The Else Part You often want to do something if the test fails. Examples:  if (it is raining) then put up your umbrella else put on your sunglasses  if (:foo > :bar) then print a "bigger" message else print a "smaller" message The part after the "else" is called the else part. It's carried out if the test fails.

Intro to CS. 13/logo2 43 More Conditionals ifelse ( ) [ ] [ ]  the second […] is the else part Example  ifelse ( :foo > :bar ) [ print [foo is bigger] ] [ print [foo is not bigger] ] The conditional must be on one line. continued

Intro to CS. 13/logo2 44

Intro to CS. 13/logo2 45 Better Formatting with ~ A long Logo line can be split across multiple lines by using ~'s  this is useful for conditional statements The previous example with ~'s:  ifelse ( :foo > :bar ) ~ [ print [foo is bigger] ] ~ [ print [foo is not bigger] ] Indent the two parts of the ifelse.

Intro to CS. 13/logo2 46 'If' Questions 1. Write a procedure that prints a message if two numbers are equal. 2.Write a procedure that prints "true" if the first number is less than the second, and "false" otherwise. 3.Write a procedure that prints one message if the first number is greater than the second, and a different message if it is not.

Intro to CS. 13/logo2 47 Answers  to equal :num1 :num2 if ( :num1 = :num2 ) ~ [ print [the numbers were equal] ] end  to lessThan :a :b ifelse ( :a < :b ) ~ [ print "true ] ~ [ print "false ] end continued

Intro to CS. 13/logo2 48 Use (print …) to print arguments

Intro to CS. 13/logo Stopping Recursive Calls The problem with my first version of spiderWebR was that it couldn't stop. This can be fixed by putting the recusive call into a conditional statement.

Intro to CS. 13/logo2 50 Lots of Spider Webs

Intro to CS. 13/logo2 51 Recursion in Picture Form spiderWebR 130 spiderWebR 150 draws hexagon and calls spiderWebR 170 draws hexagon and calls spiderWebR 190 draws hexagon and calls spiderWebR 210 draws hexagon and calls STOPS since 210 > 200

Intro to CS. 13/logo2 52 Amazing

Intro to CS. 13/logo2 53 Recursion in Picture Form maze 5 maze 10 draws line, rotates and calls maze 15 maze 200 STOPS since 200 = 200 draws line, rotates and calls many more maze calls...

Intro to CS. 13/logo2 54 Polygons

Intro to CS. 13/logo2 55 Recursion in Picture Form polygon polygon draws line, rotates and calls polygon polygon STOPS since 201 > 200 draws line, rotates and calls many more polygon calls...

Intro to CS. 13/logo2 56 Other Polygons Try these:  polygon polygon polygon polygon Try to create these with the polygon procedure.

Intro to CS. 13/logo Fractals Fractals are curves created using recursion. Complex looking 2D and 3D shapes can be generated using very small (and fast) code. Fractals have been used in many computer- generated movies (e.g. Shrek, Toy Story) to create realistic looking backgrounds  trees, clouds, water, fire, etc.

Intro to CS. 13/logo2 58 The C Curve Now try cCurve 2 15  make sure to reset and zoom out

Intro to CS. 13/logo2 59 Procedure Calls c 5 10 c 5 8 r l c 5 9 r l c 5 8 r l c 5 7 r l r l r l r l many, many more calls, until level = 0 c = cCurve r = right 90 l = left 90

Intro to CS. 13/logo2 60 The Dragon Now try dragon 4 9  make sure to reset first

Intro to CS. 13/logo2 61 Procedure Calls ld ld 10 8 l rd 10 8 ld 10 9 l rd 10 9 ld 10 8 r rd 10 8 ld 10 7 l rd 10 7ld 10 7 r rd 10 7ld 10 7 l rd 10 7 r many, many more calls, until level = 0 d = dragon ld = leftDragon rd = rightDragon r = right 90 l = left 90 d 10 10

Intro to CS. 13/logo More Logo Commands setpensize [ ]  sets the width and height of the “pen” used by the turtle  e.g. setpensize [10 10] MSW Logo only uses the width value continued

Intro to CS. 13/logo2 63 setscreencolor  changes the colour of the display  e.g. setscreencolor 14 setpos [ ]  moves the turtle to the (X,Y) coordinate in the display  e.g. setpos [ ]

Intro to CS. 13/logo2 64 Example

Intro to CS. 13/logo2 65 Random random  makes a random number between 0 and number  e.g. random 50 makes a random number between 0 and 50 You can use random in many ways:  setpencolor random 20  setscreencolor random 10  fd random 300