© Calvin College, 2009 1 For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the.

Slides:



Advertisements
Similar presentations
Variables Conditionals Boolean Expressions Conditional Statements How a program produces different results based on varying circumstances if, else if,
Advertisements

Introduction to Programming
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
A Quick Introduction to Processing
Recursion October 5, Reading Read pp in the text.
© Calvin College, Being abstract is something profoundly different from being vague... The purpose of abstraction is not to be vague, but to create.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
PROCESSING Animation. Objectives Be able to create Processing animations Be able to create interactive Processing programs.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
PROCESSING Designing and developing a program. Objectives Understand programming as the process of writing computer instructions that achieve a goal Understand.
 2003 Prentice Hall, Inc. All rights reserved. 1 Dijkstra Letter Quotes...the quality of programmers is a decreasing function of the density of go to.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Introduction CS 104: Applied C++ What is Programming? For some given problem: __________ a solution for it -- identify, organize & store the problem's.
PROCESSING & JAVA An Introduction to Computing. Objectives Be able to state and apply phases of development Be able to state and describe the goals of.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
1 CPSC 185 Introduction to Computing The course home page
PROCESSING Methods. Objectives Be able to define methods that return values Be able to define methods that do not return values Be able to use random.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
© Calvin College, What is object-oriented programming? My guess is that object-oriented programming will be in the 1980’s what structured programming.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
sequence of execution of high-level statements
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Review Loops – Condition – Index Functions – Definition – Call – Parameters – Return value.
CS 112 Introduction to Programming Graphics; Animation Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
© Calvin College, When we prepare a program, the experience can be just like composing poetry or music … My claim is that it is possible to write.
Spring 2012 CS 214 Programming Languages. Details Moodle! REQUIRED text: Sebesta, Programming Language Concepts, 9ed. Important dates: February 17: Project.
CIS 3.5 Lecture 2.2 More programming with "Processing"
2-D Shapes, Color, and simple animation. 7 Basic Shapes Ellipse :: ellipse() Arc :: arc() Line :: line() Point :: point() Rectangle :: rect() Triangle.
Mouse Inputs in Processing. Interacting with the Mouse mouseX and mouseY: pg mouseXmouseY –The position of the mouse in the canvas pmouseX and.
Lesson Two: Everything You Need to Know
Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --
Often being different. Control flow By default Java (and therefore Processing) executes lines of a program one after the other –Doesn’t matter what happened.
1 Controlling Behavior Chap.5 Study Sections 5.1 – 5.3 The if and for Statements.
Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Computer Science I Looping. User input. Classwork/Homework: Incorporate looping & user input into a sketch.
G RAPHICS & I NTERACTIVE P ROGRAMMING Lecture 2 More Programming with Processing.
Calvin College Controlling Behavior The if, switch and for Statements.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
B. RAMAMURTHY Processing and Programming cse
Review Expressions and operators Iteration – while-loop – for-loop.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:
Machine-Level Programming 2 Control Flow
Def: A control structure is a control statement and
Conditional Branch Example
Chapter 14, Translate & Rotate
Recitation 2 – 2/4/01 Outline Machine Model
Counted Loops.
20 minutes maximum exhibits
Array Data Structure and Processing Arrays
Chapter 6 Loops (iteration).
Machine-Level Programming 2 Control Flow
For Net Art Lecture 2 J Parker
Programming for Artists
Chapter 10 Algorithms.
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
Machine-Level Representation of Programs III
Machine-Level Programming 2 Control Flow
More programming with "Processing"
Structured Programming
Loops & Nested Loops CSE 120 Winter 2019
Chapter 10 Algorithms.
Trigonometry & Random March 2, 2010.
How About Some PI? Trigonometry Feb 18,2009.
Presentation transcript:

© Calvin College, For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the density of go to statements in the programs they produce. More recently I discovered why the use of the go to statement has such disastrous effects, and I became convinced that the go to statement should be abolished from all "higher level" programming languages … - Edsger W. Dijkstra, “Go To Statement Considered Harmful”, CACM, 1968“Go To Statement Considered Harmful”

