Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iteration Implemented through loop constructs (e.g., in C++)

Similar presentations


Presentation on theme: "Iteration Implemented through loop constructs (e.g., in C++)"— Presentation transcript:

1 Iteration Implemented through loop constructs (e.g., in C++)
E.g., in C++, while, for, do loops A continue statement skips rest of that iteration of the loop A break statement exits the loop Iterators also provide helpful abstractions for iteration While loop Most basic construct: test guards each iteration of a block For loop Encodes special case of a while loop (can emulate an enumeration controlled loop using logical control) Do loop Ensures execution of the block at least once

2 Recursion Functional languages severely limit side effects
Iteration relies on side effects to make progress, terminate Recursion is a natural alternative in those cases Or where avoiding side effects simplifies flow control logic Recursive functions can support lazy evaluation E.g., packaging up remaining work as a continuation and then only performing that work if it’s needed Normal order vs. applicative order evaluation Operands may not be evaluated until needed (applicative)

3 Nondeterminacy Features and Sources
Dijkstra’s original guarded if statement Multiple alternate execution branches Each guarded by conditional expression, enabled if true Non-deterministic selection from among enabled statements Dijkstra’s guarded do statement Multiple guarded statements, enabled if guard evaluates true Repeated non-deterministic selection of enabled statement Multi-threading Deterministic sequencing within but not between threads Can synchronize to re-introduce inter-thread happens-before

4 Today’s Studio Exercises
We’ll code up ideas from Scott Chapter Looking at iteration, recursion, non-determinacy Today’s exercises are again in C++ C++ allows us to explore control flow issues for statements Functional languages rely more on function calls for control Please take advantage of the on-line tutorial and reference manual pages that are linked on the course web site As always, please ask us for help as needed When done, your answers with subject line “Control Flow Studio II” to


Download ppt "Iteration Implemented through loop constructs (e.g., in C++)"

Similar presentations


Ads by Google