Karel J Robot Chapter 6.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Karel – Chapter 6 Instructions That Repeat
Karel J Robot Chapter 6.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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.
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.
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)
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.
CPS120 Introduction to Computer Science Iteration (Looping)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
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.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
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 
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.
Chapter 6 Controlling Program Flow with Looping Structures.
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 6: Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 6 Repetition Richard Gesick.
Programming Logic and Design Fourth Edition, Comprehensive
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Chapter 5: Loops and Files.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Looping and Repetition
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
The University of Texas – Pan American
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
Chapter 6: Repetition Statements
Control Structures Part 1
Introduction to Repetition Structures
FLUENCY WITH INFORMATION TECNOLOGY
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
Iteration (Loops) Loops repeat a set of instructions
The structure of programming
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Thinking procedurally
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Looping and Repetition
Chapter 13 Control Structures
Presentation transcript:

Karel J Robot Chapter 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) Chapter 6

for Loops General form: for ( <initialize variable>; <test>; <increment> ) { <instruction set> } Chapter 6

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

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

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

while Loops General form: while ( <test> ) { <instruction list> } Loop continues until test is false Chapter 6

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

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

Building a While Loop 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 <test> for the loop. You just need to negate the entire thing. Chapter 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. Chapter 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 Chapter 6

Chapter 6