MzTEK Programming - Part 2

131

description

Second lecture on introductory programming for MzTEK. It covers arrays and primitive data types. It is assumed you are at least a little familiar with Processing.

Transcript of MzTEK Programming - Part 2

Page 1: MzTEK Programming - Part 2
Page 2: MzTEK Programming - Part 2

REVIEW LAST WEEK

Page 3: MzTEK Programming - Part 2

Think of idea

Break down problem into tiny steps

Write code for one step

Run program

Page 4: MzTEK Programming - Part 2

Code You’ve Written

Instructions for Computer?

Executable Program (e.g. .exe or .app)

Interactive Development

Environment (IDE)

Compiler +

Linker +

Loader

Page 5: MzTEK Programming - Part 2

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

Page 6: MzTEK Programming - Part 2

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

ingredients or variables

Page 7: MzTEK Programming - Part 2

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

ingredients or variables

directions or algorithm

Page 8: MzTEK Programming - Part 2

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

ingredients or variables

directions or algorithm

data type

Page 9: MzTEK Programming - Part 2

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

ingredients or variables

directions or algorithm

data type variable name

Page 10: MzTEK Programming - Part 2

DECLARATION VS INITIALISATIONAs in a recipe, ingredients need to be listed at the top so you know what to buy.

In code, it’s so the compiler knows how much memory to reserve. This is called declaring.

Page 11: MzTEK Programming - Part 2

DECLARATION VS INITIALISATIONAs in a recipe, ingredients need to be listed at the top so you know what to buy.

In code, it’s so the compiler knows how much memory to reserve. This is called declaring.

int myInt;

Reserve space for an int

Page 12: MzTEK Programming - Part 2

DECLARATION VS INITIALISATIONAs in a recipe, ingredients need to be listed at the top so you know what to buy.

In code, it’s so the compiler knows how much memory to reserve. This is called declaring.

We don’t have to know what value will be stored in myInt right away.

We can choose a value later. This is called initialising.

int myInt;

Reserve space for an int

Page 13: MzTEK Programming - Part 2

DECLARATION VS INITIALISATIONAs in a recipe, ingredients need to be listed at the top so you know what to buy.

In code, it’s so the compiler knows how much memory to reserve. This is called declaring.

We don’t have to know what value will be stored in myInt right away.

We can choose a value later. This is called initialising.

int myInt;

Reserve space for an int

myInt = 15;

Store the value 15 in the space reserved for myInt

15

Page 14: MzTEK Programming - Part 2

Need to declare before or at the same time as initialisation.

Page 15: MzTEK Programming - Part 2

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

Page 16: MzTEK Programming - Part 2

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

int myInt; myInt = 3;

Page 17: MzTEK Programming - Part 2

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

int myInt; myInt = 3;

Can’t use a variable before it is initialised.

Page 18: MzTEK Programming - Part 2

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

int myInt; myInt = 3;

int myInt;myInt = myInt + 7;

Can’t use a variable before it is initialised.

Page 19: MzTEK Programming - Part 2

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

int myInt; myInt = 3;

int myInt = 3;myInt = myInt + 7;

int myInt;myInt = myInt + 7;

Can’t use a variable before it is initialised.

Page 20: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

floatintchar

Page 21: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

floatintchar

to create a float type

Page 22: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

floatintchar

to create a float type

to create an int type

Page 23: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

floatintchar

to create a float type

to create an int type

to create a char type

Page 24: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

floatintchar

to create a float type

to create an int type

to create a char type

Page 25: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

Page 26: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

you get to choose the name

Page 27: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

3. If you already know what the value of that variable is, then go ahead and set the value.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

you get to choose the name

Page 28: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

3. If you already know what the value of that variable is, then go ahead and set the value.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

float scale = 0.5;int redValue = 199;char finalMark = ‘B’;

you get to choose the name

Page 29: MzTEK Programming - Part 2

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

3. If you already know what the value of that variable is, then go ahead and set the value.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

float scale = 0.5;int redValue = 199;char finalMark = ‘B’;

if you don’t know the value yet, stop at step 2. but remember to end

each line with a ;

you get to choose the name

Page 30: MzTEK Programming - Part 2

EXERCISE

In Processing, draw a purple quadrilateral using the quad( ) function.

quad(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)

Use variables to represent corner of the quadrilateral.

Page 31: MzTEK Programming - Part 2

ARRAYS

Page 32: MzTEK Programming - Part 2

int x0 = 20;int y0 = 20;int x1 = 30;int y1 = 50;int x2 = 150;int y2 = 50;int x3 = 160;int y3 = 20;

