Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

MzTEK Programming - Part 2

1,157 views

Published on

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.

Published in: Education
  • Hi! Get Your Professional Job-Winning Resume Here! 👉 http://bit.ly/rexumtop
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

MzTEK Programming - Part 2

  1. 1. REVIEW LAST WEEK
  2. 2. Think of idea Break down problem into tiny steps Write code for one step Run program
  3. 3. Interactive Development Environment (IDE)Code You’ve Instructions for Written ? Computer Compiler Executable Program + (e.g. .exe or .app) Linker + Loader
  4. 4. int x;int y;int width;int height;size(200, 200);x = 150;y = 100;width = 90;height = 80;ellipse(x, y, width, height);
  5. 5. int x;int y;int width; ingredients or variablesint height;size(200, 200);x = 150;y = 100;width = 90;height = 80;ellipse(x, y, width, height);
  6. 6. int x;int y;int width; ingredients or variablesint height;size(200, 200);x = 150;y = 100; directions or algorithmwidth = 90;height = 80;ellipse(x, y, width, height);
  7. 7. data type int x; int y; int width; ingredients or variables int height; size(200, 200); x = 150; y = 100; directions or algorithm width = 90; height = 80; ellipse(x, y, width, height);
  8. 8. data type variable name int x; int y; int width; ingredients or variables int height; size(200, 200); x = 150; y = 100; directions or algorithm width = 90; height = 80; ellipse(x, y, width, height);
  9. 9. DECLARATION VS INITIALISATION As 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.
  10. 10. DECLARATION VS INITIALISATION As 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
  11. 11. DECLARATION VS INITIALISATION As in a recipe, ingredients We don’t have to know need to be listed at the top what value will be stored in so you know what to buy. myInt right away. In code, it’s so the compiler We can choose a value knows how much memory to reserve. This is called later. This is called initialising. declaring. int myInt; Reserve space for an int
  12. 12. DECLARATION VS INITIALISATION As in a recipe, ingredients We don’t have to know need to be listed at the top what value will be stored in so you know what to buy. myInt right away. In code, it’s so the compiler We can choose a value knows how much memory to reserve. This is called later. This is called initialising. declaring. int myInt; myInt = 15; Reserve space for Store the value 15 in the an int space reserved for myInt 15
  13. 13. Need to declare before or at the sametime as initialisation.
  14. 14. Need to declare before or at the sametime as initialisation.myInt = 3;int myInt;
  15. 15. Need to declare before or at the sametime as initialisation.myInt = 3; int myInt;int myInt; myInt = 3;
  16. 16. Need to declare before or at the sametime as initialisation.myInt = 3; int myInt;int myInt; myInt = 3;Can’t use a variable before it is initialised.
  17. 17. 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.int myInt;myInt = myInt + 7;
  18. 18. 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.int myInt; int myInt = 3;myInt = myInt + 7; myInt = myInt + 7;
  19. 19. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. float int char
  20. 20. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float int char
  21. 21. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float to create an int type int char
  22. 22. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float to create an int type int to create a char type char
  23. 23. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float to create an int type int to create a char type char2. Decide on a name for your variable. Remember the rules.
  24. 24. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float to create an int type int to create a char type char2. Decide on a name for your variable. Remember the rules. float scale; int redValue; char finalMark;
  25. 25. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float to create an int type int to create a char type char2. Decide on a name for your variable. Remember the rules. float scale; you get to choose int redValue; the name char finalMark;
  26. 26. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float to create an int type int to create a char type char2. Decide on a name for your variable. Remember the rules. float scale; you get to choose int redValue; the name char finalMark;3. If you already know what the value of that variable is, then go ahead and set the value.
  27. 27. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float to create an int type int to create a char type char2. Decide on a name for your variable. Remember the rules. float scale; you get to choose int redValue; the name char finalMark;3. If you already know what the value of that variable is, then go ahead and set the value. float scale = 0.5; int redValue = 199; char finalMark = ‘B’;
  28. 28. TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main decisions are between int, float and char. to create a float type float to create an int type int to create a char type char2. Decide on a name for your variable. Remember the rules. float scale; you get to choose int redValue; the name char finalMark;3. If you already know what the value of that variable is, then go ahead and set the value. if you don’t know the float scale = 0.5; value yet, stop at step 2. int redValue = 199; but remember to end char finalMark = ‘B’; each line with a ;
  29. 29. EXERCISEIn Processing, draw a purple quadrilateral using thequad( ) 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.
  30. 30. ARRAYS
  31. 31. 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);
  32. 32. int x0 = 20;int y0 = 20;int x1 = 30;int y1 = 50; this is a pain to keep track ofint x2 = 150;int y2 = 50;int x3 = 160;int y3 = 20;size(200, 200);quad(x0, y0, x1, y1, x2, y2, x3, y3);
  33. 33. int x0 = 20;int y0 = 20;int x1 = 30;int y1 = 50; this is a pain to keep track ofint x2 = 150;int y2 = 50;int x3 = 160; Wouldn’t it be easier to justint y3 = 20; have two lists, so only two variable names: “x” and “y”?size(200, 200);quad(x0, y0, x1, y1, x2, y2, x3, y3);
  34. 34. What if had a list And a list called X called Y X Y 0.) 20 0.) 20 1.) 30 1.) 50 2.)150 2.) 50 3.)160 3.) 20
  35. 35. What if had a list And a list called X called Y X Y 0.) 20 0.) 20 1.) 30 1.) 50 2.)150 2.) 50 3.)160 3.) 20 So if we just want the number at 2.) in the X list, we’d type x[2]
  36. 36. What if had a list And a list called X called Y X Y 0.) 20 0.) 20 That’s all an array is: 1.) 30 1.) 50a list of things, where we don’t have to name each thing, just the list. 2.)150 2.) 50 3.)160 3.) 20 So if we just want the number at 2.) in the X list, we’d type x[2]
  37. 37. To create a new list (to declare it): int[] x = new int[4];
  38. 38. To create a new list (to declare it):what kind of things are in the list?i.e. how much space needs to be reserved for each item int[] x = new int[4];
  39. 39. To create a new list (to declare it): what kind of things are in the list? i.e. how much space needs to be reserved for each item int[] x = new int[4];[ ] means the data type is an array
  40. 40. To create a new list (to declare it): what kind of things are in the list? i.e. how much space needs to be reserved for each item int[] x = new int[4];[ ] means the data type is an array what is the name of the list?
  41. 41. To create a new list (to declare it): 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? int[] x = new int[4];[ ] means the data type is an array what is the name of the list?
  42. 42. To initialise a new list : x[0] = 20; x[1] = 30; x[2] = 150; x[3] = 160;
  43. 43. To initialise a new list :what list are you referring to? x[0] = 20; x[1] = 30; x[2] = 150; x[3] = 160;
  44. 44. To initialise a new list :what list are you referring to? x[0] = 20; x[1] = 30; x[2] = 150; x[3] = 160; which item from the list?
  45. 45. To initialise a new list :what list are you referring to? what is the value x[0] = 20; of that item? x[1] = 30; x[2] = 150; x[3] = 160; which item from the list?
  46. 46. int[] x = new int[4]; x[0] x[1] x[2] x[3]
  47. 47. what kind of things are in the list?i.e. how much space needs to be reserved for each item int[] x = new int[4]; x[0] x[1] x[2] x[3]
  48. 48. 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? int[] x = new int[4]; x[0] x[1] x[2] x[3]
  49. 49. 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? int[] x = new int[4]; x[0] x[1] the length of the array and the data type determine x[2] how much memory is used x[3]
  50. 50. You can declare and initialise a new list at the same time :int[] x = {20, 30, 150, 160};
  51. 51. You can declare and initialise a new list at the same time :what kind of things are in the list?i.e. how much space needs to be reserved for each item int[] x = {20, 30, 150, 160};
  52. 52. You can declare and initialise a new list at the same time : what kind of things are in the list? i.e. how much space needs to be reserved for each item int[] x = {20, 30, 150, 160};[ ] means the data type is an array
  53. 53. You can declare and initialise a new list at the same time : what kind of things are in the list? i.e. how much space needs to be reserved for each item int[] x = {20, 30, 150, 160};[ ] means the data type is an array what is the name of the list?
  54. 54. You can declare and initialise a new list at the same time : what kind of things are in the list? i.e. how much space needs to be what is the value reserved for each item of each item? int[] x = {20, 30, 150, 160};[ ] means the data type is an array what is the name of the list?
  55. 55. EXERCISEIn Processing, rewrite this code so that it uses two 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);
  56. 56. CONTROL STRUCTURES
  57. 57. 2 tbsp olive oil or sun-dried tomato oil from the jar 1 tsp dried oregano or a small handful of fresh leaves, chopped 6 rashers of smoked streaky bacon, chopped 1 tsp dried thyme or a small handful of fresh leaves, chopped 2 large onions, chopped Drizzle balsamic vinegar 3 garlic cloves, crushed 12-14 sun-dried tomato halves, in oil 1kg/2¼lb lean minced beef Salt and freshly ground black pepper 2 large glasses of red wine A good handful of fresh basil leaves, torn 2x400g cans chopped tomatoes into small pieces 1x290g jar antipasti marinated 800g-1kg/1¾-2¼lb dried spaghetti mushrooms, drained Lots of freshly grated parmesan, to serve 2 fresh or dried bay leaves1. 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 its 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 black http://www.bbc.co.uk/food/recipes/spaghettibolognese_67868
  58. 58. 2 tbsp olive oil or sun-dried tomato oil from the jar 1 tsp dried oregano or a small handful of fresh leaves, chopped 6 rashers of smoked streaky bacon, chopped 1 tsp dried thyme or a small handful of fresh leaves, chopped 2 large onions, chopped Drizzle balsamic vinegar 3 garlic cloves, crushed 12-14 sun-dried tomato halves, in oil 1kg/2¼lb lean minced beef Salt and freshly ground black pepper 2 large glasses of red wine A good handful of fresh basil leaves, torn 2x400g cans chopped tomatoes into small pieces 1x290g jar antipasti marinated 800g-1kg/1¾-2¼lb dried spaghetti mushrooms, drained Lots of freshly grated parmesan, to serve 2 fresh or dried bay leaves1. 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 its 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 black http://www.bbc.co.uk/food/recipes/spaghettibolognese_67868
  59. 59. 2 tbsp olive oil or sun-dried tomato oil from the jar 1 tsp dried oregano or a small handful of fresh leaves, chopped 6 rashers of smoked streaky bacon, chopped 1 tsp dried thyme or a small handful of fresh leaves, chopped 2 large onions, chopped Drizzle balsamic vinegar 3 garlic cloves, crushed 12-14 sun-dried tomato halves, in oil 1kg/2¼lb lean minced beef Salt and freshly ground black pepper 2 large glasses of red wine A good handful of fresh basil leaves, torn 2x400g cans chopped tomatoes into small pieces 1x290g jar antipasti marinated 800g-1kg/1¾-2¼lb dried spaghetti mushrooms, drained Lots of freshly grated parmesan, to serve 2 fresh or dried bay leaves1. 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 its 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 black http://www.bbc.co.uk/food/recipes/spaghettibolognese_67868
  60. 60. 2 tbsp olive oil or sun-dried tomato oil from the jar 1 tsp dried oregano or a small handful of fresh leaves, chopped 6 rashers of smoked streaky bacon, We’ve covered how to create 1 tsp dried thyme or a small handful of chopped 2 large onions, chopped ingredients or variables. fresh leaves, chopped Drizzle balsamic vinegar 3 garlic cloves, crushed 12-14 sun-dried tomato halves, in oil 1kg/2¼lb lean minced beef Salt and freshly ground black pepper 2 large glasses of red wine A good handful of fresh basil leaves, torn 2x400g cans chopped tomatoes into small pieces 1x290g jar antipasti marinated 800g-1kg/1¾-2¼lb dried spaghetti mushrooms, drained Lots of freshly grated parmesan, to serve 2 fresh or dried bay leaves1. 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 its 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 black http://www.bbc.co.uk/food/recipes/spaghettibolognese_67868
  61. 61. 2 tbsp olive oil or sun-dried tomato oil from the jar 1 tsp dried oregano or a small handful of fresh leaves, chopped 6 rashers of smoked streaky bacon, We’ve covered how to create 1 tsp dried thyme or a small handful of chopped 2 large onions, chopped ingredients or variables. fresh leaves, chopped Drizzle balsamic vinegar 3 garlic cloves, crushed 12-14 sun-dried tomato halves, in oil 1kg/2¼lb lean minced beef Salt and freshly ground black pepper 2 large glasses of red wine A good handful of fresh basil leaves, torn 2x400g cans chopped tomatoes into small pieces 1x290g jar antipasti marinated 800g-1kg/1¾-2¼lb dried spaghetti mushrooms, drained Lots of freshly grated parmesan, to serve 2 fresh or dried bay leaves1. 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. Now onto the basics of2. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop how to use those variables. 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 its 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 black http://www.bbc.co.uk/food/recipes/spaghettibolognese_67868
  62. 62. HOW TO MAKE A DECISION
  63. 63. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false).
  64. 64. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false). <
  65. 65. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false). less than <
  66. 66. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false). less than 7 < 4
  67. 67. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false). less than 7 < 4 FALSE
  68. 68. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false).
  69. 69. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false). >
  70. 70. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false). greater than >
  71. 71. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false). greater than 7 > 4
  72. 72. HOW TO MAKE A DECISIONOnce we have some variables, we want to do things with thosevariables.The most basic step is to compare those variables to somethingelse.This is done with comparators - questions that can be answeredyes or no (or true or false). greater than 7 > 4 TRUE
  73. 73. OTHER COMPARISONSgreater than or equal to equal to >= == less than or equal to not equal to <= All of these result != in true or false.
  74. 74. if ( ){}else {}
  75. 75. put in a comparison statement (like < or >)if ( ){}else {}
  76. 76. put in a comparison statement (like < or >)if ( ) what to do if our comparison{ statement is true}else {}
  77. 77. put in a comparison statement (like < or >)if ( ) what to do if our comparison{ statement is true}else {} what to do if our comparison statement is false
  78. 78. put in a comparison statement (like < or >) if ( ) what to do if our comparison { statement is true } we don’t have to always have an else else {statement, sometimes you only care if the } statement is true what to do if our comparison statement is false
  79. 79. If something is true, do an action.
  80. 80. If something is true, do an action.If something isn’t true, instead do adifferent action.
  81. 81. If something is true, do an action. If the potatoes are too lumpy, keep mashing.If something isn’t true, instead do adifferent action.
  82. 82. If something is true, do an action. If the potatoes are too lumpy, keep mashing.If something isn’t true, instead do adifferent action. If the potatoes are not too lumpy, stop mashing.
  83. 83. If something is true, do an action. If the potatoes are too lumpy, keep mashing. We don’t have to test this twice. We know ifIf something isn’t true, instead do a the potatoes are eitherdifferent action. too lumpy or not too lumpy. If the potatoes are not too lumpy, stop mashing.
  84. 84. A NOTE ON AXES When graphing information, we are used to numbers
  85. 85. A NOTE ON AXES When graphing information, we are used to numbers increasing as we move right
  86. 86. A NOTE ON AXES When graphing information, we are used to numbersand increasing as we move up increasing as we move right
  87. 87. A NOTE ON AXES This is different to how numbers work with programming graphics.
  88. 88. A NOTE ON AXES Numbers start in theupper left corner at the origin (0, 0) This is different to how numbers work with programming graphics.
  89. 89. A NOTE ON AXES Numbers start in theupper left corner at the origin (0, 0) This is different to how numbers work with programming graphics. and increase as we move right
  90. 90. A NOTE ON AXES Numbers start in theupper left corner at the origin (0, 0) This is different to how numbersand increase as we work with programming graphics. move down and increase as we move right
  91. 91. EXERCISECreate a Processing sketch which draws a green circle if themouse is in the top half of the window and changes the circle’scolour to red if the mouse in the bottom half of the window.Start with the code below. void setup() { // create the window size(400, 400); } void draw() { // set the colour fill(10, 10, 255); // draw the circle ellipse(mouseX, mouseY, 100, 100); }
  92. 92. What if we want to use multiple if statements? int counter; // some other code... if (counter < 10) { if (counter > 0 ) { counter++; } }
  93. 93. What if we want to use multiple if statements? If counter is less than 10 and greater than 0, then int counter; increase counter by 1. // some other code... if (counter < 10) { if (counter > 0 ) { counter++; } }
  94. 94. What if we want to use multiple if statements? If counter is less than 10 and greater than 0, then int counter; increase counter by 1. // some other code... if (counter < 10) { if (counter > 0 ) { counter++; } } counter is only increased if both if statements are true.
  95. 95. What if we want to use multiple if statements? If counter is less than 10 and greater than 0, then int counter; increase counter by 1. // some other code... if (counter < 10) { if (counter > 0 ) { counter++; } } counter is only increased ifThese are called nested if both if statements are true.statements, because one is inside the { } of the other.
  96. 96. What if we want to use multiple if statements? int counter; // some other code... if (counter > 10) { counter = 0; } if (counter < 0 ) { } counter = 0; }
  97. 97. What if we want to use multiple if statements? If counter is greater than 10 or less than 0, then reset counter to 0. int counter; // some other code... if (counter > 10) { counter = 0; } if (counter < 0 ) { } counter = 0; }
  98. 98. What if we want to use multiple if statements? If counter is greater than 10 or less than 0, then reset counter to 0. int counter; // some other code... if (counter > 10) { counter = 0; } if (counter < 0 ) { } counter = 0; } counter is reset if either if statements are true.
  99. 99. BOOLEAN OPERATOR ORTrue OR False isFalse OR True isTrue OR True isFalse OR False is
  100. 100. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True isTrue OR True isFalse OR False is
  101. 101. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True is TrueTrue OR True isFalse OR False is
  102. 102. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True is TrueTrue OR True is TrueFalse OR False is
  103. 103. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True is TrueTrue OR True is TrueFalse OR False is False
  104. 104. BOOLEAN OPERATOR ORTrue OR False is TrueFalse OR True is True When using OR in code, type ||True OR True is TrueFalse OR False is False
  105. 105. BOOLEAN OPERATOR AND True AND False is False AND True is True AND True is False AND False is
  106. 106. BOOLEAN OPERATOR AND True AND False is False False AND True is True AND True is False AND False is
  107. 107. BOOLEAN OPERATOR AND True AND False is False False AND True is False True AND True is False AND False is
  108. 108. BOOLEAN OPERATOR AND True AND False is False False AND True is False True AND True is True False AND False is
  109. 109. BOOLEAN OPERATOR AND True AND False is False False AND True is False True AND True is True False AND False is False
  110. 110. BOOLEAN OPERATOR AND True AND False is False False AND True is False When using OR in code, type && True AND True is True False AND False is False
  111. 111. BOOLEAN OPERATOR NOT NOT True is NOT False is
  112. 112. BOOLEAN OPERATOR NOT NOT True is False NOT False is
  113. 113. BOOLEAN OPERATOR NOT NOT True is False NOT False is True
  114. 114. BOOLEAN OPERATOR NOT NOT True is False NOT False is True When using NOT in code, type !
  115. 115. EXERCISEModify your Processing sketch which draws a green circle if themouse is in the top half of the window and changes the circle’scolour 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.
  116. 116. LOOPS
  117. 117. LOOPS There are two ways to repeat something:1. Do this N number of times.2. Keep doing this until something else happens.
  118. 118. LOOPS There are two ways to repeat something: Repeat this event in1. Do this N the calendar of number this times. many times.2. Keep doing this until something else happens.
  119. 119. LOOPS There are two ways to repeat something: Repeat this event in1. Do this N the calendar of number this times. many times.2. Keep doing this event in the Repeat this until calendar until a certain something else occurs. date happens.
  120. 120. DO THIS N TIMESint i;for(i=0; i<4; i++) {}
  121. 121. DO THIS N TIMESstart with a number, in this case 0 int i; for(i=0; i<4; i++) { }
  122. 122. DO THIS N TIMESstart with a number, in if this statement is this case 0 true int i; for(i=0; i<4; i++) { }
  123. 123. DO THIS N TIMESstart with a number, in if this statement is this case 0 true int i; for(i=0; i<4; i++) { } then do whatever is written here
  124. 124. DO THIS N TIMESstart with a number, in if this statement is this case 0 true int i; for(i=0; i<4; i++) { } when you’ve done what’s then do whatever is in the { } once, do this, in written here this case add make i equal to its current value plus 1
  125. 125. DO THIS N TIMESstart with a number, in if this statement is true go back to see if the this case 0 middle statement is still int i; true for(i=0; i<4; i++) { } when you’ve done what’s then do whatever is in the { } once, do this, in written here this case add make i equal to its current value plus 1
  126. 126. KEEP DOING THIS UNTILSOMETHING ELSE HAPPENS while ( ) { }
  127. 127. KEEP DOING THIS UNTILSOMETHING ELSE HAPPENS if the statement here is true while ( ) { }
  128. 128. KEEP DOING THIS UNTILSOMETHING ELSE HAPPENS if the statement here is true while ( ) { } then do what is between { } once
  129. 129. KEEP DOING THIS UNTIL SOMETHING ELSE HAPPENS if the statement here is true while ( ) {then repeat by checking the statement again } then do what is between { } once
  130. 130. EXERCISEWrite out each iteration of these loops and what the variablesequal at the end of each loop.int i;int j = 15; int k = 100;for (i=0; i<12; i++) while ( k > 0 ) {{ k = k -10; j = j * 2 - i; }}

×