31
Algorithms, Programs and Scripts The Building Blocks: Control Constructs Functions and Objects SCDB 2002 (revised 2003,2004,2005,2007,2008)

Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Algorithms, Programs and Scripts

The Building Blocks:

Control Constructs

Functions

and Objects

SCDB 2002 (revised 2003,2004,2005,2007,2008)

Page 2: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Algorithms

The word algorithm is derived from a way of carrying out

calculations which was invented by the mathematician Al-

Kwarizmi.

It refers to a process of achieving a task (originally mathematical)

by following an ordered sequence of steps.

Cooking recipes and directions how to get from one place to

another may be thought of as algorithms.

An algorithm is an ordered sequence of steps used to achieve a task

Page 3: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Programs and Scripts

A computer program is an ordered sequences of instructions (an

algorithm) expressed in a computer programming language. A

script is also an ordered sequence of instructions.

A script, however, is not compiled into a separate executable file

but is interpreted each time that it is run.

In time you can learn how to write your own algorithms and

scripts ‘from scratch’ but the best way to learn to write scripts or

programs and devise algorithms is to study and copy what others

have already done and try to do similar things yourself.

It is usually possible to modify and combine existing algorithms

or programs to suit your needs.

Programs are compiled into separate ‘executables’ Scripts are not.

Page 4: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Learning a new language

Learning scripting or programming is very like learning to use a

natural language.

When learning a foreign language, we usually copy examples to

start with, then as we gain confidence we begin to say new things

based on what we have been taught.

It would be unusual to deliberately work out a totally new way of

introducing yourself*, or asking for directions.

Just as we first copy and then continue to learn new things to say

and new ways of saying them in natural languages, so it is with

programming and scripting.

* Though of course we may do it by accident

Learning to program or script is like learning a foreign language

Page 5: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Control Constructs

Natural languages are used for all sorts of purposes, but

programming and scripting languages are designed for writing

instructions that can be followed by a computer system.

Scripts may simply be long sequences of instructions. There are,

however, things that we can ask computers to do ( like only follow some

instructions in a certain circumstance, or to repeat a sequence of

instructions) that enable us to access and exploit those aspects of

computers that make them special. We can also make our instructions

more sophisticated and communicate more efficiently. We need a special

way of organising instructions to do this and to do it we use control

constructs. Specific examples will be introduced later.

They called this because they control the order in which a computer will

execute instructions* in scripts and computer programs.

* Sometimes called ‘flow of control’, or ‘control flow’

Control Constructs are used to access the special capabilities of computers

Page 6: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Instructions

In Python, an instruction is a single executable statement. An

analog in natural language would be a sentence. For example,

a = 100.56

is an instruction which effectively says:

The program is going to need some space in memory to store

some data. I will refer to that location by the name ‘a’ and initially I

want you to store the value 100.56 at that location.

Instructions in scripting languages tend to be fairly ‘powerful’,

that is they say a lot with only a few symbols. This is because it

would be very laborious to have to repeat the sentence above

every time you wanted to declare a variable.

Instructions are like sentences

Page 7: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Variables

The instruction

a = 250

Is a declaration not only that some memory is needed to store

something, but also that the contents of that location in memory

can be changed when the program is running (executing) -

different values may be stored in it.

This sort of data item is called a variable.

In many programming languages you have to say exactly what

‘type’ of data is to be stored in a variable, e.g. an integer or

character. Python uses an approach where we do not need to

stipulate whether we are going to store an integer, character or

whatever. The interpreter will work that out for itself when our

script actually gives it some data to store.

Declaring a variable

Page 8: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

An example sequence of instructions

The following lines of Python instruct the computer to:

1) find space to store three data items in memory and name the

locations of these spaces a and b.

2)Then save values in two of these locations.

3)Then add the values in a and b and store the result at location c.

a = 10

b = 35

c = a + b

Each line is a statement or instruction.

Calculating with variables

Page 9: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Functions

c = a + b

Is a simple instruction.

Instructions may be grouped together and given a name as a

function. Once a function has been defined, we just have to use

its name (‘call’ it) to get the computer to follow its instructions.

This approach is very useful as it means we can build more

complex scripts and programs out of simpler components.

Most languages come with libraries of pre-defined functions for

common tasks like maths, trigonometry and manipulating text.

Functions are a bit like clichés in natural languages.