size(200, 200);quad(x0, y0, x1, y1, x2, y2, x3, y3);

Page 33: MzTEK Programming - Part 2

int x0 = 20;int y0 = 20;int x1 = 30;int y1 = 50;int x2 = 150;int y2 = 50;int x3 = 160;int y3 = 20;

size(200, 200);quad(x0, y0, x1, y1, x2, y2, x3, y3);

this is a pain to keep track of

Page 34: MzTEK Programming - Part 2

int x0 = 20;int y0 = 20;int x1 = 30;int y1 = 50;int x2 = 150;int y2 = 50;int x3 = 160;int y3 = 20;

size(200, 200);quad(x0, y0, x1, y1, x2, y2, x3, y3);

this is a pain to keep track of

Wouldn’t it be easier to just have two lists, so only two variable names: “x” and “y”?

Page 35: MzTEK Programming - Part 2

X

0.) 20

1.) 30

2.)150

3.)160

Y

0.) 20

1.) 50

2.) 50

3.) 20

What if had a list called X

And a list called Y

Page 36: MzTEK Programming - Part 2

X

0.) 20

1.) 30

2.)150

3.)160

Y

0.) 20

1.) 50

2.) 50

3.) 20

What if had a list called X

And a list called Y

So if we just want the number at 2.) in the X list, we’d type

x[2]

Page 37: MzTEK Programming - Part 2

X

0.) 20

1.) 30

2.)150

3.)160

Y

0.) 20

1.) 50

2.) 50

3.) 20

What if had a list called X

And a list called Y

So if we just want the number at 2.) in the X list, we’d type

x[2]

That’s all an array is:a list of things, where we don’t have to

name each thing, just the list.

Page 38: MzTEK Programming - Part 2

To create a new list (to declare it):

int[] x = new int[4];

Page 39: MzTEK Programming - Part 2

To create a new list (to declare it):

int[] x = new int[4];

what kind of things are in the list? i.e. how much space needs to be

reserved for each item

Page 40: MzTEK Programming - Part 2

To create a new list (to declare it):

int[] x = new int[4];

what kind of things are in the list? i.e. how much space needs to be

reserved for each item

[ ] means the data type is an array

Page 41: MzTEK Programming - Part 2

To create a new list (to declare it):

int[] x = new int[4];

what is the name of the list?

what kind of things are in the list? i.e. how much space needs to be

reserved for each item

[ ] means the data type is an array

Page 42: MzTEK Programming - Part 2

To create a new list (to declare it):

int[] x = new int[4];

what is the name of the list?

what kind of things are in the list? i.e. how much space needs to be

reserved for each item how long will the list be?

[ ] means the data type is an array

Page 43: MzTEK Programming - Part 2

To initialise a new list :

x[0] = 20;x[1] = 30;x[2] = 150;x[3] = 160;

Page 44: MzTEK Programming - Part 2

To initialise a new list :

x[0] = 20;x[1] = 30;x[2] = 150;x[3] = 160;

what list are you referring to?

Page 45: MzTEK Programming - Part 2

To initialise a new list :

x[0] = 20;x[1] = 30;x[2] = 150;x[3] = 160;

which item from the list?

what list are you referring to?

Page 46: MzTEK Programming - Part 2

To initialise a new list :

x[0] = 20;x[1] = 30;x[2] = 150;x[3] = 160;

which item from the list?

what list are you referring to?

what is the value of that item?

Page 47: MzTEK Programming - Part 2

int[] x = new int[4];

x[0]

x[1]

x[2]

x[3]

Page 48: MzTEK Programming - Part 2

int[] x = new int[4];

what kind of things are in the list? i.e. how much space needs to be

reserved for each item

x[0]

x[1]

x[2]

x[3]

Page 49: MzTEK Programming - Part 2

int[] x = new int[4];

what kind of things are in the list? i.e. how much space needs to be

reserved for each item how long will the list be?

x[0]

x[1]

x[2]

x[3]

Page 50: MzTEK Programming - Part 2

int[] x = new int[4];

what kind of things are in the list? i.e. how much space needs to be

reserved for each item how long will the list be?

x[0]

x[1]

x[2]

x[3]

the length of the array and the data type determine

how much memory is used

Page 51: MzTEK Programming - Part 2

You can declare and initialise a new list at the same time :

int[] x = {20, 30, 150, 160};

Page 52: MzTEK Programming - Part 2

You can declare and initialise a new list at the same time :

int[] x = {20, 30, 150, 160};

what kind of things are in the list? i.e. how much space needs to be

reserved for each item

Page 53: MzTEK Programming - Part 2

