Iterators - Iterators Iterators 2.1 Chapter Two Chapter...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Iterators Beta Draft - Do not distribute © 2001, By Randall Hyde Page 1305 Iterator s Chapter Two 2.1 Chapter Overview This chapter discusses the lo w-le v el implementation of iterators. Earlier , this te xt briefl y discussed iter - ators and the FOREA CH loop in the chapter on intermediate procedures (see “Iterators and the FOREA CH Loop” on page 843 ). This chapter will re vie w that information, discuss some uses for iterators, and then present the lo w-le v el implementation of this interesting control structure. 2.2 Review of Iterators An iterator is a cross between a control structure and a function. Although common high le v el languages do not support iterators, the y are present in some v ery high le v el languages 1 . Iterators pro vide a combination state machine/function call mechanism that lets a function pick up where it last left of f on each ne w call. Iterators are also part of a loop control structure, with the iterator pro viding the v alue of the loop control v ariable on each iteration. T o understand what an iterator is, consider the follo wing for loop from P ascal: for I := 1 to 10 do <some statement>; When learning P ascal you were probably taught that this statement initializes i with one, compares with 10, and e x ecutes the statement if is less than or equal to 10. After e x ecuting the statement, the FOR statement increments and compares it with 10 ag ain, repeating the process o v er and o v er ag ain until i is greater than 10. While this description is semantically correct, and indeed, it’ s the w ay that most P ascal compilers implement the FOR loop, this is not the only point of vie w that describes ho w the for loop operates. Sup - pose, instead, that you were to treat the T O reserv ed w ord as an operator . An operator that e xpects tw o parameters (one and ten in this case) and returns the range of v alues on each successi v e e x ecution. That is, on the fi rst call the T O operator w ould return one, on the second call it w ould return tw o, etc. After the tenth call, the T O operator w ould f ail which w ould terminate the loop. This is e xactly the description of an itera - tor . In general, an iterator controls a loop. Dif ferent languages use dif ferent names for iterator controlled loops, this te xt will just use the name FOREA CH as follo ws: foreach iterator () do statements ; endfor; An iterator returns tw o v alues: a boolean success or f ailure v alue and a function result. As long as the iterator returns success, the FOREA CH statement e x ecutes the statements comprising the loop body . If the iterator returns f ailure, the FOREA CH loop terminates and e x ecutes the ne xt sequential statement follo wing the FOREA CH loop’ s body . Iterators are considerably more comple
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

Iterators - Iterators Iterators 2.1 Chapter Two Chapter...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online