Instructions may be grouped together and re-used

Page 10: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Object Oriented Programming – an approach to look at later

OOP –Object Oriented Programming/Paradigm

In this approach to programming, declarations of data and

subprograms are grouped together in logical modules (called

objects) to make it easier to develop complex programs.

There are many advantages and efficiencies that can be achieved

using OOP.

Page 11: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Summary of part 1

Scripts are ordered sequences of instructions, written in a language

designed for giving instructions to a computer system.

Learning a programming language is like learning a foreign

language, first we imitate then we start getting creative.

Instructions can be grouped together and named so we can build

complex scripts or programs out of simpler components:

subprograms and functions.

In the Object Oriented approach, data and subprograms are grouped

together for a modular approach to program design.

Next we look at control constructs

Summary of ideas introduced so far

Page 12: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

In the diagrams that follow, each grey bar,

Control Constructs

represents a simple instruction (usually one line)

or a

compound instruction (several statements grouped together).

It may also represent a function or subprogram call that can be

executed by the computer.

Page 13: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Control constructs govern the sequence of execution of

instructions in computer programs.

Although the exact syntax used will vary in different languages,

the constructs explained in this series of slides can be found in

most procedural programming languages and object oriented

programming languages.

Control Constructs

Page 14: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

The following slides describe 7 control constructs

1) The basic simple sequence of instructions

2) The IF conditional construct

3) The IF ELSE conditional construct

4) The WHILE loop repetition construct

5) The REPEAT UNTIL repetition constructs

6) The FOR loop repetition construct

7) The multiple branching conditional construct

There is then a brief introduction to ways of organising scripts

using Functions and Objects

A list of ways of controlling and organising scripts

Page 15: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

1 - a sequence of instructions

Execute

these

instructions

1 - A sequence of instructions

follow the first instruction,

then follow the next, then the

next and so on.

It is simply a list of

instructions with one following

another.

Page 16: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Conditional Constructs

The next set of constructs are used to tell the computer to make

decisions.

One of the most powerful things that computers can do is make

decisions. If we can describe a decision that we would like the

computer to make, and what sort of decision it should make given

certain circumstances, we can delegate decision-making to the

machine.

This is one of the characteristics of computer technology that

leads to most disputes. It is often said that computers don’t make

mistakes, programmers do – the programmer’s mistake is often

to get the test in a conditional construct wrong, accidentally

programming the computer to make an inappropriate decision.

Conditional constructs enable us delegate decisions to computers

Page 17: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

2 - IF conditional construct

2 - IF conditional construct

The “condition” may be

anything that can be checked

or tested by the computer.

If the condition is not true, the

instructions are not executed.

Execution continues with the

instruction that immediately

follows the if construct.

Execute these

instructions

IF a condition

is true

Page 18: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

3 - IF ELSE conditional construct

3 - IF ELSE conditional

construct

Here the computer has two

choices. It will only execute

one of them.

It will execute either one or the

other set of instructions

depending on whether

“condition” is true or false.

Execute these

instructions

IF condition

is true

ELSE Execute

these

instructions

Page 19: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

4 - multiple branching conditional construct

4 – MULTIPLE BRANCHING

conditional construct

This construct extends the

number of options to select

from. Only one option may be

chosen.

Usually the first condition

that tests true is followed.

Often referred to as a case

structure as there are several

cases to select from;

“in case x1 do this , in case

x2 do this, in case xn …”

and so on.

Execute these

instructions IF

x1 is true

Execute these

instructions IF

x2 is true

Execute these

instructions IF

xn is true

Page 20: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Repetition Constructs

The next set of constructs are those that deal with repetition.

Each repetition is called an iteration so loops are sometimes

called iterations or iterative loops.

One of the things that has to be possible in any repetition

construct is to make sure that at some point it is exited, otherwise

you may get an infinite loop – the repetition may continue for

ever.

So every loop will have a condition that is tested to determine

whether the instructions should be repeated or the loop should

be ended. This conditional test may be to see whether a variable

has reached a certain value, or whether the user has pressed a

certain key, conditional statements will be explained in a

differents et of slides.

Repetition constructs need a way of finishing or they keep repeating for ever

Page 21: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

5 - while loop repetition construct

Repeatedly

execute these

instructions

WHILE a given

condition is true

5 - WHILE loop repetition

construct

The computer will follow the

instructions over and over

