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.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
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.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Algorithms An algorithm is an exact set of instructions for carrying out a task using a limited menu of commands Algorithms are most often associated with.
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:
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
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.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
For Loop Tips And Tricks
Application development with Java Lecture 6 Rina Zviel-Girshin.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
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.
COMP Loop Statements Yi Hong May 21, 2015.
Review Expressions and operators Iteration – while-loop – for-loop.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
UMBC CMSC 104 – Section 01, Fall 2016
Unit 3 Lesson 9 Repetition Statements (Loops)
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.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Computation as an Expressive Medium
The University of Texas Rio Grande Valley
The University of Texas – Pan American
Lecture Notes – Week 3 Lecture-2
3 Control Statements:.
For Loops.
Repetition Control Structure
Chapter 6: Repetition Statements
Lecture 8:The For Loop AP Computer Science Principles.
February , 2009 CSE 113 B.
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Loops CGS3416 Spring 2019 Lecture 7.
Programming Fundamental
Presentation transcript:

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. Get help from other people –If you know how to program, help others Ask questions

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 A program often needs to increase (or decrease) the value of an integer variable by one, especially in loops. So there is a quick way to write this: 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

Checkers With Loops size(500,500); int xPos=0, yPos=0; int xInc=(width/2)+1, yInc=(height/2)+1; int xSize=width/2, ySize=height/2; int count=0; while (count <4) { if(count == 1) { xPos=xPos+xInc; } else if(count == 2) { yPos=yPos+yInc; } else if(count == 3) { xPos=xPos-xInc; } if(xPos%2 == yPos%2) { fill(0); } else if (xPos%2 != yPos%2) { fill(255); } rect(xPos, yPos, xSize, ySize); count++; }

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); }

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 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.