COMP 102 - McGill University

16
COMP 102: Computers and Computing Lecture 6: Introduction to Scripting Instructor: Joelle Pineau ([email protected]) Class web page: www.cs.mcgill.ca/~jpineau/comp102 Joelle Pineau 2 COMP-102: Computers and Computing Programming basics Difference between a programming language and a program. Understanding the need to be very precise when giving instructions to a computer. Notion of variables, naming conventions, variable types. Mathematical operators, comparison operators. Loops and functions (more on this today).

Transcript of COMP 102 - McGill University

Page 1: COMP 102 - McGill University

COMP 102: Computers and ComputingLecture 6: Introduction to Scripting

Instructor: Joelle Pineau ([email protected])

Class web page: www.cs.mcgill.ca/~jpineau/comp102

Joelle Pineau2COMP-102: Computers and Computing

Programming basics

• Difference between a programming language and a program.

• Understanding the need to be very precise when giving

instructions to a computer.

• Notion of variables, naming conventions, variable types.

• Mathematical operators, comparison operators.

• Loops and functions (more on this today).

Page 2: COMP 102 - McGill University

Joelle Pineau3COMP-102: Computers and Computing

Scratch

• Developed by the “Lifelong Kindergarten” group at MIT.– Homepage: http://scratch.mit.edu/

– For online help: http://info.scratch.mit.edu/Support

– To download: http://scratch.mit.edu/download

NOTE: You do not have to give out any of your personal info if you don’twant to. When you get to the registration screen, just click on the “Continueto Scratch Download” button at the bottom of the screen.

• Scratch allows users to write programs by dragging together blocks.

Joelle Pineau4COMP-102: Computers and Computing

Programming Window

Page 3: COMP 102 - McGill University

Joelle Pineau5COMP-102: Computers and Computing

Things: Performance area

• Stage (background)

• Sprites (objects)

– Scripts

(behaviors)

– Costumes

(appearance)

– Sounds

(available sounds)

Joelle Pineau6COMP-102: Computers and Computing

More things

• Run / Stop

• Script inventory

– Motion

– Looks

– Sound

– Pen

– Control

– Sensing

– Numbers

– Variables

Page 4: COMP 102 - McGill University

Joelle Pineau7COMP-102: Computers and Computing

Motion Inventory

Joelle Pineau8COMP-102: Computers and Computing

Shapes

Page 5: COMP 102 - McGill University

Joelle Pineau9COMP-102: Computers and Computing

Looks Inventory

Joelle Pineau10COMP-102: Computers and Computing

Sound Inventory

Page 6: COMP 102 - McGill University

Joelle Pineau11

Sensing Inventory

COMP-102: Computers and Computing

Joelle Pineau12COMP-102: Computers and Computing

Pen Inventory

Page 7: COMP 102 - McGill University

Joelle Pineau13COMP-102: Computers and Computing

Drawing Commands

• To build a program, drag components from the menus to the workspace.

– You can choose from any of the sub-menus (Motion, Pen, Control, …)

• To run your program, simply click on the code.

Joelle Pineau14

Resetting the Sprite

• To reset position of the Sprite, use commands from menu.

COMP-102: Computers and Computing

Page 8: COMP 102 - McGill University

Joelle Pineau15

Creating new Sprites

Joelle Pineau16

Creating new Sprites

Each Sprite has its ownprogram.

Click on the Sprite -------->to see its program board.

To delete a Sprite, right-clickon it, and choose “delete”.All its programs will also bedeleted.

Page 9: COMP 102 - McGill University

Joelle Pineau17

Saving your project

COMP-102: Computers and Computing

Joelle Pineau18COMP-102: Computers and Computing

Control Inventory

Page 10: COMP 102 - McGill University

Joelle Pineau19COMP-102: Computers and Computing

Creating a new command: “square”

Joelle Pineau20COMP-102: Computers and Computing

Calling this command

Page 11: COMP 102 - McGill University

Joelle Pineau21COMP-102: Computers and Computing

Use your command!

Joelle Pineau22

• Declare a variable called length.

• Assign is a value of 100.

• Access this variable whenever you move.

COMP-102: Computers and Computing

Using variables

Page 12: COMP 102 - McGill University

Joelle Pineau23

Variables Inventory

COMP-102: Computers and Computing

Joelle Pineau24COMP-102: Computers and Computing

Variable Types in Scratch

• Don’t need to specify variable type in Scratch.

– Decision by designers of the language, to keep things simple.

• How can the machine interpret variable (e.g. know how many

bytes to read)?

– Type of variable is completely determined by the context.

• Why don’t other languages do this?

Page 13: COMP 102 - McGill University

Joelle Pineau25

Loops

• Repeat the move command 4 times.

• Same output. Shorter program.

COMP-102: Computers and Computing

Joelle Pineau26COMP-102: Computers and Computing

Infinite looping

To stop, hit the Stop button.

Page 14: COMP 102 - McGill University

Joelle Pineau27

Operators Inventory

COMP-102: Computers and Computing

Joelle Pineau28COMP-102: Computers and Computing

Conditionals

• Denoted by if keyword.

• Block of code inside the conditional

executes only if condition is true.

• Condition expressed as a logical

expression: number < 10

• Condition depends on the changing

value of the number variable.

Page 15: COMP 102 - McGill University

Joelle Pineau29COMP-102: Computers and Computing

Kinds of Loops

• For loop:

– Repeat a fixed number of times.

– Flexible

• While loop (called “repeat until” in Scratch):

– Repeat while a (boolean) condition is true.

– More Flexible

Joelle Pineau30COMP-102: Computers and Computing

Unrolling, With Conditions

Page 16: COMP 102 - McGill University

Joelle Pineau31COMP-102: Computers and Computing

Recursion

Idea: A function that repeats itself in a self-similar way.

First try: Try again!

(infinite recursion) (finite recursion)

Joelle Pineau32COMP-102: Computers and Computing

Take-home message

• Have fun with Scratch!