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.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CS0007: Introduction to Computer Programming
Game with US Beginner Tutorial. Welcome!! Who I am What is Processing? Basic Coding Input Methods Images Classes Arrays.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
IAT 334 Java using Processing ______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
IAT 800 Foundations of Computational Art and Design Lecture 2.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CONTROLLING PROGRAM FLOW
______________________________________________________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] |
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Lesson Two: Everything You Need to Know
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
LCC 6310 Computation as an Expressive Medium Lecture 2.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Continuous. Flow of Control Programs can broadly be classified as being –Procedural Programs are executed once in the order specified by the code varied.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
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.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
The switch Statement, and Introduction to Looping
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
JavaScript: Control Statements.
Computation as an Expressive Medium
Lecture Notes – Week 3 Lecture-2
Chapter 4 - Program Control
3 Control Statements:.
Repetition Control Structure
Computing Fundamentals
Lecture 8:The For Loop AP Computer Science Principles.
Lecture 6: Conditionals AP Computer Science Principles
February , 2009 CSE 113 B.
Loops & Nested Loops CSE 120 Winter 2019
IAT 800 Foundations of Computational Art and Design
Chap 7. Advanced Control Statements in Java
Continuous & Random September 21, 2009.
LCC 6310 Computation as an Expressive Medium
Programming Fundamental
Presentation transcript:

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 earlier Often we want to control which steps are executed depending on what else has happened That is, we want conditional control flow –This is necessary in order to make anything that is interactive

