Iteration Implemented through loop constructs e g in

  • Slides: 4
Download presentation
Iteration • Implemented through loop constructs (e. g. , in C++) – – E.

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 CSE 425: Control Flow II

Recursion • Functional languages severely limit side effects – Iteration relies on side effects

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) CSE 425: Control Flow II

Nondeterminacy Features and Sources • Dijkstra’s original guarded if statement – Multiple alternate execution

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 CSE 425: Control Flow II

Today’s Studio Exercises • We’ll code up ideas from Scott Chapter 6. 5 -6.

Today’s Studio Exercises • We’ll code up ideas from Scott Chapter 6. 5 -6. 8 – 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, email your answers with subject line “Control Flow Studio II” to cse 425@seas. wustl. edu CSE 425: Control Flow II