© Calvin College, Control Structures ● Motivation ● Control Structures: Control Structures – Sequence Sequence – Selection Selection – Repetition Repetition ● Animation and Repetition Animation and Repetition ● Structured Programming Structured Programming

© Calvin College, Introduction ● Von Neumann architectures execute statements one at a time. ● The sequence of this execution is called the control flow of a program. ● Control structures specify the nature of this flow.

© Calvin College, Sequential Execution Processing executes blocked statements sequentially: { statement 1 statement 2... statement N } statement 1 statement 2 statement n

© Calvin College, Example: Design (1) ● We start with the restricted goal of drawing an ellipse and its center point. ● Algorithm: – Data: x, y, diameter, radius – Setup: 1. Setup 300x300 panel with white background 2. Initialize x, y, diameter and radius – Draw: 1. Draw an circle at x, y with diameter. 2. Draw the center point.

© Calvin College, Example: Implementation (1) ● Relevant implementation tools: – sequential execution

© Calvin College, Example: Testing (1) ● The resulting output should show an ellipse with its center point.

© Calvin College, final int WIDTH = 300, HEIGHT = WIDTH, ellipseX, ellipseY, ellipseDiameter; void setup() { size(WIDTH, HEIGHT); smooth(); ellipseX = WIDTH / 2; ellipseY = HEIGHT / 2; ellipseDiameter = 100; } void draw() { background(255); strokeWeight(2); ellipse(ellipseX, ellipseY, ellipseDiameter, ellipseDiameter); strokeWeight(5); point(ellipseX, ellipseY); }

© Calvin College, Selective Execution if statements execute their statements selectively, based on a boolean condition: if (condition) statement 1

© Calvin College, Selection: The Simple if The if statement has different forms. The first form is called the simple if: if (condition) statement condition statement T F

© Calvin College, Iteration 2 ● Analysis ● Design ● Implementation ● Test

