Presentation is loading. Please wait.

Presentation is loading. Please wait.

Karel J Robot Chapter 6. What we have learned so far: Classes vs objects (e.g. UrRobot vs. karel) Parts of a class (constructor, method) Classifying errors.

Similar presentations


Presentation on theme: "Karel J Robot Chapter 6. What we have learned so far: Classes vs objects (e.g. UrRobot vs. karel) Parts of a class (constructor, method) Classifying errors."— Presentation transcript:

1 Karel J Robot Chapter 6

2 What we have learned so far: Classes vs objects (e.g. UrRobot vs. karel) Parts of a class (constructor, method) Classifying errors Polymorphism (extending classes) Abstract classes and interfaces Conditionals Booleans, logical operators

3 For Loop Loops – code that repeats some action either a finite or infinite amount of times Also called iteratives 2 types of loops ▫Definate loops (for) perform instructions an explicit number of times ▫Indefinate loops (while) perform instructions until a certain test fails

4 For Loop Example: public void move(int numMoves){..} Which loop do we want? Structure of a for loop: For(int x = 0; x<10; x++) { } For loop needs 3 things: ▫An initializer – can be initialized in for loop, or before ▫A boolean test – loop while run until the test is false ▫An operation – typically an incrementer

5 For Loop How many times will each for-loop run? for(x=0; x<10; x++) { …} for(x=0; x<=10; x++){…} for(x=10; x>0; x--){…} for(x=2; x>=14; x+=2) {…} Feel free to program the code and test it Let’s keep to a standard form: Start: x=0; Test: x<#; //don’t use <=; the # will represent how many times the loop runs Increment by x++;

6 While Loop Aside: the For-Loop takes on different forms, but the previous slides demonstrate its simplest form For Loop – good for looping when you know how many times you need to repeat When you need to program a loop that runs until a certain event happens…. Use a while loop

7 While Loop Form: while( ) { } Examples: while(!nextToABeeper()) {..} while(x>10){…} while(2>=4){…} The test can be any boolean outcome The actions in the loop should make progress to change the boolean test (to avoid an infinite loop)

8 Relational Operators Review ->Logical operators: &&, ||, ! Relational Operators are used for comparison >, >=, < <=, ==, != Note: = and == are not the same thing! = is the assignment operator… == is ‘equal to’

9 Student work Read chapter 6 (up to page 161) Program the Guard robot on page 162 to the end of the chapter Problem set 6 #1,2,3,8,14,16,24,34


Download ppt "Karel J Robot Chapter 6. What we have learned so far: Classes vs objects (e.g. UrRobot vs. karel) Parts of a class (constructor, method) Classifying errors."

Similar presentations


Ads by Google