1/32
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Control Structure
a control statement and the statements whose execution it controls
2 types of control structures
Selection Statements -- Iterative Statements
Only design issue relevant to all selection and iteration control statements
Should a control structure have multiple entries?
Selection Statement
provides the means of choosing between two or more paths of execution
2 categories of selection statements
Two-way selection -- Multiple-way selection
3 design issues for two-way selection
form and type of the control expression -- how then and else clauses are specified -- how meaning of nested selectors is specified
Control Expression in C89
used arithmetic expressions -- nonzero is true -- zero is false
Control Expression in Java/C#
only Boolean expressions allowed
Clause Form in C-based languages
uses braces to form compound statements
Clause Form in Perl
all then and else clauses must be compound statements even if they contain a single statement
Clause Form in Python
uses indentation to define clauses -- colon used instead of then
Nesting Selectors rule
else clause is always paired with the nearest unpaired then clause -- applies in Java and contemporary languages
C# switch
differs from C-based switch -- static semantic rule disallows implicit execution of more than one segment -- every segment must end with break or goto
Iterative Statement
causes a statement or collection of statements to be executed zero, one, or more times -- often called a loop
Pretest loop
loop completion condition checked before the loop body is executed -- example: while
Posttest loop
loop completion condition checked after the loop body is executed -- example: do-while
Key difference: while vs do-while
do-while always executes the loop body at least once
Counter-Controlled Loop
has a loop variable -- specifies initial value, terminal value, and stepsize
Stepsize
the difference between sequential loop variable values
Loop Parameters
the initial, terminal, and stepsize values
C for loop infinite loop
if the second expression is absent
C for vs Fortran/Ada
C's for is more flexible -- each expression can comprise multiple statements -- allows multiple loop variables of any type
Java/C# for loop
like C++ except loop control expression is restricted to Boolean
Logically Controlled Loop
repetition based on a Boolean expression rather than a counter
User-Located Loop Exits -- C/C++
unconditional unlabeled exits using break
User-Located Loop Exits -- Java/C#/Perl
unconditional labeled exits (break in Java/C# -- last in Perl)
continue statement
skips the rest of the current iteration without terminating the loop -- transfers control to the loop's control mechanism
Iterator
called at the beginning of each iteration -- returns an element from a data structure in a specific order
foreach in C#
iterates on elements of arrays and other collections -- uses built-in iterators of predefined generic collections
Unconditional Branch / goto
the most powerful statement for controlling execution flow -- carelessly used, it makes programs difficult to read and unreliable
Languages without goto
Java -- Python -- Ruby
Guarded Commands (Dijkstra 1975)
alternative control statements -- evaluates all Boolean guards -- if more than one true, chooses non-deterministically -- if none true, runtime error -- basis for CSP concurrency mechanisms
Motivation for Guarded Commands
support program correctness verification during development rather than after completion