Class 35: Conditionals and Loops
Held Wednesday, April 28
- Are there final questions on exam 3?
- Note that the only way to build ``integers'' with
pred is to start with
- An ``argument'' should be about a paragraph long and should take
the form ``You should (should not) be able to assign XXX to YYY
because ....'' I'd prefer if you'd also attempt to refute
counter-arguments, but that is not strictly necessary.
- Why are you making this simple grammar unambiguous? Because it's
good practice. Also, we may derive meaning from the grammar.
For example, if we were working with parenthesized expressions,
we might treat concatenation as multiplication, and we might
be working in a world in which order of multiplcation matters.
- Recall that there are three basic attribtues we want for control
- Locality: a statement should only have local effect
- Limited entry: only a limited number of ways to enter
the control structure
- Limited exit: only a limited number of ways out of the control
- Blocks illustrate many of the principles.
- A block is a sequence of statements treated as a logical whole
and typically surrounded by some key words
or key symbols
- Limited entry
- In most languages, you must enter a block at the top.
- This means that you cannot directly jump to any statement.
- This means that we only need to consider the effect of
local statements (those within a block) that
precede a statement.
- Limited exit
- While you may exit the block in multiple ways (e.g., doing all
the statements or encountering a break), there is only one
- This makes it easier to keep track of what happens next, or what
we need to happen within the block.
- A conditional is a form of control in which the statements executed
are determined by one or more tests. Common conditionals include
If Statements and Case Statements.
- In some sense, conditionals select which statements to execute.
- Conditionals are both useful and important as they allow programs to
exhibit significantly different behavior (in terms of the statements
executed) depending on the inputs.
- Languages without some form of selection have less power (formally
- While we have a general sense of the simplest conditionals (conditional
branches and if statements), there are many design issues in
- A Case Statement consists of a case expression and a sequence
of value/statement pairs. The expression is evaluated and used to
select a particular value/statement pair.
- There are a number of interesting design decisions in case statements:
- What are the possible types of values in the pairs?
- Must those values form a continuous range?
- Do we permit a "default" value?
- What do the values "look like"? Must they be constants? Constant
expressions? Can they be variables?
- Can multiple value/statement pairs have the same value? If so, what is
- Created Tuesday, January 19, 1999 as a blank outline.
- Filled in the details on Wednesday, April 28, 1999.
Many details were based on
outline 16 and
outline 17 of
- Removed uncovered material (all on loops) on Sunday, May 2, 1999.