Download presentation
Presentation is loading. Please wait.
Published byJonathan Damian Shelton Modified over 9 years ago
1
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops Procedural abstraction Recursion Concurrency Exception handling and speculation Nondeterminacy
2
CSE 425: Control Flow I Expressions vs. Statements It is useful to differentiate expressions vs. statements –Statements (e.g., assignment) have side effects but usually do not return a value (C++ doesn’t follow this strictly) –Expressions (e.g., right hand side of a statement) provide a value but usually don’t have side effects (again except C++) Expression syntax may be prefix, infix, postfix –Prefix and postfix don’t require parentheses for precedence Comparison to procedures and functions –Operands are viewed as arguments or actual parameters –Referential transparency assumes no side effects –Side effects may change control flow as well as values Some statements mimic complete control constructs –E.g., conditional operator in C++ acts like if/else construct
3
CSE 425: Control Flow I Structured vs. Unstructured Control Flow Goto considered too low level –Still available in some languages –Rarely necessary, often better to use other features instead –E.g., break and continue statements in C++ Many languages offer structured alternatives –E.g., break to exit a loop or a selected branch in C++ –E.g., continue to skip the rest of an iteration in C++ Return and multi-level return –Return may set a value and also transfer control to caller –Multi-level return (or exception) may unwind farther Continuations capture a context for further execution –E.g., to defer part of the execution until later
4
CSE 425: Control Flow I Sequencing Ordering of statements –Happens-before relationship in control flow –Particularly important when thinking about concurrency Data dependences –Previous write (store) to a variable (location) seen by subsequent read (load) instruction(s) Compound statements –A block of statements is used in place of a single statement
5
CSE 425: Control Flow I Selection If statements (e.g., in C++) –If statement evaluates expression (not necessarily Boolean) –If true (non-zero) executes statements in its first block –Otherwise executes else block if one was provided –Can next if statements, so else associates with most recent If that does not already have an else part Switch (case) statements –Condense if/else logic into cases of an ordinal expression –Default blocks (no case matches) –Breaks, fall through can be used to emulate ranges of cases
6
CSE 425: Control Flow I Today’s Studio Exercises We’ll code up ideas from Scott Chapter 6.1-6.4 –Looking at expressions, statements, sequencing, selection –Next time we’ll look 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 I” to cse425@seas.wustl.edu
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.