Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by

July 11, 2018 | Author: Anonymous | Category: N/A
Share Embed


Short Description

Download Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by...

Description

Karel – Chapter 6 Instructions That Repeat

Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A class

1

Iteration Graphic Organizer Control structures

Methods

Sequential

Iteration

Conditional

Types of control structures

Types of loops

Java statement

Syntax Examples:

2

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)

3

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? 4

Iteration Graphic Organizer Control structures

Methods

Sequential

Iteration

Conditional

Types of control structures

Definite Loops

Types of loops

Indefinite Loops

for

Java statement

while

Syntax Examples:

5

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

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, 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”. 7

for Loops (cont.) • Example: for (int i=1; i
View more...

Comments

Copyright © 2017 DOCUMEN Inc.