Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Size: px
Start display at page:

Download "Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2"

Transcription

1 Loops Variable Scope Remapping Nested Loops CS Loops 1 Donald Judd CS Loops 2

2 judd while (expression) { statements CS Loops 3 Four Loop Questions 1. What do I want to repeat? - a rect 2. What do I want to change each time? - the y position of the rect 3. Where does it start, how does it change? - start at y = 10, draw a rect every 20 pixels 4. How long should it repeat? - until it reaches the bottom of the canvas CS Loops 4

3 Anatomy of a While Loop initialisation test expression update code block CS Loops 5 See 05 Loops (trace) CS Loops 6

4 Debugging Tracethrough CS Loops 7 draw() frames vs. while loop iterations the code block in draw is repeated every frame the code block in while is repeated every loop iteration frames loop iterations while (x < width) { CS Loops 8

5 What do you see after 5 frames? int a; background(200); a = 20; while (a < width) { ellipse(a, 50, 20, 20); a = a + 20; CS Loops 9 lines_rs draws lines randomly spaced across canvas width CS Loops 10

6 lines_es draws an exact number of lines spaced equally across canvas width notes: using width/height in variable assignment float vs. int margins CS Loops 11 lines_fs draws an exact number of lines with fixed spacing variations and notes: counter variable position variable (line location a function of counter) right to left variation CS Loops 12

7 Common Logic Errors Leading to Infinite Loops Adding a semicolon after the boolean expression means no code block. Without a code block, the loop variable won't update. x = 0; while (x < width); { line(x, 10, x, height - 10); x = x + 10; The loop variable is updated such that the loop condition will always be true. x = 0; while (x < width) { line(x, 10, x, height - 10); x = x - 10; The while statement will never end, and the program will freeze (infinite loop). Fix this by removing the semicolon after the boolean expression. The while statement will never end, and the program will freeze (infinite loop). Fix this by ensuring that your code eventually makes the loop repetition expression false. CS Loops 13 Debugging Loops Test a single loop operation first (without a loop) Simplify the loop operation Slow down draw frames with framerate(1) Use println x = 0; println("start loop"); // debug while (x < width) { println("loop", x); // debug line(x, 10, x, height - 10); x = x + 10; println("done loop"); // debug CS Loops 14

8 What do you see after 5 frames? int i; int x; background(200); i = 0; x = 20; while (i < 5) { ellipse(x, 50, 20, 20); i = i + 1; x = x + 20; CS Loops 15 gradient creates a grayscale gradient example of varying more than one variable CS Loops 16

9 (demos) convert while loop to for loop CS Loops 17 Assignment Operator Short Forms These all add 1 to x (they are all equivalent): x = x + 1; x += 1; x++; increment operator These both add 5 to x (they are both equivalent): x = x + 5; x += 5; Other examples // same as x = x + 10 * y; x += 10 * y; // same as x = x + random(-2, 2); x += random(-2, 2); CS Loops 18

10 More Assignment Operator Short Forms x--; decrement operator x -= 10; x *= 10; x /= 10; coding CS Loops 19 Anatomy of a For Loop initialization test expression update code block CS Loops 20

11 When to use for or while loop? for is a short form for while, and they re interchangeable. (You can always use while if you want) initialisation test expression update initialization test expression update CS Loops 21 When to use for or while loop? But sometimes one is easier to use in certain cases: Use for to loop an exact number of repetitions: - I want 10 pacmen - I want 3 lines - I want random(3,11) lines Use for to update by same amount: - I want to count by 10 from 0 to I want lines spaced 10 pixels apart the width of the canvas Use while to update by different amounts when you don t need to loop an exact number of repetitions: - I want lines spaced 2-10 pixels apart the width of the canvas CS Loops 22

12 Four Loop Questions 1. What do I want to repeat? - a line 2. What do I want to change each time? - the x position 3. Where does it start, how does it change? - start at x = 10, draw a line every 20 pixels 4. How long should it repeat? - I want to draw 7 lines Use for to loop an exact number of times CS Loops 23 lines_fs_for Use for to loop an exact number of times (a for loop version of lines_fs) // start line with margin from left x = spacing / 2; for (int i = 0; i < num; i++) { line(x, 10, x, height - 10); x += spacing; CS Loops 24

13 Four Loop Questions 1. What do I want to repeat? - a line 2. What do I want to change each time? - the x position 3. Where does it start, how does it change? - start at x = spacing/2, draw a line every spacing pixels 4. How long should it repeat? - I want to draw 11 lines (equally spaced) Use for to update by same amount CS Loops 25 lines_es_for Use for to update by same amount for (float x = spacing / 2; x < width; x += spacing) { line(x, 10, x, height - 10); CS Loops 26

14 Four Loop Questions 1. What do I want to repeat? - a line 2. What do I want to change each time? - the x position 3. Where does it start, how does it change? - start at x = 0, increment x randomly between 2 and 20 pixels 4. How long should it repeat? - I want to draw lines all the way across the canvas Use while to update by different amounts when you don t need to loop an exact number of repetitions CS Loops 27 lines_rs_for You can do this with for, but easier/clearer with while for (float x = 0; x < width; x += random (2, 20)) { line(x, 10, x, height - 10); CS Loops 28

15 What do you see after 1 frame? int i; int a; background(200); i = 0; while (i < ) { a = int(random(0, 100)); point(a, a); i++; CS Loops 29 Scope What parts of your code have access to a variable - i.e. what variables can a code statement "see" CREDIT: CS Loops 30

16 Global Scope We have been declaring variables outside of functions - These are called global variables Global variables can be used anywhere in a program after they are declared - They have global scope - They "remember" their value between calls to setup(), draw(), etc. - Built-in Processing variables are global e.g. width, height, framecount, mousex, key (see GS 52) CS Loops 31 Local Scope You can declare variables inside any code block - These are called local variables Local Variables can be used anywhere in that code block after they're declared recall, a code block is anything in between { and - functions have code blocks - loops have code blocks - (conditionals have code blocks) local variables cease to exist after the code block is exited - they have local scope CS Loops 32

17 lines_es_local background(200); // x is a local variable only visible to draw()! float x = spacing / 2; while (x < width) { line(x, 10, x, height - 10); x += spacing; CS Loops 33 Variable Scope Analogy global (remember it) local (use it then throw it out) global (remember it and update it) CS Loops 34

18 scope of w scope of y scope of h int w = 100; int y = w; void setup() { int h = w * 3; size(w, h); background(240); line(0, y, w, y); scope of n scope of px scope of py int n = int(random(0, 500)); for(int i = 0; i < n; i++) { float px = random(0, w); float py = random(0, y); point(px, py); y--; CS Loops 35 Computer Memory and Variables y int w = 100; int y = w; w 100 void setup() { int h = w * 3; size(w, h); mousex 100 background(240); line(0, y, w, y); 67 TWO_PI int n = int(random(0, 5 for(int i = 0; i < n; i float px = random(0, float py = random(0, point(px, CS py); Loops 36

19 Computer Memory and Variables y setup() variables int w = 100; int y = w; w 100 h void setup() { int h = w * 3; size(w, h); mousex background(240); line(0, y, w, y); 67 TWO_PI int n = int(random(0, 5 for(int i = 0; i < n; i float px = random(0, float py = random(0, point(px, CS py); Loops 37 Computer Memory and Variables y setup() variables int w = 100; int y = w; w 100 h void setup() { int h = w * 3; size(w, h); mousex background(240); line(0, y, w, y); 67 TWO_PI int n = int(random(0, 5 for(int i = 0; i < n; i float px = random(0, float py = random(0, point(px, CS py); Loops 38

20 When to use local variables When you don't need to "remember" the value after the code block completes - a loop variable - an intermediate calculation - to re-use a common value in a function call CS Loops 39 (local variable for intermediate calculation) local variables // calculate start and end angles float start = radians(dir mouthopening/2); float end = radians(dir + mouthopening/2)); // use those angles to draw the arc arc(width/2, height/2, mousex, mousex, start, end); CS Loops 40

21 (local variable for re-use) else if (shape == 3) { local variable // half size int hs = size / 2; triangle(mousex - hs, mousey + hs, mousex, mousey hs, mousex + hs, mousey + hs); CS Loops 41 (out of scope errors with local variables) cannot find variable named "hs" else if (shape == 3) { triangle(mousex - hs, mousey + hs, mousex, mousey hs, mousex + hs, mousey + hs); // half size int hs = size / 2; hs is defined after it is used CS Loops 42

22 (out of scope errors with local variables) int x = 0; void setup() { // local variable // only accessible to setup() int speed = 2; speed is defined as a local variable in setup() ellipse(x, 50, 20, 20); // this will cause an error: there is no // variable called 'speed' accessible to draw() x += speed; cannot find variable named "speed" CS Loops 43 (logic errors with local variables) int x; x is declared as a local variable and it will be assigned a default value of 0 each frame ellipse(x, 50, 20, 20); x += 2; the local variable x will be incremented, but then x will be thrown away as soon as draw finishes CS Loops 44

23 (logic errors with local variables) int x = 0; int x; x is declared as a local variable and it will "shadow" (hide) the global variable x. The local variable x will be assigned a default value of 0 each frame ellipse(x, 50, 20, 20); x += 2; the local variable x will be incremented, but then x will be thrown away as soon as draw finishes CS Loops 45 What does this loop output? int a = 2; void setup() { int a = 3; for (int i = 0; i < a; i++) { println("duck"); println("goose"); CS Loops 46

24 Changing Multiple Things in One Loop 1. What do I want to repeat? - a line 2. What do I want to change each time? - the y position and the stroke hue 3. Where does they start, how do they change? - start y at 0, increment by 1 - start hue at 0, increment by??? 4. How long should it repeat? - as long as y is less than the canvas height CS Loops 47 rainbow ideas: 1. [RESTRICTIVE] size(360, 100); 2. [HACK] colormode(hsb, height, 100, 100); 3. [GOOD] start hue at 0, increment so hue = 360 when y = height 4. [BEST] express hue as a function of y float vs. int, using float() conversion function or making constant values a float with.0 CS Loops 48

25 Remapping y- y height CS Loops 49 Remapping Different Scales ystart margin = margin y height ystop = height - margin height - margin CS Loops 50

26 rainbow draw rainbow with top and bottom margin CS Loops 51 Remapping Different Scales ystart margin = margin y ystop = height -margin height height - margin CS Loops 52

27 value2 = map(value1, start1, stop1, start2, stop2) value1 the incoming value to be converted start1 lower bound of the value's current range stop1 upper bound of the value's current range start2 lower bound of the value's target range stop2 upper bound of the value's target range CS Loops 53 rainbow_map remap variable using map() variables for y range and hue range: current range: ystart to ystop target range: huestart to huestart float hue = map(y, ystart, ystop, huestart, huestop); CS Loops 54

28 rainbow_interactive use map to adjust start and stop colours based on mousey nested conditional show interactive feedback use keys to change margin CS Loops 55 (demos) creating an infinite loop: typical causes: forgetting to change the counter variable changing the counter variable incorrectly using wrong test an infinite loop is a logical error CS Loops 56

29 precision (from "04 Conditionals") float a = 0; void setup() { framerate(2); a += 0.1; if (a == 5) { background(255, 0, 0); // watch the console output! println(a); CS Loops 57 infinite loop due to float precision float a = 0; void setup() { framerate(2); // infinite loop while (a!= 5) { // watch the console output! println(a); a += 0.1; CS Loops 58

30 when to use int? when to use float? use int to count things (e.g. loop 10 times) use int to index things (e.g. array positions) use int if you want to check for equality (e.g. i == 10) use int if you don t care about decimals (e.g. pixel position) otherwise use float CS Loops 59 What does this code output? void setup() { int i = 0; for (int y = 0; y < 2; y++) { i = i + 1; for (int x = 0; x < 3; x++) { i = i + 1; print(i); CS Loops 60

31 spraypaint // draw many dots around the cursor stroke(0, 10); strokeweight(1); // typical "counting" loop for (int i = 0; i < dots; i++) { // local variables to make code easier to read int x = mousex + int(random(-spread, spread)); int y = mousey + int(random(-spread, spread)); point(x, y); CS Loops 61 hittest_loop int x = 0; while (x + size < width) { // the hit test if (mousex >= x && mousex <= x + size && mousey >= y && mousey <= y + size) { fill(#ff0000); // red else { fill(255); // draw the rectangle at the current position rect(x, y, size, size); x += size; CS Loops 62

32 Nested Loop outer code block inner code block CS Loops 63 nestedloop int a = 0; // count the cells int s = 25; // size of each cell // row for (int i = 0; i < 4; i++) { // column for (int j = 0; j < 3; j++) { // calculate coordinates based on cell int x = i * s; int y = j * s; // draw something point(x, y); text(a, x, y); a = a + 1; CS Loops 64

33 Tracethrough tracethrough using the Processing debugger CS Loops 65 Nested Loop Clock Analogy for (int h = 0; h < 24; h++) { for (int m = 0; m < 60; m++) { for (int s = 0; s < 60; s++ ) { println(h, m, s); CS Loops 66

34 nestedloop_columns void setup() { size(300, 300); colormode(hsb, 360, 100, 100, 100); // row for (int x = 0; x < width; x += 20) { // pick a hue for the column float hue = map(x, 0, width, 360, 0); // column for (int y = 0; y <= height; y += 40) { fill(hue, 80, 80); rect(x, y, 20, 40); CS Loops 67 nestedloop_rows // nested loop coloured rows void setup() { size(300, 300); colormode(hsb, 360, 100, 100, 100); // column for (int y = 0; y <= height; y += 40) { // pick a hue for the row float hue = map(y, 0, height, 360, 0); // row for (int x = 0; x < width; x += 20) { fill(hue, 80, 80); rect(x, y, 20, 40); CS Loops 68

35 What does this code output? void setup() { int i = 0; for (int y = 0; y < 2; y++) { for (int x = 0; x < 3; x++) { i++; print(i); CS Loops 69 hittest_nestedloop using two while loops CS Loops 70

36 randomnestedloop random number of loop iterations int conversion example loop from bottom of canvas to top more map() examples CS Loops 71

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1 Conditionals Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary CS105 04 Conditionals 1 Pick a number CS105 04 Conditionals 2 Boolean Expressions An expression that

More information

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values Functions parameters return values 06 Functions 1 Functions Code that is packaged so it can be run by name Often has parameters to change how the function works (but not always) Often performs some computation

More information

Bits and Bytes. How do computers compute?

Bits and Bytes. How do computers compute? Bits and Bytes How do computers compute? Representing Data All data can be represented with: 1s and 0s on/of true/false Numbers? Five volunteers... Binary Numbers Positional Notation Binary numbers use

More information

CISC 1600 Lecture 3.1 Introduction to Processing

CISC 1600 Lecture 3.1 Introduction to Processing CISC 1600 Lecture 3.1 Introduction to Processing Topics: Example sketches Drawing functions in Processing Colors in Processing General Processing syntax Processing is for sketching Designed to allow artists

More information

Interaction Design A.A. 2017/2018

Interaction Design A.A. 2017/2018 Corso di Laurea Magistrale in Design, Comunicazione Visiva e Multimediale - Sapienza Università di Roma Interaction Design A.A. 2017/2018 8 Loops and Arrays in Processing Francesco Leotta, Andrea Marrella

More information

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca final float MAX_SPEED = 10; final float BALL_SIZE = 5; void setup() { size(500, 500); void draw() { stroke(255); fill(255);

More information

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work. MIDTERM REVIEW: THURSDAY I KNOW WHAT I WANT TO REVIEW. BUT ALSO I WOULD LIKE YOU TO TELL ME WHAT YOU MOST NEED TO GO OVER FOR MIDTERM. BY EMAIL AFTER TODAY S CLASS. What can we do with Processing? Let

More information

A B C D CS105 03a Interaction

A B C D CS105 03a Interaction Interaction Function Definition Events Built-in Variables CS105 03a Interaction 1 Which image is drawn by this code? strokeweight(10); stroke(0, 255, 0); // green line(99, 0, 0, 99); stroke(200, 0, 200);

More information

1. Complete these exercises to practice creating user functions in small sketches.

1. Complete these exercises to practice creating user functions in small sketches. Lab 6 Due: Fri, Nov 4, 9 AM Consult the Standard Lab Instructions on LEARN for explanations of Lab Days ( D1, D2, D3 ), the Processing Language and IDE, and Saving and Submitting. Rules: Do not use the

More information

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created. + Inheritance + Inheritance Classes that we design in Java can be used to model some concept in our program. For example: Pokemon a = new Pokemon(); Pokemon b = new Pokemon() Sometimes we need to create

More information

What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username

What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username What is a variable? a named location in the computer s memory mousex mousey width height fontcolor username Variables store/remember values can be changed must be declared to store a particular kind of

More information

Repetition is the reality and the seriousness of life. Soren Kierkegaard

Repetition is the reality and the seriousness of life. Soren Kierkegaard 6 Loops Loops 81 Repetition is the reality and the seriousness of life. Soren Kierkegaard What s the key to comedy? Repetition. What s the key to comedy? Repetition. Anonymous In this chapter: The concept

More information

Design Programming DECO2011

Design Programming DECO2011 Design Programming DECO2011 Rob Saunders web: http://www.arch.usyd.edu.au/~rob e-mail: rob@arch.usyd.edu.au office: Room 274, Wilkinson Building Data, Variables and Flow Control What is a Variable? Computers

More information

Iteration in Programming

Iteration in Programming Iteration in Programming for loops Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list There are three types of loop in programming: While

More information

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Spring 2017 Howard Rosenthal Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when

More information

if / if else statements

if / if else statements if / if else statements December 1 2 3 4 5 Go over if notes and samples 8 9 10 11 12 Conditionals Quiz Conditionals TEST 15 16 17 18 19 1 7:30 8:21 2 8:27 9:18 3 9:24 10:14 1 CLASS 7:30 8:18 1 FINAL 8:24

More information

CST112 Looping Statements Page 1

CST112 Looping Statements Page 1 CST112 Looping Statements Page 1 1 2 3 4 5 Processing: Looping Statements CST112 Algorithms Procedure for solving problem: 1. Actions to be executed 2. Order in which actions are executed order of elements

More information

Name CMPS 5J Final March 17, 2010 This is a closed notes, closed book exam. Each problem is worth 1 point unless indicated otherwise.

Name CMPS 5J Final March 17, 2010 This is a closed notes, closed book exam. Each problem is worth 1 point unless indicated otherwise. Name CMPS 5J Final March 17, 2010 This is a closed notes, closed book exam. Each problem is worth 1 point unless indicated otherwise. There are 21 problems and 25 points total. There are multiple versions

More information

Chapter 5. Condi.onals

Chapter 5. Condi.onals Chapter 5 Condi.onals Making Decisions If you wish to defrost, press the defrost bu=on; otherwise press the full power bu=on. Let the dough rise in a warm place un.l it has doubled in size. If the ball

More information

Repetition and Loop Statements Chapter 5

Repetition and Loop Statements Chapter 5 Repetition and Loop Statements Chapter 5 1 Chapter Objectives To understand why repetition is an important control structure in programming To learn about loop control variables and the three steps needed

More information

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Computer Programming Basic Control Flow - Loops Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons Objectives To learn about the three types of loops: while for do To avoid infinite

More information

Variables One More (but not the last) Time with feeling

Variables One More (but not the last) Time with feeling 1 One More (but not the last) Time with feeling All variables have the following in common: a name a type ( int, float, ) a value an owner We can describe variables in terms of: who owns them ( Processing

More information

Final Exam Winter 2013

Final Exam Winter 2013 Final Exam Winter 2013 1. Which modification to the following program makes it so that the display shows just a single circle at the location of the mouse. The circle should move to follow the mouse but

More information

This unit introduces the basics of trigonometry and how to utilize it for generating form.

This unit introduces the basics of trigonometry and how to utilize it for generating form. Math 3: Trigonometry This unit introduces the basics of trigonometry and how to utilize it for generating form. Syntax introduced: PI, QUARTER_PI, HALF_PI, TWO_PI, radians(), degrees() sin(), cos(), arc()

More information

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

More information

Scope of this lecture. Repetition For loops While loops

Scope of this lecture. Repetition For loops While loops REPETITION CITS1001 2 Scope of this lecture Repetition For loops While loops Repetition Computers are good at repetition We have already seen the for each loop The for loop is a more general loop form

More information

Variables. location where in memory is the information stored type what sort of information is stored in that memory

Variables. location where in memory is the information stored type what sort of information is stored in that memory Variables Processing, like many programming languages, uses variables to store information Variables are stored in computer memory with certain attributes location where in memory is the information stored

More information

We will start our journey into Processing with creating static images using commands available in Processing:

We will start our journey into Processing with creating static images using commands available in Processing: Processing Notes Chapter 1: Starting Out We will start our journey into Processing with creating static images using commands available in Processing: rect( ) line ( ) ellipse() triangle() NOTE: to find

More information

CISC 1600, Lab 3.1: Processing

CISC 1600, Lab 3.1: Processing CISC 1600, Lab 3.1: Processing Prof Michael Mandel 1 Getting set up For this lab, we will be using OpenProcessing, a site for building processing sketches online using processing.js. 1.1. Go to https://www.openprocessing.org/class/57767/

More information

An Introduction to Processing

An Introduction to Processing An Introduction to Processing Creating static drawings Produced by: Mairead Meagher Dr. Siobhán Drohan Department of Computing and Mathematics http://www.wit.ie/ Topics list Coordinate System in Computing.

More information

Variables and Control Structures. CS 110 Eric Eaton

Variables and Control Structures. CS 110 Eric Eaton Variables and Control Structures CS 110 Eric Eaton Review Random numbers mousex, mousey setup() & draw() framerate(), loop(), noloop() Mouse and Keyboard interaccon Arcs, curves, bézier curves, custom

More information

2D Shapes. Creative Coding & Generative Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar

2D Shapes. Creative Coding & Generative Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar 2D Shapes Creative Coding & Generative Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar Did you do this? Read Chapter 2 (pages 33-50) Read and do the Coordinate Systems & Shapes and Color tutorials

More information

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator. Chapter 5: Looping 5.1 The Increment and Decrement Operators Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Addison-Wesley Pearson Education, Inc. Publishing as Pearson Addison-Wesley

More information

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut dthiebaut@smith.edu Important Review Does the animation leave a trace? Are the moving objects move without a

More information

For Loop. Variations on Format & Specific Examples

For Loop. Variations on Format & Specific Examples For Loop The for loop is an iterative loop. You determine how many times it executes, using 3 expressions and a loop control variable (lcv). The first expression initializes the loop control variable (lcv)

More information

CS 101 Functions. Lecture 15

CS 101 Functions. Lecture 15 CS 101 Functions Lecture 15 1 Key Processing language features so-far Basic color/shapes drawing Variables For-loops If-statements 2 Functions In the next few days, we ll be talking about Functions A Function

More information

Chapter 5: Loops and Files

Chapter 5: Loops and Files Chapter 5: Loops and Files 5.1 The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1;

More information

Chapter Goals. Contents LOOPS

Chapter Goals. Contents LOOPS CHAPTER 4 LOOPS Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30, 2011 Chapter Goals To implement while, for, and do loops To hand-trace the execution of a program To become familiar with common

More information

Object-Oriented Programming in Java

Object-Oriented Programming in Java CSCI/CMPE 3326 Object-Oriented Programming in Java Loop Statements Dongchul Kim Department of Computer Science University of Texas Rio Grande Valley The Increment and Decrement Operators There are numerous

More information

CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions

CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions The exam will be closed-note and closed-book; please consider this fact before using your notes on this practice version. Please see

More information

CISC 1600, Lab 2.1: Processing

CISC 1600, Lab 2.1: Processing CISC 1600, Lab 2.1: Processing Prof Michael Mandel 1 Getting set up For this lab, we will be using Sketchpad, a site for building processing sketches online using processing.js. 1.1. Go to http://cisc1600.sketchpad.cc

More information

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false 5 Conditionals Conditionals 59 That language is an instrument of human reason, and not merely a medium for the expression of thought, is a truth generally admitted. George Boole The way I feel about music

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

Basic Computer Programming (Processing)

Basic Computer Programming (Processing) Contents 1. Basic Concepts (Page 2) 2. Processing (Page 2) 3. Statements and Comments (Page 6) 4. Variables (Page 7) 5. Setup and Draw (Page 8) 6. Data Types (Page 9) 7. Mouse Function (Page 10) 8. Keyboard

More information

Recall that creating or declaring a variable can be done as follows:

Recall that creating or declaring a variable can be done as follows: Lesson 2: & Conditionals Recall that creating or declaring a variable can be done as follows:! float radius = 20;! int counter = 5;! string name = Mr. Nickel ;! boolean ispressed = true;! char grade =

More information

CS110 Introduction to Computing Fall 2016 Practice Exam 1

CS110 Introduction to Computing Fall 2016 Practice Exam 1 CS110 Introduction to Computing Fall 2016 Practice Exam 1 The exam will be closed-note and closed-book; please consider this fact before using your notes on this practice version. Please see the abbreviated

More information

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn CPS109 Course Notes 6 Alexander Ferworn Unrelated Facts Worth Remembering Use metaphors to understand issues and explain them to others. Look up what metaphor means. Table of Contents Contents 1 ITERATION...

More information

Interaction Design A.A. 2017/2018

Interaction Design A.A. 2017/2018 Corso di Laurea Magistrale in Design, Comunicazione Visiva e Multimediale - Sapienza Università di Roma Interaction Design A.A. 2017/2018 7 Conditionals in Processing Francesco Leotta, Andrea Marrella

More information

Introduction to Processing. Sally Kong

Introduction to Processing. Sally Kong Introduction to Processing Sally Kong - Open Source Platform - Geared toward creating visual, interactive media - Created by Ben Fry and Casey Reas Basic Setup void setup() { size(800, 600); background(255);

More information

STUDENT LESSON A12 Iterations

STUDENT LESSON A12 Iterations STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of

More information

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

L o o p s. for(initializing expression; control expression; step expression) { one or more statements } L o o p s Objective #1: Explain the importance of loops in programs. In order to write a non trivial computer program, you almost always need to use one or more loops. Loops allow your program to repeat

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1 Chapter 5: Loops and Files The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++; is the same as val = val + 1; ++ can be used before (prefix) or after (postfix)

More information

Processing Assignment Write- Ups

Processing Assignment Write- Ups Processing Assignment Write- Ups Exercise 1-1 Processing is not an elaborate series of points like connect the dots or is it? Can t be cause I got it all wrong when I mapped out each and every point that

More information

Methods (cont.) Chapter 7

Methods (cont.) Chapter 7 Methods (cont.) Chapter 7 Defining Simple Methods ReturnType Iden&fier ( ParameterList ) { Body ReturnType is the type of value returned from the method/funcbon. IdenBfier is the name of the method/funcbon.

More information

Repetition CSC 121 Fall 2014 Howard Rosenthal

Repetition CSC 121 Fall 2014 Howard Rosenthal Repetition CSC 121 Fall 2014 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while

More information

LECTURE 5 Control Structures Part 2

LECTURE 5 Control Structures Part 2 LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

More information

Flow of Control: Loops

Flow of Control: Loops Walter Savitch Frank M. Carrano Flow of Control: Loops Chapter 4 Java Loop Statements: Outline The while statement The do-while statement The for Statement Java Loop Statements A portion of a program that

More information

1 Getting started with Processing

1 Getting started with Processing cisc3665, fall 2011, lab I.1 / prof sklar. 1 Getting started with Processing Processing is a sketch programming tool designed for use by non-technical people (e.g., artists, designers, musicians). For

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

Learning outcomes. COMPSCI 101 Principles of Programming. Drawing 2D shapes using Characters. Printing a Row of characters

Learning outcomes. COMPSCI 101 Principles of Programming. Drawing 2D shapes using Characters. Printing a Row of characters Learning outcomes At the end of this lecture, students should be able to draw 2D shapes using characters draw 2D shapes on a Canvas COMPSCI 101 Principles of Programming Lecture 25 - Using the Canvas widget

More information

Class #1. introduction, functions, variables, conditionals

Class #1. introduction, functions, variables, conditionals Class #1 introduction, functions, variables, conditionals what is processing hello world tour of the grounds functions,expressions, statements console/debugging drawing data types and variables decisions

More information

Module 01 Processing Recap

Module 01 Processing Recap Module 01 Processing Recap Processing is a language a library an environment Variables A variable is a named value. It has a type (which can t change) and a current value (which can change). Variables

More information

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca COMP 1010- Summer 2015 (A01) Jim (James) Young young@cs.umanitoba.ca jimyoung.ca order of operations with the explicit cast! int integervariable = (int)0.5*3.0; Casts happen first! the cast converts the

More information

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada Simple Java YEAH Hours Brahm Capoor and Vrinda Vasavada What are YEAH hours? Held soon after each assignment is released Help you to get an early start on your assignments Future dates TBA Slides will

More information

[ the academy_of_code] Senior Beginners

[ the academy_of_code] Senior Beginners [ the academy_of_code] Senior Beginners 1 Drawing Circles First step open Processing Open Processing by clicking on the Processing icon (that s the white P on the blue background your teacher will tell

More information

PROGRAMMING. We create identity

PROGRAMMING. We create identity 2 PROGRAMMING We create identity TOPICS COVERED Getting Started Setup and draw Variables mousex, mousey other system variables variable declaration types Arithmetic Predefined methods abs, max, delay Topics

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science For loops, nested loops and scopes Jordi Cortadella Department of Computer Science Outline For loops Scopes Nested loops Introduction to Programming Dept. CS, UPC 2 Calculate x y Algorithm: repeated multiplication

More information

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1 Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1 Chapter 6 : (Control Structure- Repetition) Using Decrement or Increment While Loop Do-While Loop FOR Loop Nested Loop

More information

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting Unit 2, Part 2 Definite Loops Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting Let's say that we're using a variable i to count the number of times that

More information

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue

More information

Programming for Engineers Iteration

Programming for Engineers Iteration Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

Appendix: Common Errors

Appendix: Common Errors Appendix: Common Errors Appendix 439 This appendix offers a brief overview of common errors that occur in Processing, what those errors mean and why they occur. The language of error messages can often

More information

Repetition, Looping CS101

Repetition, Looping CS101 Repetition, Looping CS101 Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions

More information

What is a variable? A named locajon in the computer s memory. A variable stores values

What is a variable? A named locajon in the computer s memory. A variable stores values Chapter 4 Summary/review coordinate system basic drawing commands and their parameters (rect, line, ellipse,background, stroke, fill) color model - RGB + alpha Processing IDE - entering/saving/running

More information

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1. Programming Fundamentals Welcome! 0 COMP1511 18s1 Programming Fundamentals COMP1511 18s1 Lecture 5 1 More Loops Andrew Bennett while loops loops inside loops stopping loops 2 Before we begin introduce

More information

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017 Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The

More information

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x Outline For loops, nested loops and scopes For loops Scopes Jordi Cortadella Department of Computer Science Nested loops Calculate x y Algorithm: repeated multiplication x x x x y times y x i p=x i 4 3

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 5 The Loop-the-Loop ( 2.3-2.4) 9/21/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline 1. For-loops! 2. Algorithm Design and Pseudocode 9/21/2011

More information

Module 01 Processing Recap. CS 106 Winter 2018

Module 01 Processing Recap. CS 106 Winter 2018 Module 01 Processing Recap CS 106 Winter 2018 Processing is a language a library an environment Variables A variable is a named value. It has a type (which can t change) and a current value (which can

More information

University of Cincinnati. P5.JS: Getting Started. p5.js

University of Cincinnati. P5.JS: Getting Started. p5.js p5.js P5.JS: Getting Started Matthew Wizinsky University of Cincinnati School of Design HTML + CSS + P5.js File Handling & Management Environment Canvas Coordinates Syntax Drawing Variables Mouse Position

More information

Chapter 4: Control structures. Repetition

Chapter 4: Control structures. Repetition Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

Lecture 7. Processing Development Environment (or PDE)

Lecture 7. Processing Development Environment (or PDE) Lecture 7 Processing Development Environment (or PDE) Processing Class Overview What is Processing? Installation and Intro. Serial Comm. from Arduino to Processing Drawing a dot & controlling position

More information

Control Statements Review CSC 123 Fall 2018 Howard Rosenthal

Control Statements Review CSC 123 Fall 2018 Howard Rosenthal Control Statements Review CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Review some of Java s control structures Review how to control the flow of a program via selection mechanisms Understand if, if-else,

More information

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013 While Loops A loop performs an iteration or repetition A while loop is the simplest form of a loop Occurs when a condition is true CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby

More information

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration) Week 7: Advanced Loops while Loops in C++ (review) while (expression) may be a compound (a block: {s) Gaddis: 5.7-12 CS 1428 Fall 2015 Jill Seaman 1 for if expression is true, is executed, repeat equivalent

More information

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while

More information

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing SECTION 5: STRUCTURED PROGRAMMING IN MATLAB ENGR 112 Introduction to Engineering Computing 2 Conditional Statements if statements if else statements Logical and relational operators switch case statements

More information

Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools

Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools Outline CIS 110: Introduction to Computer Programming 1. For-loops! 2. Algorithm Design and Pseudocode Lecture 5 The Loop-the-Loop ( 2.3-2.4) 1 2 Announcements Date of the final is tentatively: MONDAY,

More information

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators Chapter 5: 5.1 Looping The Increment and Decrement Operators The Increment and Decrement Operators The Increment and Decrement Operators ++ is the increment operator. It adds one to a variable. val++;

More information

Chapter 4: Control structures

Chapter 4: Control structures Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

Example: Monte Carlo Simulation 1

Example: Monte Carlo Simulation 1 Example: Monte Carlo Simulation 1 Write a program which conducts a Monte Carlo simulation to estimate π. 1 See https://en.wikipedia.org/wiki/monte_carlo_method. Zheng-Liang Lu Java Programming 133 / 149

More information

Watch the following for more announcements

Watch the following for more announcements Review "plain text file" loadstrings() split() splittokens() selectinput() println(), float(), int(), can take an array argument, will return an array easy way to convert an array of Strings to an array

More information

CSE120 Wi18 Final Review

CSE120 Wi18 Final Review CSE120 Wi18 Final Review Practice Question Solutions 1. True or false? Looping is necessary for complex programs. Briefly explain. False. Many loops can be explicitly written out as individual statements

More information

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition 5.2 Q1: Counter-controlled repetition requires a. A control variable and initial value. b. A control variable

More information

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved. Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information