Loops & Nested Loops CSE 120 Winter 2019

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
Lecture 10.2 Different Types of Loops, including for and do.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Repetition Statements while and do while loops
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
CPS120 Introduction to Computer Science Iteration (Looping)
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Application development with Java Lecture 6 Rina Zviel-Girshin.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
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 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Nested Loops & Arrays CSE 120 Spring 2017
Expressions & Control Flow CSE 120 Spring 2017
Control Flow (Python) Dr. José M. Reyes Álamo.
Lecture 4b Repeating With Loops
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
Lecture 13 & 14.
Variables & Datatypes CSE 120 Winter 2018
Chapter 8 Repetition Statements
Expressions & Control Flow CSE 120 Winter 2018
Outline Altering flow of control Boolean expressions
LRobot Game.
Variables & Datatypes CSE 120 Spring 2017
Chapter 5, Conditionals Brief Notes
Nested Loops, Arrays CSE 120 Winter 2018
Pages Section: Control2: Repetition
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Pages:51-59 Section: Control1 : decisions
Chapter 6: Repetition Statements
Functions in Processing CSE 120 Winter 2019
Basic Input and Output CSE 120 Winter 2019
Variables & Datatypes CSE 120 Winter 2019
Arrays CSE 120 Winter 2019 Instructor: Teaching Assistants:
Processing and Drawing CSE 120 Winter 2019
LCC 6310 Computation as an Expressive Medium
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Buttons & Boards CSE 120 Winter 2019
Pages:51-59 Section: Control1 : decisions
Pages Section: Control2: Repetition
Pages Section: Control2: Repetition
CSC215 Lecture Control Flow.
Chapter 3 Flow of Control Loops in Java.
Looping and Repetition
Expressions & Conditionals CSE 120 Winter 2019
Presentation transcript:

Loops & Nested Loops CSE 120 Winter 2019 Instructor: Teaching Assistants: Justin Hsia Ann Shan, Eunia Lee, Pei Lee Yap, Sam Wolfson, Travis McGaha ❄️☃️ SNOW LECTURE – SNOW DAY ☃️❄️

Outline Loops while-loop for-loop Nested Loops

Looping Sometimes we want to do the same (or similar) things over and over again Looping saves us time from writing out all of the instructions Loops control a sequence of repetitions

While-Loop Basic form: while(condition) { // loop // body } Start End Loop Body False True Basic form: while(condition) { // loop // body } Repeat loop body until condition is false Must make sure to update conditional variable(s) in loop body, otherwise you cause an infinite loop draw() is basically a while(true) loop

While-Loop Example [Demo] Row of six animals: Using a while-loop: void drawRow() { ??? // draw six mice } void drawMouse(float x, float y, color c) { ... // drawing commands mouse_loop.pde void drawRow() { int count = 0; while (count < 6) { drawMouse(80*count, 20, color(150)); count = count + 1; }

While-Loop More general form: // init cond var(s) while(condition) { Start Initialize Var(s) Condition? End Loop Body False True Update Var(s) More general form: // init cond var(s) while(condition) { // loop body // update var(s) } This occurs so commonly that we create a separate syntax for it!

For-Loop for(init; cond; update){ // loop body } Start Initialize Var(s) Condition? End Loop Body False True Update Var(s) for(init; cond; update){ // loop body } First runs init expression(s) Then checks cond If true, runs loop body followed by update statement(s)

For-Loop Example Without loop: With loop: line( 20, 40, 80, 80); for (int i = 20; i < 400; i = i + 60) { line(i, 40, i + 60, 80); }

Understanding the For-Loop Choice of variable name(s) is not critical Represent the value(s) that vary between different executions of the loop body Think of as temporary variable(s) If variable i is declared in the initialization statement, then it only exists within this loop initialization for (int i = 20; i < 400; i = i + 60) { line(i, 40, i + 60, 80); }

Understanding the For-Loop Condition evaluated before the loop body and must evaluate to true or false Reminder: > greater than < less than >= greater than or equal to >= less than or equal to == equal to != not equal to condition for (int i = 20; i < 400; i = i + 60) { line(i, 40, i + 60, 80); }

Understanding the For-Loop Update is an assignment that is executed after the loop body Loop body is enclosed by curly braces {} and should be indented for readability update for (int i = 20; i < 400; i = i + 60) { line(i, 40, i + 60, 80); } loop body

Loops Worksheet While-loop: For-loop: while(condition) { // loop body Start End Loop Body False True While-loop: while(condition) { // loop body } For-loop: for(init; cond; update){ Start Initialize Var(s) Condition? End Loop Body False True Update Var(s)

Processing Demo: Circles on Canvas Edge

Processing Demo: Circles on Canvas Edge size(720, 120); // canvas size background(255); // white background noStroke(); // no outline on circles fill(75, 47, 131); // UW purple int diam = 40; // loop for circles along the top edge for (int x = 0; x <= width; x = x+diam) { ellipse(x, 0, diam, diam); } // loop for circles along the left edge for (int y = 0; y <= height; y = y+diam) { ellipse(0, y, diam, diam);

Outline Loops while-loop for-loop Nested Loops

Nested Loops Generally a for-loop has a single loop variable that changes with each iteration What if you need/want more things to change? Can nest loops – i.e. put a loop inside of another loop

Example: Rectangle Grid