© Calvin College, void mouseClicked() { if (dist(mouseX, mouseY, ellipseX, ellipseY) <= ellipseRadius) { fillColor = color(random(255), random(255), random(255)); }

© Calvin College, Selection: The Two-Branch if if (condition) statement 1 else statement 2 condition statement 1 T F statement 2 The second form of if statement includes an else clause and a second statement:

© Calvin College, Iteration 3 ● Analysis ● Design ● Implementation ● Test

© Calvin College, void mouseClicked() { if (dist(mouseX, mouseY, ellipseX, ellipseY) <= ellipseRadius) { fillColor = color(random(255), random(255), random(255)); } else { backgroundColor = color(random(255), random(255), random(255)); }

© Calvin College, Selection: The Multi-branch if The final form nests the if statements: if (Cond 1 ) { Stmts 1 } else if (Cond 2 ) { Stmts 2... } else if (Cond N ) { Stmts N } else { Stmts N+1 } Stmts N+1 Cond 1 Stmts 1 TF Stmts 2 Cond 2 TF Stmts N Cond N TF...

© Calvin College, Iteration 4 ● Analysis ● Design ● Implementation ● Test

© Calvin College, void mouseClicked() { if (dist(mouseX, mouseY, ellipseX, ellipseY) <= ellipse2Radius){ ellipse2Color = a random color; } else if (dist(mouseX, mouseY, ellipseX, ellipseY) <= ellipseRadius){ ellipseColor = a random color; } else { backgroundColor = a random color; }

© Calvin College, int width = 300, height = width; int ellipseX, ellipseY, ellipse1Diameter, ellipse1Radius, ellipse2Diameter, ellipse2Radius; color ellipse1Color, ellipse2Color, backgroundColor; void setup() { size(width, height); smooth(); ellipseX = width / 2; ellipseY = height / 2; ellipse1Diameter = 150; ellipse1Radius = ellipse1Diameter / 2; ellipse2Diameter = 75; ellipse2Radius = ellipse2Diameter / 2; ellipse1Color = color(255); ellipse2Color = color(255); backgroundColor = color(255); } void draw() { background(backgroundColor); strokeWeight(2); fill(ellipse1Color); ellipse(ellipseX, ellipseY, ellipse1Diameter, ellipse1Diameter); fill(ellipse2Color); ellipse(ellipseX, ellipseY, ellipse2Diameter, ellipse2Diameter); strokeWeight(5); point(ellipseX, ellipseY); } Continued

© Calvin College, void mouseClicked() { if (dist(mouseX, mouseY, ellipseX, ellipseY) <= ellipse2Radius) { ellipse2Color = color(random(255), random(255), random(255)); } else if (dist(mouseX, mouseY, ellipseX, ellipseY) <= ellipse1Radius) { ellipse1Color = color(random(255), random(255), random(255)); } else { backgroundColor = color(random(255), random(255), random(255)); }

© Calvin College, Selection: Issues

© Calvin College, Repetitive Execution Repetition statements execute their statements repeatedly, usually based on a Boolean condition.

© Calvin College, Repetition: The for Loop for (initExpr; loopCond; incrExpr) Statement InitExpr LoopCond Statement IncrExpr F T The counting for statement repeats based on a counter.

© Calvin College, The for statement is normally used to count things. Repetition: The Counting Loop for (int count = first; count <= last; count++) Statement Example: for (int i = 1; i <= 5; i++) { println(i); }

© Calvin College, 2009 Example 25

© Calvin College, Iteration 5 ● Analysis ● Design ● Implementation ● Test

© Calvin College, final int COUNT = 13; void setup() { size(500, 250); noLoop(); } void draw() { for (int i = 0; i < COUNT; i++) { rect(0, i * height/COUNT, width - 1, height/COUNT); }

© Calvin College, Iteration 6 ● Analysis ● Design ● Implementation ● Test

© Calvin College, void draw() { for (int i = 0; i < COUNT; i++) { if (i % 2 == 0) { fill(191, 10, 48); } else { fill(255); } rect(0, i * height/COUNT, width-1, height/COUNT); }

© Calvin College, Repetition: Nested for Loops for (initExpr 1 ; loopCond 1 ; incrExpr 1 ) for (initExpr 2 ; loopCond 2 ; incrExpr 2 ) Statement InitExpr 1 LoopCond 1 IncrExpr 1 F T Loops can be nested as well. InitExpr 2 LoopCond 2 Statement IncrExpr 2 F T

© Calvin College, Iteration 7 ● Analysis ● Design ● Implementation ● Test

© Calvin College, fill(0,40,104); rect(0, 0, 190, 134); stroke(255); strokeWeight(5); for (int row = 0; row < 9; row++ ) { for (int column = 0; column < 11; column++) { if ((row%2==0 && column%2==0) || (row%2!=0 && column%2!=0)) { point(column*190/ , row*134/9 + 5); }

© Calvin College, /** * USA draws a properly scaled US flag (see * kvlinden 28april2009 */ int unit=250; float height = unit, width = height*1.9; float unionHeight, unionWidth, stripeHeight, starDiameter; PImage star; void setup() { size((int)width,(int)height); background(191,10,48); fill(255); smooth(); noStroke(); noLoop(); unionHeight=height*7/13; unionWidth=width*2/5; stripeHeight=height/13; starDiameter=height*0.0616; star = loadImage("whiteStar100.gif"); imageMode(CENTER); } Continued

© Calvin College, void draw() { // Draw stripes. for (int i=1; i<13; i+=2) { rect(0,i*(int)stripeHeight, (int)width,(int)stripeHeight); } // Draw stars. fill(0,40,104); rect(0,0,(int)unionWidth,(int)unionHeight); for (int row = 1; row <= 9; row++) { for (int column = 1; column <= 11; column++) { // Add a star for odd rows/cols or // even rows/columns. if (((row%2!=0) && (column%2!=0)) || ((row%2==0) && (column%2==0))) { image(star, column*unionWidth/12, row*unionHeight/10, starDiameter, starDiameter); }

© Calvin College, Repetition: Issues

© Calvin College, Example: Analysis (1) ● We’d like to draw regular polygons. ● We should be able to: – Set the number of sides; – Approximate the area of the enclosing circle; – Drag the polygon around. ● Some sample images are shown here.

© Calvin College, Example: Analysis (1) ● The program should: – Draw n-gons; – Check the validity of n; – Compute the n-gon’s area.

© Calvin College, 2009 Animation and Repetition ● We have now discussed two forms of repetition in Processing: – Processing implements animation by repeatedly calling the draw() method; – You can program your own repetition using the for statement. ● Your program can contain both forms of repetition. 38

© Calvin College, Iteration 8 ● Analysis ● Design ● Implementation ● Test

© Calvin College, void drawPolygon(int sides) { if (sides < 3) { println("invalid number of sides: " + sides + "\nsimplePolygon exiting"); exit(); } float angle = / sides, theta1, theta2; strokeWeight(1); for (int i = 0; i < sides; i = i+1) { theta1 = radians(angle * i); theta2 = radians(angle * (i + 1)); line(polygonX + cos(theta1) * polygonRadius, polygonY + sin(theta1) * polygonRadius, polygonX + cos(theta2) * polygonRadius, polygonY + sin(theta2) * polygonRadius); }

© Calvin College, Iteration 9 ● Analysis ● Design ● Implementation ● Test

© Calvin College, void setup() { size(width, height); smooth(); numSides = 3; frameRate(4); } void draw() { background(255); strokeWeight(5); point(polygonX, polygonY); drawPolygon(numSides++); }

© Calvin College, Iteration 10 ● Analysis ● Design ● Implementation ● Test

© Calvin College, float drawPolygon(int sides) { // Check the parameter as before. float angle = / sides, theta1, theta2; strokeWeight(1); for (int i = 0; i < sides; i = i+1) { theta1 = radians(angle * i); theta2 = radians(angle * (i + 1)); triangle(polygonX, polygonY, polygonX + cos(theta1) * polygonRadius, polygonY + sin(theta1) * polygonRadius, polygonX + cos(theta2) * polygonRadius, polygonY + sin(theta2) * polygonRadius); } // Compute n-gon area. return (sides * polygonRadius * sin(2 * PI / sides)) / 2.0; }

© Calvin College, Iteration 11 ● Analysis ● Design ● Implementation ● Test

© Calvin College, float drawPolygon(int sides) { // repeated code removed here… for (int i = 0; i < sides; i = i+1) { if (i % 2 == 0) { fill(random(255), random(255), random(255)); } else { fill(0); } // Compute thetas as before. triangle(polygonX, polygonY, // 4 points as before… ); } return (sides * polygonRadius * sin(2 * PI / sides)) / 2.0; }

© Calvin College, Out of Control Structures Unrestricted go-to statements can be used to produce “spaghetti” code: 00 if (condition) then statements A goto 20 endif statements B 10 continue statements C 20 continue statements D 30 if (condition) goto 10

© Calvin College, _main: pushl %ebp movl %esp,%ebp subl $24,%esp call ___main movl $0,-4(%ebp).align 4 L3: cmpl $9,-4(%ebp) jle L6 jmp L4.align 4 L6: cmpl $4,-4(%ebp) jg L5 L7: L5: incl -4(%ebp) jmp L3.align 4 L4: xorl %eax,%eax jmp L2.align 4 L2: movl %ebp,%esp popl %ebp ret int main() { for (int i = 0; i < 10; i++) if (i < 5) ; } Control in Assembly Code

© Calvin College, Edsger Dijkstra ( ) Structured Programming ● Dijkstra argued that control flow should be structured. ● He had opinions on languages as well: – FORTRAN - "the infantile disorder" – PL/I -- "the fatal disease" -- belongs more to the problem set than the solution set. – It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. – The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense. What’s the Big Idea picture from