CSE 452: Programming Languages Logical Programming Languages Part 2.
MzTEK Programming - Part 2
-
Upload
becky-stewart -
Category
Education
-
view
808 -
download
0
description
Transcript of MzTEK Programming - Part 2
REVIEW LAST WEEK
Think of idea
Break down problem into tiny steps
Write code for one step
Run program
Code You’ve Written
Instructions for Computer?
Executable Program (e.g. .exe or .app)
Interactive Development
Environment (IDE)
Compiler +
Linker +
Loader
int x;int y;int width;int height;
size(200, 200);
x = 150;y = 100;width = 90;height = 80;
ellipse(x, y, width, height);
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
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
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
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
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.
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
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
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
Need to declare before or at the same time as initialisation.
Need to declare before or at the same time as initialisation.
myInt = 3;int myInt;
Need to declare before or at the same time as initialisation.
myInt = 3;int myInt;
int myInt; myInt = 3;
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.
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.
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.
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 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 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 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
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
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;
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
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
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
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
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.
ARRAYS
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);
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
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”?
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
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]
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.
To create a new list (to declare it):
int[] x = new int[4];
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
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
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
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
To initialise a new list :
x[0] = 20;x[1] = 30;x[2] = 150;x[3] = 160;
To initialise a new list :
x[0] = 20;x[1] = 30;x[2] = 150;x[3] = 160;
what list are you referring to?
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?
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?
int[] x = new int[4];
x[0]
x[1]
x[2]
x[3]
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]
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]
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
You can declare and initialise a new list at the same time :
int[] x = {20, 30, 150, 160};
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
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
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
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
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);
CONTROL STRUCTURES
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
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
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
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.
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.
HOW TO MAKE A DECISION
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).
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).
<
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
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
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
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).
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).
>
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
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
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
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.
if ( ) {
}else {
}
if ( ) {
}else {
}
put in a comparison statement (like < or >)
if ( ) {
}else {
}
put in a comparison statement (like < or >)
what to do if our comparison statement is true
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
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
If something is true, do an action.
If something isn’t true, instead do a different action.
If something is true, do an action.
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 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.
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.
When graphing information, we are used to numbers
A NOTE ON AXES
When graphing information, we are used to numbers
increasing as we move right
A NOTE ON AXES
When graphing information, we are used to numbers
and increasing as we move up
increasing as we move right
A NOTE ON AXES
A NOTE ON AXES
This is different to how numbers work with programming graphics.
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)
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)
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)
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);
}
What if we want to use multiple if statements?
int counter;// some other code...if (counter < 10) {if (counter > 0 ) {counter++;
}}
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.
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.
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.
int counter;// some other code...if (counter > 10) {counter = 0;
}if (counter < 0 ) {}counter = 0;
}
What if we want to use multiple if statements?
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?
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?
BOOLEAN OPERATOR OR
ORTrue False
TrueFalse
True True
False False
OR
OR
OR
is
is
is
is
BOOLEAN OPERATOR OR
ORTrue False
TrueFalse
True
True True
False False
OR
OR
OR
is
is
is
is
BOOLEAN OPERATOR OR
ORTrue False
TrueFalse
True
True True
False False
OR
OR
OR
is
is
is
is
True
BOOLEAN OPERATOR OR
ORTrue False
TrueFalse
True
True True
False False
OR
OR
OR
is
is
is
is
True
True
BOOLEAN OPERATOR OR
ORTrue False
TrueFalse
True
False
True True
False False
OR
OR
OR
is
is
is
is
True
True
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 ||
BOOLEAN OPERATOR AND
ANDTrue False
TrueFalse
True True
False False
AND
AND
AND
is
is
is
is
BOOLEAN OPERATOR AND
ANDTrue False
TrueFalse
True True
False False
AND
AND
AND
is
is
is
is
False
False
BOOLEAN OPERATOR AND
ANDTrue False
TrueFalse
True True
False False
AND
AND
AND
is
is
is
is
False
False
BOOLEAN OPERATOR AND
ANDTrue False
TrueFalse
True True
False False
AND
AND
AND
is
is
is
is
True
False
False
BOOLEAN OPERATOR AND
ANDTrue False
TrueFalse
False
True True
False False
AND
AND
AND
is
is
is
is
True
False
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
BOOLEAN OPERATOR NOT
NOT True
FalseNOT
is
is
BOOLEAN OPERATOR NOT
NOT True
FalseNOT
Falseis
is
BOOLEAN OPERATOR NOT
NOT True
FalseNOT
False
True
is
is
BOOLEAN OPERATOR NOT
NOT True
FalseNOT
False
True
is
is
When using NOT in code, type !
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.
LOOPS
LOOPS
There are two ways to repeat something:
1. Do this N number of times.
2. Keep doing this until something else happens.
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.
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.
DO THIS N TIMES
int i;for(i=0; i<4; i++) {
}
DO THIS N TIMES
int i;for(i=0; i<4; i++) {
}
start with a number, in this case 0
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
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
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
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
KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS
while ( ) {
}
KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS
while ( ) {
}
if the statement here is true
KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS
while ( ) {
}
if the statement here is true
then do what is between { } once
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
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;
}