pp ceebot 1 intro.ppt - compatibility mode ceebot 1 introdu… · %uldq :dug&hherw ,qwurgxfwlrq...

58
Brian Ward Ceebot 1 : Introduction to Ceebot Slide 1 Unit 1 Introduction to Ceebot Programming Principles

Upload: others

Post on 14-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 1

Unit 1

Introductionto

Ceebot

Programming Principles

Page 2: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 2

What is Ceebot?

Page 3: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 3

An Animated Virtual World of Robots

Page 4: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 4

A Programming Environment

Page 5: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 5

A Programming Language

move(20);

turn(90);

fire(1);

message("Program Completed");

if (count < 10){

turn(angle);wait(0.5);drop();

}

Using C#/C++/Java syntax

grab();

while (i <= 5){

GetItem(); i++;

}

Page 6: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 6

How to Use

Ceebot

Page 7: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 7

CeeBot Start Screen

Page 8: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 8

The Main CeeBot Menu

Page 9: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 9

The introductory video can be skipped by pressing the [esc] key

Introductory Video

Page 10: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 10

Page 11: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 11

Page 12: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 12

The CeeBot Editor/Compiler

fire(1);

Page 13: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 13

Running the program

Danger!low robot

shield level

Page 14: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 14

Result: Success or Failure?

Page 15: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 15

Using Ceebot

to write

larger programs

Page 16: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 16

Another CeeBot Scenario

Transport a Titanium cube to the platform

Select the first program slot .. and

then the editor

Page 17: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 17

Use the Editor

grab(); move(20);drop();

// Author: B Ward. ID: 23415// Date: 18 Dec 2010

programcomments

click here tocompile the

program(or click OK)

click here tocompile the

program(or click OK)

also known as source codealso known as source code

Page 18: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 18

The compiler

will report syntax errors

The compiler

will report syntax errors

move(20)

Page 19: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 19

Running the program

Run(execute)

the program

Run(execute)

the program

Page 20: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 20

Some Useful Instructions

move(…);

turn(…);

fire(…);

message(…);

drop();

grab();

wait(…);

pendown();

red();

Put them in the right order and use the

correct parameters to create your program.===============

each instruction ends

with a semicolon ;

Put them in the right order and use the

correct parameters to create your program.===============

each instruction ends

with a semicolon ;

Page 21: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 21

AlgorithmsA plan for the program using

english-like statements

Page 22: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 22

Draw a Square

Page 23: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 23

Algorithm … then CodeThe square is orange and 20 metres in size

Program Codeextern void object::Task2_1(){

}

orange();pendown();move(20);turn(90);move(20);turn(90);move(20);turn(90);move(20);turn(90);penup();

Algorithm

1. Choose orange colour2. put pen down3. move 20 metres forward4. turn 90 degrees anti clockwise5. move 20 metres forward6. turn 90 degrees anti clockwise7. move 20 metres forward8. turn 90 degrees anti clockwise9. move 20 metres forward10. turn 90 degrees anti clockwise11. raise pen

the steps are numbered in the

order of execution

the steps are numbered in the

order of execution // Author: BWard. ID:156874 // Course: BSc Comp // Date: 18/12/2010

Page 24: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 24

Variation

Page 25: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 25

Draw a different square

Make left and right sides blue, top and bottom sides redAlgorithm1.2. put pen down3. move 20 metres forward4. turn 90 degrees anti clockwise5.6. move 20 metres forward7. turn 90 degrees anti clockwise8.9. move 20 metres forward10. turn 90 degrees anti clockwise11.12. move 20 metres forward13. turn 90 degrees anti clockwise14. raise pen

Program Code

extern void object::Task2_1()

{

}

red();pendown();move(20);turn(90);blue();move(20);turn(90);red();move(20);turn(90);blue();move(20);turn(90);penup();

choose red colour

choose blue colour

choose red colour

choose blue colour

Page 26: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 26

Variableshow to store information temporarily in a program

Page 27: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 27

Draw a Square using variables

We shall use variables for:

• length of side• angle turned

We shall use variables for:

• length of side• angle turned

Page 28: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 28

float length; // declare a float variable called lengthfloat angle; // declare a float variable called anglelength = 20; // store 20 in lengthangle = 90; // store 90 in angle

The Code to Draw a Square

Task2_1()

{

}

move(length); // move using length variableturn(angle); // turn using angle variableetc.

move( );length // move using length variableturn( );angle // turn using angle variable

// variation of Square program using variables

orange(); // set drawing colourpendown(); // prepare to draw

Page 29: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 29

Using a robot to do maths!

using variables

Page 30: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 30

Using a robot to do maths

{

}

Task5_3()

float a, b; // declare 2 variables a and b

a = 5; // store 5 in a

a = a + 2; // add 2 to a and store again

b = a*2 - 10; // multiply by 2 and subtract 10

b = b/2; // divide by 2

a = b + a + 3; // add b, a, 3

message("The answer is " + a); // display result

New instruction for screen output display

(see next week)

New instruction for screen output display

(see next week)

Page 31: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 31

Result

output produced by message(…)

instruction

output produced by message(…)

instruction

Page 32: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 32

Page 33: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 33

Page 34: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 34

Push [F1] to get instructions

Page 35: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 35

What is a computer program

???

Page 36: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 36

A Program is:

• A set of instructions to the computer

• To make the computer do something useful

• Designed by programmers• Written in a language like Ceebot,