You can declare and initialise a new list at the same time :

int[] x = {20, 30, 150, 160};

what kind of things are in the list? i.e. how much space needs to be

reserved for each item

[ ] means the data type is an array

Page 54: MzTEK Programming - Part 2

You can declare and initialise a new list at the same time :

int[] x = {20, 30, 150, 160};

what is the name of the list?

what kind of things are in the list? i.e. how much space needs to be

reserved for each item

[ ] means the data type is an array

Page 55: MzTEK Programming - Part 2

You can declare and initialise a new list at the same time :

int[] x = {20, 30, 150, 160};

what is the value of each item?

what is the name of the list?

what kind of things are in the list? i.e. how much space needs to be

reserved for each item

[ ] means the data type is an array

Page 56: MzTEK Programming - Part 2

In Processing, rewrite this code so that it uses two arrays.

EXERCISE

int x0 = 20;int y0 = 20;int x1 = 30;int y1 = 50;int x2 = 150;int y2 = 50;int x3 = 160;int y3 = 20;

size(200, 200);quad(x0, y0, x1, y1, x2, y2, x3, y3);

Page 57: MzTEK Programming - Part 2

CONTROL STRUCTURES

Page 58: MzTEK Programming - Part 2

1. Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

2. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

3. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

Page 59: MzTEK Programming - Part 2

1. Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

2. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

3. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

Page 60: MzTEK Programming - Part 2

1. Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

2. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

3. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

Page 61: MzTEK Programming - Part 2

1. Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

2. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

3. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

We’ve covered how to create ingredients or variables.

Page 62: MzTEK Programming - Part 2

1. Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

2. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

3. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

We’ve covered how to create ingredients or variables.

Now onto the basics of how to use those variables.

Page 63: MzTEK Programming - Part 2

HOW TO MAKE A DECISION

Page 64: MzTEK Programming - Part 2

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

Page 65: MzTEK Programming - Part 2

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

<

Page 66: MzTEK Programming - Part 2

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

<less than

Page 67: MzTEK Programming - Part 2

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

7 4<less than

Page 68: MzTEK Programming - Part 2

FALSE

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

7 4<less than

Page 69: MzTEK Programming - Part 2

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

Page 70: MzTEK Programming - Part 2

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

>

Page 71: MzTEK Programming - Part 2

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

>greater than

Page 72: MzTEK Programming - Part 2

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

7 4>greater than

Page 73: MzTEK Programming - Part 2

TRUE

HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with those variables.

The most basic step is to compare those variables to something else.

This is done with comparators - questions that can be answered yes or no (or true or false).

7 4>greater than

Page 74: MzTEK Programming - Part 2

OTHER COMPARISONS

>=

<=

==

!=

greater than or equal to

less than or equal to

equal to

not equal to

All of these result in true or false.

Page 75: MzTEK Programming - Part 2

if ( ) {

}else {

}

Page 76: MzTEK Programming - Part 2

if ( ) {

}else {

}

put in a comparison statement (like < or >)

Page 77: MzTEK Programming - Part 2

if ( ) {

}else {

}

put in a comparison statement (like < or >)

what to do if our comparison statement is true

Page 78: MzTEK Programming - Part 2

if ( ) {

}else {

}

put in a comparison statement (like < or >)

what to do if our comparison statement is true

what to do if our comparison statement is false

Page 79: MzTEK Programming - Part 2

if ( ) {

}else {

}

we don’t have to always have an else

statement, sometimes you only care if the statement is true

put in a comparison statement (like < or >)

what to do if our comparison statement is true

what to do if our comparison statement is false

Page 80: MzTEK Programming - Part 2

If something is true, do an action.

Page 81: MzTEK Programming - Part 2

If something isn’t true, instead do a different action.

If something is true, do an action.

Page 82: MzTEK Programming - Part 2

If something isn’t true, instead do a different action.

If something is true, do an action.

If the potatoes are too lumpy, keep mashing.

Page 83: MzTEK Programming - Part 2

If something isn’t true, instead do a different action.

If something is true, do an action.

If the potatoes are too lumpy, keep mashing.

If the potatoes are not too lumpy, stop mashing.

Page 84: MzTEK Programming - Part 2

If something isn’t true, instead do a different action.

If something is true, do an action.

If the potatoes are too lumpy, keep mashing.

If the potatoes are not too lumpy, stop mashing.

We don’t have to test this twice. We know if the potatoes are either too lumpy or not too

lumpy.

Page 85: MzTEK Programming - Part 2

When graphing information, we are used to numbers

A NOTE ON AXES

Page 86: MzTEK Programming - Part 2

When graphing information, we are used to numbers