again as long as the condition

being tested is true: each time

that the instructions are

executed it will check to see if

the condition has changed; if it

is still true it will repeat the

instructions, if not it will move

on to the instruction that

follows the loop construct

Page 22: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

6 - repeat until loop repetition construct

Repeatedly

execute these

instructions

UNTIL a given

condition is true

6 – REPEAT UNTIL loop

repetition construct

Each time the instructions

have been followed the

computer checks whether the

condition has changed; if it is

still false it follows the

instructions again; if the

condition is now true the loop

is terminated.

Similar to the while loop

except that the instructions are

only executed if the condition

tested for is false.

Page 23: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

7 - for loop repetition construct

Repeatedly

execute these

instructions for

a given number of

times

7 - FOR loop repetition

construct

Instructions will be executed a

number of times

predetermined at the time the

loop is entered. Somehow the

computer has to be able to

count how many times it has

executed the instructions.

The number of times the loop

will be repeated can be

determined in several ways. In

XSI for example, some

instructions might be repeated

for every model in a scene.

Page 24: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

8 - functions

8 - Functions

Functions are ways of grouping a number of instructions

together in a way that allows them to be used in different parts of

a program without being written out over and over again.

By breaking down your program-writing problem into easily

solvable parts that can be embodied in functions you can build a

more complex set of instructions from simpler ones.

Functions are also used when we want to calculate a value and

‘return’ it. Python has many ‘library’ functions we can load in as

‘modules’. Examples being ‘range’, ‘random’ and ‘abs’.

Page 25: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Summary

Summary of part 2

A number of different constructs may be used by the

programmer to ensure that:

• instructions are executed repeatedly in loops

• choices may be made between several options

• certain instructions are only executed conditionally

Instructions and data declarations can be grouped together in

functions that can be used as modules to build more complex

programs.

Note: The Object Oriented approach to programming uses

similar control constructs but the data and instructions are

grouped differently leading to a slightly different syntax.

Page 26: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Summary

Note: The grey bars in the diagrams may be complex statements

which could include any number of statements, including

combinations of the control constructs described.

i.e. it is possible to have:

•loops within loops,

•conditional constructs with conditional constructs,

•loops within conditional constructs

•etc.

Computer programming involves the efficient use of

combinations of these constructs.

Page 27: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Observations

Breaking down a problem into handleable chunks is an important

aspect of computer programming.

As human beings we can only concentrate consciously on a few

things at a time.

Very complex programs cannot be understood in their entirety

but they may be understood as being made up of parts which,

when looked at on their own, can be understood.

We are used to this approach in other tasks, for example:

remembering routes, constructing things from components,

following cooking recipes, interpreting musical scores, following

knitting patterns etc..

Page 28: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Observations

The important thing to realise about using control constructs is

that the ways in which they are combined is similar in most

procedural programming languages.

Often converting a program from one language to another is a

relatively straightforward matter of changing the syntax; the

algorithm may not have to be changed.

Also, although there are not many control constructs, by careful

use of them in different combinations startlingly convoluted and

elaborate programs can be devised.

Programs can have a beauty of their own - one can admire the

efficiency with which a given set of instructions are composed

and algorithms implemented.

Page 29: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Observations

The way in which programs are developed is similar to many

other creative activities:

You work out what your goal is and make rough notes as to what

you want to achieve, breaking your design task down into sub-

goals.

Next you draft a rough algorithm, or several algorithms, which

you hope will, when encoded in a program, achieve the intended

outcome.

You then implement your design, checking it and revising it as

you go along.

Knowing when you should stop refining your program is a

similar challenge to knowing when a piece of art or design

‘works’.

Page 30: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Observations

Approaches to developing software:

• Software engineering (by the rules)

• Working intuitively “Hacking”

Programming can be seen:

• as a craft

• as design

• as an art

• as a science

Many different practices have been developed in the various

disciplines that use computers.

Page 31: Control Constructs VBScript - Bournemouthnccastaff.bournemouth.ac.uk/sbell/scriptinginmaya/Control Construc… · * Sometimes called ‘flow of control’, or ‘control flow’ Control

Observations

There are two basic approaches to program development:

Top Down:

Where you start from an overview of the whole program and then

attend to the details.

Bottom Up:

Where you build a complex program from smaller components,

which sometimes exist already.

Most programming consists of a combination of both

approaches.