1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.

Slides:



Advertisements
Similar presentations
Karel – Chapter 6 Instructions That Repeat
Advertisements

Karel J Robot Chapter 6.
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
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.
Dependency Analysis We want to “parallelize” program to make it run faster. For that, we need dependence analysis to ensure correctness.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
CPS120 Introduction to Computer Science Iteration (Looping)
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
CS 108 Computing Fundamentals Notes for Thursday, February 19, 2015.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
CS 100 Introduction to Computing Seminar October 7, 2015.
CPS120 Introduction to Computer Science Iteration (Looping)
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
Structured Programming The Basics. Control structures They control the order of execution What order statements will be done in, or whether they will.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Karel J. Robot Chapter 6 Instructions That Repeat.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
PHY 107 – Programming For Science. Today’s Goal  How to use while & do / while loops in code  Know & explain when use of either loop preferable  Understand.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Karel J. Robot Chapter 6 Instructions That Repeat.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Structured Programming The Basics
Identify the Appropriate Method for Handling Repetition
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Chapter 4 Repetition Statements (loops)
Lesson 05: Iterations Class Chat: Attendance: Participation
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Karel J Robot Chapter 6.
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
The University of Texas – Pan American
FLUENCY WITH INFORMATION TECNOLOGY
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Statements in C Programming
Iteration (Loops) Loops repeat a set of instructions
Python While Loops.
Review of Previous Lesson
Presentation transcript:

1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times –Indefinite loops ( while ) perform instructions an indefinite number of times (until a certain test fails)

2 Ch. 6 Questions for You a new class you’re writing has a method with the following signature: move (int numMoves) –should it be implemented with a definite loop or an indefinite loop? justify your answer –could it be implemented with the other type of loop? if so, should we/could we/good idea/bad idea???

3 Ch. 6 Questions for You take 5 minutes and draw a situation on paper that would necessitate that you use a while loop to solve the problem

4 Ch. 6 for Loops General form: for ( ; ; ) { }

5 Ch. 6 for Loops (cont.) sets a variable/identifier to a certain value (variable is usually count, i, j) is a test that is evaluated each time the body is about to be executed (when false, loop is exited) changes the loop control variable (usually x++ or x-- )

6 Ch. 6 for Loops (cont.) Example: for (int x=1; x<=5; x++) { karel.move(); } This causes k arel to move 5 times

7 Ch. 6 for Loops (cont.) Flow of for loops: 1.Initialization statement executes 2.If test is true, proceed to step 3; if test is false, go to step 6 3.Instructions in body of loop are executed 4.Increment statement executes 5.Return to step 2 6.Program proceeds to statements after loop

8 Ch. 6 while Loops General form: while ( ) { } Loop continues until test is false

9 Ch. 6 Questions for You back on slide 3 you drew up a situation that would best be solved using a while loop - now write the code to solve your problem

10 Ch. 6 while Loops (cont.) Example: while (karel.frontIsClear ()) { karel.move (); } Causes k arel to move continuously until there is a wall in front of it

11 Ch. 6 while Loops (cont.) Flow of while loops 1.If test is true, proceed to step 2; if test is false, go to step 4 2.Instructions in body of loop are executed 3.Go to step 1 4.Statements after loop are executed

12 Ch. 6 Building a While Loop Formalizing (making easier) the ad-hoc way you’ve been writing while loops to this point. This will be extremely helpful to you at various times in the course and on the AP exam. Step 1 – Identify what is true when the loop is finished (this is always easier than determining what is false while it is still executing) Step 2 – Use the opposite form of step 1’s result as the for the loop. You just need to negate the entire thing. ( note: Now and again you might find that “DeMorgan-izing” your result is helpful when answering AP-like questions.)

13 Ch. 6 Building a While Loop (cont’d) Step 3 – make progress toward the goal (completion of the loop) within the loop Step 4 – Do whatever is required before and/or after the loop to ensure that we solve the given problem Example: Pick all beepers from a corner without knowing how many there are.

14 Ch. 6 Let’s build a while loop Move a robot until it is not on a corner with any other robots and there is a wall on the left, then pick up the beeper that will be waiting there (the “hard” part is getting the condition correct without using a trial-and-error method – you don’t get to run any programs during our tests nor on the AP exam – also, you’ll code your labs faster if you have a way to ensure correct conditions EVERY time – without the crutch of running your code) You may use && and || for this exercise. Use the 4- step process now… while (???) { ??? }

15 Ch. 6 Your try the while condition You may use && and || for this Write a loop that moves a robot forward until it is either next to a robot, next to a beeper, or there is no wall on its right while ( ??? ) { ??? }

16 Ch. 6 Infinite Loops In a for or while loop, it is possible for the test to never be false When this happens, the loop continues infinitely Depending on the compiler, application, and other circumstances, an error may occur or the app may crash