increasing as we move right

A NOTE ON AXES

Page 87: MzTEK Programming - Part 2

When graphing information, we are used to numbers

and increasing as we move up

increasing as we move right

A NOTE ON AXES

Page 88: MzTEK Programming - Part 2

A NOTE ON AXES

This is different to how numbers work with programming graphics.

Page 89: MzTEK Programming - Part 2

A NOTE ON AXES

This is different to how numbers work with programming graphics.

Numbers start in the upper left corner at the

origin (0, 0)

Page 90: MzTEK Programming - Part 2

A NOTE ON AXES

This is different to how numbers work with programming graphics.

and increase as we move right

Numbers start in the upper left corner at the

origin (0, 0)

Page 91: MzTEK Programming - Part 2

A NOTE ON AXES

This is different to how numbers work with programming graphics.

and increase as we move down

and increase as we move right

Numbers start in the upper left corner at the

origin (0, 0)

Page 92: MzTEK Programming - Part 2

EXERCISECreate a Processing sketch which draws a green circle if the mouse is in the top half of the window and changes the circle’s colour to red if the mouse in the bottom half of the window. Start with the code below.

void setup() {// create the windowsize(400, 400);

}

void draw() {// set the colourfill(10, 10, 255);

// draw the circleellipse(mouseX, mouseY, 100, 100);

}

Page 93: MzTEK Programming - Part 2

What if we want to use multiple if statements?

int counter;// some other code...if (counter < 10) {if (counter > 0 ) {counter++;

}}

Page 94: MzTEK Programming - Part 2

What if we want to use multiple if statements?

int counter;// some other code...if (counter < 10) {if (counter > 0 ) {counter++;

}}

If counter is less than 10 and greater than 0, then increase counter by 1.

Page 95: MzTEK Programming - Part 2

What if we want to use multiple if statements?

int counter;// some other code...if (counter < 10) {if (counter > 0 ) {counter++;

}}

If counter is less than 10 and greater than 0, then increase counter by 1.

counter is only increased if both if statements are true.

Page 96: MzTEK Programming - Part 2

What if we want to use multiple if statements?

int counter;// some other code...if (counter < 10) {if (counter > 0 ) {counter++;

}}

If counter is less than 10 and greater than 0, then increase counter by 1.

These are called nested if statements, because one is inside the { } of the other.

counter is only increased if both if statements are true.

Page 97: MzTEK Programming - Part 2

int counter;// some other code...if (counter > 10) {counter = 0;

}if (counter < 0 ) {}counter = 0;

}

What if we want to use multiple if statements?

Page 98: MzTEK Programming - Part 2

int counter;// some other code...if (counter > 10) {counter = 0;

}if (counter < 0 ) {}counter = 0;

}

If counter is greater than 10 or less than 0, then reset counter to 0.

What if we want to use multiple if statements?

Page 99: MzTEK Programming - Part 2

int counter;// some other code...if (counter > 10) {counter = 0;

}if (counter < 0 ) {}counter = 0;

}

If counter is greater than 10 or less than 0, then reset counter to 0.

counter is reset if either if

statements are true.

What if we want to use multiple if statements?

Page 100: MzTEK Programming - Part 2

BOOLEAN OPERATOR OR

ORTrue False

TrueFalse

True True

False False

OR

OR

OR

is

is

is

is

Page 101: MzTEK Programming - Part 2

BOOLEAN OPERATOR OR

ORTrue False

TrueFalse

True

True True

False False

OR

OR

OR

is

is

is

is

Page 102: MzTEK Programming - Part 2

BOOLEAN OPERATOR OR

ORTrue False

TrueFalse

True

True True

False False

OR

OR

OR

is

is

is

is

True

Page 103: MzTEK Programming - Part 2

BOOLEAN OPERATOR OR

ORTrue False

TrueFalse

True

True True

False False

OR

OR

OR

is

is

is

is

True

True

Page 104: MzTEK Programming - Part 2

BOOLEAN OPERATOR OR

ORTrue False

TrueFalse

True

False

True True

False False

OR

OR

OR

is

is

is

is

True

True

Page 105: MzTEK Programming - Part 2

BOOLEAN OPERATOR OR

ORTrue False

TrueFalse

True

False

True True

False False

OR

OR

OR

is

is

is

is

True

True

When using OR in code, type ||

Page 106: MzTEK Programming - Part 2

BOOLEAN OPERATOR AND

ANDTrue False

TrueFalse

True True

False False

AND

AND

AND

is

is

is

is

Page 107: MzTEK Programming - Part 2

BOOLEAN OPERATOR AND

