1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Karel – Chapter 6 Instructions That Repeat
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.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
Looping Yong Choi School of Business CSU, Bakersfield.
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 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
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.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CPS120 Introduction to Computer Science Iteration (Looping)
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 5.  We’ve been using UrRobot – knows only 5 things  Let’s now use a new type of Robot: Robot!  i.e. – public class MileMover extends Robot.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Chapter 6 Questions Quick Quiz
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)
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.
Lecture 7: Repeating a Known Number of Times
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.
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
Chapter 4 Repetition Structures
Karel the Robot – Making Decisions
Lecture Notes – Week 3 Lecture-2
Unit 3 - The while Loop - Extending the Vic class - Examples
Repetition Control Structure
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
Building Java Programs
Iteration (Loops) Loops repeat a set of instructions
The for-statement.
Presentation transcript:

1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com

2 Control structures IterationConditional MethodsSequential Iteration Graphic Organizer Types of loops Java statement Types of control structures Syntax Examples:

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

4 Definite vs. Indefinite Loop? A new class you’re writing has a method with the following signature: public void 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? Is it a good idea or bad idea?

5 Control structures IterationConditional Definite Loops MethodsSequential Iteration Graphic Organizer Indefinite Loops whilefor Types of loops Java statement Types of control structures Syntax Examples:

6 for Loops General syntax: for ( ; ; ) { }

7 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, the loop is exited) changes the loop control variable (usually i++ or i--). The incrementing is done at the bottom of the loop. i++ means “add 1 to i”. i-- means “subtract 1 from i”.

8 for Loops (cont.) Example: for (int i=1; i<=3; i++) { move(); putBeeper(); } This causes the robot to move and place a beeper down on 3 different corners Note: Write this on the board so that we can refer to it on next slide

9 for Loops (cont.) Flow of for loops: 1.Initialization statement executes 2.If test is true, proceed to next step; 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

10 Control structures IterationConditional Definite Loops MethodsSequential Iteration Graphic Organizer Indefinite Loops initialize variabletestincrement Types of loops Types of control structures Syntax Examples: for ( int i=1; i<=3; i++ ) { move(); putBeeper(); } whilefor Java statement

11 Move a specified number of times Example: public void move (int numMoves) { for (int i=1; ___________; i++) { super.move(); } } This causes the robot to move a specified number of times for (int i=1; i<=numMoves; i++)

12 Try this yourself Write a for loop to pick up a beeper (if there is one) on the four street corners directly in front of the robot: for (int i=1; i<=4; i++) { move(); if ( nextToABeeper() ) pickBeeper(); }

13 BeeperSquare Exercise

14 BeeperSquare Exercise Create a BeeperSquare robot class that extends SmartBot or Robot (you may want to use some of its methods) Create a method named drawSquare() that uses at least one “for” loop (two “for” loops would be better) that will create a square with 6 beepers on each side. Use a single robot to do this. The end result should be a square containing 20 beepers (only one beeper on each street corner). Create a client program that will test this new class. Try constructing two robots and drawing 2 separate squares in the world (overlapping the two).