if if statements create conditional flow if ( ) { // do this code } –Note the { }s Boolean expressions have one of two values: true or false

Boolean Expressions used in Conditional Flow Conditional tests use the following operators Relational operators > (greater than) < (less than) >= (greater than or equal to) <= (less than or equal to) == (equals) != (not equal to) Logical operators || (logical OR) && (logical AND) ! (logical NOT)

Examples of Boolean Expressions used for Conditional Tests Simple equality tests: (5 == 6)false (5 == 5) true Relational tests: (5 < 6) true (5 > 5) false (5 <= 5) true Logical tests: (true || false)true (!false) true Combined tests: !(15 > 20) true ((5 == 6) && (5 == 5))false ((5 == 6) || (5 == 5)) true

In Processing println(3 > 5); // prints false println(5 > 3); // prints true println(5 > 5); // prints false println(3 < 5); // prints true println(5 < 3); // prints false println(5 < 5); // prints false println(3 >= 5); // prints false println(5 >= 3); // prints true println(5 >= 5); // prints true println(3 <= 5); // prints true println(5 <= 3); // prints false println(5 <= 5); // prints true

Example of Simple Conditional Statement Print a different message depending on the value of a variable: int x = 5; if (x == 5) { println(”x is 5”); }

Graphic Example from the Text // The text expressions are "x > 100" and "x < 100" // Because x is 150, the code inside the first block // runs and the ellipse draws, but the code in the second // block is not run and the rectangle is not drawn int x = 150; if (x > 100) { // If x is greater than 100, ellipse(50, 50, 36, 36);// draw this ellipse } if (x < 100) { // If x is less than 100 rect(35, 35, 30, 30);// draw this rectangle } line(20, 20, 80, 80);

if-else variation if ( ) { // do this code } else { // do this other code } –Note the { }s

Example from the Text // Because x is 90, only the rectangle draws int x = 90; if (x > 100) {// If x is greater than 100, ellipse(50, 50, 36, 36);// draw this ellipse. } else {// Otherwise, rect(33, 33, 34, 34);// draw this rectangle } line(20, 20, 80, 80);

if-elseif variation if ( ) { // do this code } else if ( ) { // do this other code } if ( ) { // do this code } else if ( ) { // do this other code } else { // do this other code }

Example From the Text // If x is less than or equal to 100, then draw // the rectangle. Otherwise, if x is greater than // or equal to 300, draw the line. If x is between // 100 and 300, draw the ellipse. Because x is 101, // only the ellipse draws. int x = 101; if (x <= 100) { rect(33, 33, 34, 34); } else if (x >= 300) { line(50, 0, 50, 100); } else { ellipse(50, 50, 36, 36); }

Using Conditional Statements Declare a variable temp of type int. Assume that you will use temp to determine what to wear. Your outerwear(?) choices are “ none ”, “ heavy jacket ”, “ hoodie ”, and “ raincoat ”. Write a processing program that will look at temp to determine what outerwear is appropriate for any value of temp. Write the appropriate outerwear choice using println.

So far… We've seen how to write commands in Processing –We've learned that commands need to end with a ; –We've learned how to call methods with specific arguments –We've learned how to declare variables and assign values to them –We've learned how to print values to the text output area –We've learned about the primitive variable types –We've learned how to control flow with conditional if- else statements By default, the sequence of commands we write in Processing will execute once and then the program will terminate

Motivation size(300,300); background(0); stroke(255); point( (width/11)*1, height/2); point( (width/11)*2, height/2); point( (width/11)*3, height/2); point( (width/11)*4, height/2); point( (width/11)*5, height/2); point( (width/11)*6, height/2); point( (width/11)*7, height/2); point( (width/11)*8, height/2); point( (width/11)*9, height/2); point( (width/11)*10, height/2);

A Better Way size(300,300); background(0); stroke(255); int i = 1; while(i<11) { point( (width/11)*i, height/2 ); i++; }

while Loops while( ) { } Executes the code within the code block (or the curly brackets) while the boolean expression is true

++ The Increment Operator -- The Decrement Operator Within a loop, we often want to count up (increment) and count down (decrement) often. For this the C designers created two special operators x++ is the same as writing x = x + 1 x-- is the same as writing x = x - 1

Other Operators Operators in Java/Processing also include shorthand ways to write simple addition, subtraction, multiplication and division statements: x += 2 is the same as writing x = x + 2 x *= 3 is the same as writing x = x * 3

Lines size(480, 120); smooth(); strokeWeight(2); int x = 20, inc = 8; while(x < 400) { line(x, 40, x + 60, 80); x += inc; }

Stripes size(500,500); background(255) ; noStroke() ; int numStripes = 8 ; int stripeWidth = height/numStripes ; int count = 0 ; while (count < numStripes) { if (count % 2 == 0) { fill(0) ; } else { fill(255, 0, 0) ; } rect(count*stripeWidth, 0, stripeWidth, height); count++ ; }

Checkers With Loops size(500,800); int numColumns=3, numRows=4; int x=0, y=0, count=0; int xSize= width/numColumns, ySize=height/numRows; while (count < numColumns * numRows) { if(count % 2 == 0) { fill(0); } else { fill(255); } rect(x, y, xSize, ySize); count++; x += xSize; if (count % numColumns == 0) { y += ySize; x = 0; }

for loops for ( ; ; ) { } First executes the initialization statement Then tests the boolean expression – if it's true, executes the code inside the curly brackets once Then repeats the following: execute final statement, test boolean expression, execute code if true This structure is often used to execute a block of code a specific number of times as follows: Init statement -> start counter, e.g. int counter = 0; Boolean exp -> looping condition, e.g. counter < 10; Final statement -> increment counter, e.g. counter++; (same as counter=counter+1;)

Example int i, spacing; spacing=width/5; for (i=1; i<5; i++) { ellipse(spacing*i,spacing*i,spacing,spacing); }

An Example from the Text size(500,500); for(int d=width; d>0; d-=10) { ellipse(width/2, height/2, d, d); }

Converting a for Loop to a while Loop Seeing how for loops can be converted to while loops helps you understand for loops for( ; ; ) { } is the same as while( ) { }

Example int max =500; size(max,max); colorMode(RGB, max); for(int i=0; i<=max; i++) { stroke(i,0,0); line(i, 0, i, height); } int max =500; size(max,max); colorMode(RGB, max); int i=0; while(i<=max) { stroke(i,0,0); line(i, 0, i, height); i++; }

Nested Loops noStroke(); colorMode(RGB, 100); for(int i=0; i<100; i++) { for(int j=0; j<100; j++) { stroke(i, j, 0); point(i, j); }

Checkers (again) size(500,500); background(255) ; noStroke() ; int numStripes = 8 ; int cellSize = min(width, height)/numStripes ; for (int i=0; i<numStripes; ++i) { for (int j=0; j<numStripes; ++j) { if ((i+j)%2==0) { fill(0) ; } else { fill(255, 0, 0) ; } rect(i*cellSize, j*cellSize, cellSize, cellSize) ; }

Usage Use a for loop if you know how many times you want to repeat. Use a while loop if you don’t know how many times you want the loop to execute. The while loop is considered a general purpose loop construct, but remember the test or boolean expression is evaluated outside the loop, so the loop body may not execute.

In-class Lab Modify the checkers program to –Display four different colors in some interesting way Use an else-if –Put white “checkers” in the squares –Make the checkers on the edge green

More In-class Lab Initialize variables x and y to 0. Using a while statement, create a program that writes a point out at (x,y), and incrementing x and y at each repetition until x==width or y==height. Create a regular pattern with 5 lines using a for statement. Create an image (where you change every pixel in the window) using a nested for loop.

Assignment 2 - Due Feb 3 Electronic quilts Remember to comment your code, and to submit only your source code.