ANDTrue False

TrueFalse

True True

False False

AND

AND

AND

is

is

is

is

False

Page 108: MzTEK Programming - Part 2

False

BOOLEAN OPERATOR AND

ANDTrue False

TrueFalse

True True

False False

AND

AND

AND

is

is

is

is

False

Page 109: MzTEK Programming - Part 2

False

BOOLEAN OPERATOR AND

ANDTrue False

TrueFalse

True True

False False

AND

AND

AND

is

is

is

is

True

False

Page 110: MzTEK Programming - Part 2

False

BOOLEAN OPERATOR AND

ANDTrue False

TrueFalse

False

True True

False False

AND

AND

AND

is

is

is

is

True

False

Page 111: MzTEK Programming - Part 2

False

BOOLEAN OPERATOR AND

ANDTrue False

TrueFalse

False

True True

False False

AND

AND

AND

is

is

is

is

True

When using OR in code, type &&

False

Page 112: MzTEK Programming - Part 2

BOOLEAN OPERATOR NOT

NOT True

FalseNOT

is

is

Page 113: MzTEK Programming - Part 2

BOOLEAN OPERATOR NOT

NOT True

FalseNOT

Falseis

is

Page 114: MzTEK Programming - Part 2

BOOLEAN OPERATOR NOT

NOT True

FalseNOT

False

True

is

is

Page 115: MzTEK Programming - Part 2

BOOLEAN OPERATOR NOT

NOT True

FalseNOT

False

True

is

is

When using NOT in code, type !

Page 116: MzTEK Programming - Part 2

EXERCISEModify your Processing sketch which draws a green circle if the mouse is in the top half of the window and changes the circle’s colour to red if the mouse in the bottom half of the window.

Now using the AND statement, draw

• a green circle in the upper left quadrant of the window,

• a blue circle in the upper right quadrant,

• a red circle in the lower left quadrant,

• and a yellow circle in the lower right quadrant.

Page 117: MzTEK Programming - Part 2

LOOPS

Page 118: MzTEK Programming - Part 2

LOOPS

There are two ways to repeat something:

1. Do this N number of times.

2. Keep doing this until something else happens.

Page 119: MzTEK Programming - Part 2

LOOPS

There are two ways to repeat something:

1. Do this N number of times.

2. Keep doing this until something else happens.

Repeat this event in the calendar this

many times.

Page 120: MzTEK Programming - Part 2

LOOPS

There are two ways to repeat something:

1. Do this N number of times.

2. Keep doing this until something else happens.

Repeat this event in the calendar this

many times.

Repeat this event in the calendar until a certain

date occurs.

Page 121: MzTEK Programming - Part 2

DO THIS N TIMES

int i;for(i=0; i<4; i++) {

}

Page 122: MzTEK Programming - Part 2

DO THIS N TIMES

int i;for(i=0; i<4; i++) {

}

start with a number, in this case 0

Page 123: MzTEK Programming - Part 2

DO THIS N TIMES

int i;for(i=0; i<4; i++) {

}

start with a number, in this case 0

if this statement is true

Page 124: MzTEK Programming - Part 2

DO THIS N TIMES

int i;for(i=0; i<4; i++) {

}

start with a number, in this case 0

if this statement is true

then do whatever is written here

Page 125: MzTEK Programming - Part 2

DO THIS N TIMES

int i;for(i=0; i<4; i++) {

}

start with a number, in this case 0

if this statement is true

when you’ve done what’s in the { } once, do this, in this case add make i equal to its current value plus 1

then do whatever is written here

Page 126: MzTEK Programming - Part 2

DO THIS N TIMES

int i;for(i=0; i<4; i++) {

}

start with a number, in this case 0

if this statement is true

when you’ve done what’s in the { } once, do this, in this case add make i equal to its current value plus 1

then do whatever is written here

go back to see if the middle statement is still

true

Page 127: MzTEK Programming - Part 2

KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS

while ( ) {

}

Page 128: MzTEK Programming - Part 2

KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS

while ( ) {

}

if the statement here is true

Page 129: MzTEK Programming - Part 2

KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS

while ( ) {

}

if the statement here is true

then do what is between { } once

Page 130: MzTEK Programming - Part 2

KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS

while ( ) {

}

if the statement here is true

then do what is between { } once

then repeat by checking the statement again

Page 131: MzTEK Programming - Part 2

EXERCISEWrite out each iteration of these loops and what the variables equal at the end of each loop.

int i;int j = 15;

for (i=0; i<12; i++) {j = j * 2 - i;

}

int k = 100;

while ( k > 0 ) {k = k -10;

}