C++, Java• Other high-level languages could

be used .. e.g. Cobol, Fortran, Pascal, Basic, etc, etc.

Page 37: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 37

Programming Languages

Pascal C

C++

Java CobolBasic

Ada

Prolog

Fortran Lisp

C#

Modula-2

Occam

JavaAlgol

APL

BCPL

COMAL

Eiffel

Forth

Logo

Perl

PL/I

Python

RPG

Simula Smalltalk

Ceebot

Visual Basic

SnobolCecil

Page 38: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 38

UniversalTranslator

Star Trek Talk

How do StarTrek officers understand aliens?

Jaggh kapraplaakh ???Prepare for

disembowelling

Page 39: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 39

Computer Translation

How do computers understand our instructions?

message("Hello");

Compiler

Page 40: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 40

Sequences

Page 41: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 41

How a Sequence of instructions is Programmed

{

}

move(20);

grab();

turn(-90);

move(10);

drop();

a sequence blockis enclosed in

curly brackets { }

instructions are all in lowercase and end

with a semicolon ;

The instructions are indented (using tab key or spaces) and placed in the order in which they are to be executed (from top to bottom)

Page 42: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 42

A typical sequence

{

grab();

move(20);

drop();

}

Page 43: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 43

Sequence

The sequence is :• a block of instructions .. one after

the other• with no deviation or repetition

The order of the instructions in a sequence is very important ………if the order is changed, so is the logic of the program

The sequence is a basic construct of all programming languages

move(20);

grab();

turn(-90);

drop();

etc. etc.

Page 44: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 44

Why use algorithms?

Algorithms can be used to designprograms before coding starts

This is especially important when writing larger and more complicated programs

We use algorithms from the start, so you learn Good Practice

Page 45: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 45

Programcomments

Page 46: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 46

Good Practice

Use plenty of comments in all your programsExample Uses

extern void object::Task0_7()

{

}

/* Author: B WardDate: 14/08/05 Task: install power cell */

grab(); // pick up objectturn(-90); // turn clockwisewait(0.5); // pause for 0.5 secdrop(); // drop object

// this is a one line comment // the compiler ignores this line

/* this is a multiple-line comment and the compiler will ignore everything here */

Use comments to:• identify author and program• explain trickier parts

Use comments to:• identify author and program• explain trickier parts

Page 47: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 47

Variableshow to store information temporarily in a program

Page 48: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 48

What is a Variable?

1. A storage area in the computer memory

2. Can store information for use later in the program

3. A variable can be set up to store different types of data: numbers, words, etc.

3. The contents may change as the program runs (hence the name: variable)

4. Variables need to be given unique names

5. A variable name is also known as an identifier

Page 49: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 49

Declaring Variables

Before you can use a variable, you must declare it

This means giving the variable

a type and a name

string firstname; int count; float number ;

type name

Page 50: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 50

identifiers (variable names)

Rules for identifiers

1. Name must start with a letter

2. No spaces in the name

3. Can only have letters, digits, underscore

4. No reserved words (move, turn, etc.)

5. Length, length and LENGTH are all different variables (i.e. Ceebot is case-sensitive)

6. Good Practice: always choose names that are meaningful

Name OK or not?My_Namemy-name 1stname D2Number4%costfirst name

Page 51: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 51

Data Types for Ceebot Variables

intCan store whole numbers e.g.3 0 -261 46 -7

floatCan store numbers with decimal places e.g.10.67 -0.05 13.0 176.4

objectCan store details of an objecte.g. Titanium, PowerCell

pointCan hold position coordinates

stringCan store text(strings of characters) e.g."High Wycombe" "Brian"

Each type needs a different amount of

storage space

booleanCan only be true or false

There are 5 main data types for variables

Page 52: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 52

Assignmenthow to put values

into variables

Page 53: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 53

Store values in a variable

number

float number ; // declare variable

number = 10.50 ; // store 10.50

number = 25.00 ; // store 25.00

number

Page 54: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 54

Assignments

Information can be stored in a variable using:

the assignment statement and assignment operator (=)

e.g:

Computer Memory

Variable Contents

age

choice

wage

name

title

25

15.50A

Brian Ward

Menu List

age = 25 ;

wage = 15.50 ;

name = "Brian Ward" ;

title = "Menu List" ;

choice = "A" ;

Page 55: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 55

Initialising Variables

Variables can be given an initial value at the same time as they are declared

e.g.

int count = 0 ;

float price = 7.54 ;

string name = "Joe Smith" ;

Page 56: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 56

Instructionsand

parameters

Page 57: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 57

Which of these instructions have parameters?

move(20);

turn(90);

fire(1);

wait(0.5);

red();

Note: most instructions have brackets, but not all use them

grab();

pendown(); drop();

Page 58: PP Ceebot 1 Intro.ppt - Compatibility Mode Ceebot 1 Introdu… · %uldq :dug&hherw ,qwurgxfwlrq wr &hherw6olgh 7kh lqwurgxfwru\ ylghr fdq eh vnlsshg e\ suhvvlqj wkh >hvf@ nh\,qwurgxfwru\

Brian Ward Ceebot 1 : Introduction to Ceebot Slide 58

Parameters

move( ) ;

Brackets ( ) act like a doorway into the instruction

15

15

The parameter is used to complete the instruction: move(15);

the parameter is a value passed

in

the parameter is a value passed

in

in this case, 15 is used

in this case, 15 is used