DOC PREVIEW
CUNY CISC 1001 - While Loop Tutorial

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture F1While Loop TutorialComputing and Art : Nature, Power, and LimitsCC 3.12: Fall 2007Functionalia•HW E: DUE Wednesday, Nov 14th, 11:59 pm•Wednesday Open Lab 9:30 - 12:00 (but NOT Office Hours)InstructorChipp Jansen, [email protected] Web Pagehttp://www.sci.brooklyn.cuny.edu/~chipp/cc3.12/Office Hours | Extra HelpMy Office Hours : Mondays 12:30 to 1:30 - basement Ingersoll 0317NVirtual: Tuesdays 9 to 11pm - AIM: chippbotMonday1:30 to 5pmTuesday1:30 to 3:30Wednesday1:30 to 3:30Thursday12:45 - 1:45, 2:15 - 3:15Bridges Student Resource Center : 0317NFunctionaliaToday:•Iteration and the While Loop.IterationIteration means doing something more than once perhaps doing somethings over and over ... and over again.Iteration allows us to tell the computer to do repetitive task with explicitly telling the computer what to do.Example: Tell the computer to draw 100 ellipses in a row, which having to write the ellipse function 100 times.Iteration allows for Complex RepetitionVariety is possible with IterationShapes are basically circular, but vary in size, color, and palcement.Loops On a computer, we implement Iteration through Loops.Two kinds of loops:infinite loops: repeat indefinatly until you quit the program, or turn off the computer. The draw() function is example of something that loops infinitely. conditional loops: repeats while a certain condition is true. These loops allow you to do something for a certain number of times. In Processing, we will use a while loop to repetitively draw a shape.There are other loops in programming languages:•for loops•do... while loops•for each loops// Add variables herevoid setup() {size(600, 600);}void draw() {}1. Start with the Start template.// Add variables hereint x;void setup() {size(600, 600);x = 0;println(“Value of x “ + x);}void draw() {}2. We will start by adding a variable (x) to use in our loop.This variable is called the loop variable.Loops use variables to keep track of progress, and to know when to stop.// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 10) {println(“Value of x “ + x);x = x + 1;}}void draw() {}3. Let us put the println statement into a while loop to repetitively print the value of x out.// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 10) {println(“Value of x “ + x);x = x + 1;}}void draw() {}4. The structure is the same as the if() conditional. Starts with the word “while”// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 10) {println(“Value of x “ + x);x = x + 1;}}void draw() {}5. Then there is a conditional. Just like the if() statement.// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 10) {println(“Value of x “ + x);x = x + 1;}}void draw() {}6. While this conditional is true, what follows in the curly braces runs over and over again. Until the conditional becomes false.// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 10) {println(“Value of x “ + x);x = x + 1;}}void draw() {}7. What is printed out? How do the values of x change through the loop?// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 10) {println(“Value of x “ + x);x = x + 1;}}void draw() {}8. Notice every time through the loop the value of x is increased by one. At what value of x does the loop stop?// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 10) {println(“Value of x “ + x);x = x + 2;}}void draw() {}9. What happens when the value of x is increased is changed to 2?How many times does the loop run?// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 10) {println(“Value of x “ + x);ellipse(x, 100, 20, 20);x = x + 2;}}void draw() {}10. Let’s add an ellipse to the while loop.We will use the loop variable x in place of the x parameter of the ellipse.What happens? How many ellipses are drawn on the screen?// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 600) {println(“Value of x “ + x);ellipse(x, 100, 20, 20);x = x + 2;}}void draw() {}11. In the previous step: The ellipses were drawn from the x value range of 0 to 10. The range of values that the variable x contained.Let us increase the range by increasing the limit in the conditional to 600 (the width of the screen).How many ellipses will be drawn?// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 600) {println(“Value of x “ + x);ellipse(x, 100, 20, 20);x = x + 100;}}void draw() {}12. The ellipses are drawn on top of each other. We can increase the spacing between the ellipses by increasing the value that the x variable is increased by every time through the while loop.Let us increase the value to 100, how many ellipses will be drawn?// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 600) {println(“Value of x “ + x);fill(0, x, 0);ellipse(x, 100, 20, 20);x = x + 100;}}void draw() {}13. We can use the loop variable in other functions as well in the loop. Changing the fill color of the ellipse for example.What is the color range for these ellipses?// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 600) {println(“Value of x “ + x);fill(0, x/3, 0);ellipse(x, 100, 20, 20);x = x + 100;}}void draw() {}14. Since the range of color values are 0 to 255. And our variable varies from 0 to 600, we can use division to narrow the fill value.We can divide x by 3, and pass the resulting value to the fill() function. Note: this does NOT change the value of x for the loop.// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 600) {println(“Value of x “ + x);fill(0, x/3, 0);ellipse(x, 100, 20, 20);x = x + 100;}}void draw() {}MathCodeadd+subtract-divide/multiply*15. Other mathematical operations that are possible.// Add variables hereint x;void setup() {size(600, 600);x = 0;while(x < 600) {println(“Value of x “ + x);fill(0, x/3, 0);ellipse(x, 100, 20, 20);x = x + 100;}}void draw() {}16. Up until this point, the while loop has been in the setup() function. Remember, that setup() occurs only once, at the beginning of the program.Let’s see what happens when we move the while loop to the draw function...// Add variables hereint x;void setup() {size(600, 600);}void draw() {x = 0;while(x < 600) {println(“Value of x “ + x);fill(0, x/3, 0);ellipse(x, 100, 20, 20);x = x + 100;}}17. Notice that everything except for the size() function was moved to the


View Full Document

CUNY CISC 1001 - While Loop Tutorial

Download While Loop Tutorial
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view While Loop Tutorial and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view While Loop Tutorial 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?