PPL Chapter 8
PPL Chapter 8
Programming Languages
Dr. R. Bala Krishnan
Asst. Prof.
Dept. of CSE
NIT, Trichy – 620 015
Ph: 999 470 4853 E-Mail: balakrishnan@nitt.edu
Books
• Text Books
Robert W. Sebesta, “Concepts of Programming Languages”, Tenth
Edition, Addison Wesley, 2012.
Michael L. Scott, “Programming Language Pragmatics”, Third Edition,
Morgan Kaufmann, 2009.
• Reference Books
Allen B Tucker, and Robert E Noonan, “Programming Languages –
Principles and Paradigms”, Second Edition, Tata McGraw Hill, 2007.
R. Kent Dybvig, “The Scheme Programming Language”, Fourth
Edition, MIT Press, 2009.
Jeffrey D. Ullman, “Elements of ML Programming”, Second Edition,
Prentice Hall, 1998.
Richard A. O'Keefe, “The Craft of Prolog”, MIT Press, 2009.
W. F. Clocksin, C. S. Mellish, “Programming in Prolog: Using the ISO
Standard”, Fifth Edition, Springer, 2003.
2
Chapters
Chapter No. Title
1. Preliminaries
2. Evolution of the Major Programming Languages
3. Describing Syntax and Semantics
4. Lexical and Syntax Analysis
5. Names, Binding, Type Checking and Scopes
6. Data Types
7. Expressions and Assignment Statements
8. Statement-Level Control Structures
9. Subprograms
10. Implementing Subprograms
11. Abstract Data Types and Encapsulation Constructs
12. Support for Object-Oriented Programming
13. Concurrency
14. Exception Handling and Event Handling
15. Functional Programming Languages
3
16. Logic Programming Languages
Chapter 8 – Statement-Level Control
Structures
4
Objectives
• Overview of the evolution of control statements
5
Introduction
• Computations in imperative-language programs are accomplished by
evaluating expressions and assigning the resulting values to variables
• One of the primary conclusions of these efforts was that, although a single
control statement (a selectable goto) is minimally sufficient, a language that is
designed not to include a goto needs only a small number of different control
statements 6
Introduction
• It was proven that all algorithms that can be expressed by
flowcharts can be coded in a programming language with only two
control statements
- One for choosing between two control flow paths (switch, if)
- One for logically controlled iterations (for, while, do)
• Programmers care less about the results of theoretical research on
control statements than they do about writability and readability
• Rather than requiring the use of a logically controlled loop
statement for all loops, it is easier to write programs when a
counter-controlled loop statement can be used to build loops that
are naturally controlled by a counter
• Primary factor that restricts the number of control statements in a
language is readability
• Too few control statements can require the use of lower-level
statements, such as the goto, which also makes programs less
readable 7
Introduction
• How much a language should be expanded to increase its writability
at the expense of its simplicity, size and readability
• A control structure is a control statement and the collection of
statements whose execution it controls
• Only one design issue that is relevant to all of the selection and
iteration control statements
- Should the control structure have multiple entries?
• All selection and iteration constructs control the execution of code
segments, and the question is whether the execution of those code
segments always begins with the first statement in the segment
• It is now generally believed that multiple entries add little to the
flexibility of a control statement, relative to the decrease in
readability caused by the increased complexity
• Note that multiple entries are possible only in languages that
include gotos and statement labels
8
Introduction
• Reader might wonder why multiple exits from control structures are
not considered a design issue
• Reason is that all programming languages allow some form of
multiple exits from control structures, the rationale being as follows
- If all exits from a control structure are restricted to
transferring control to the first statement following the
structure, where control would flow if the control structure
had no explicit exit, there is no harm to readability and also
no danger
- If an exit can have an unrestricted target and therefore can
result in a transfer of control to anywhere in the program
unit that contains the control structure, the harm to
readability is the same as for a goto statement anywhere else
in a program
• Languages that have a goto statement allow it to appear anywhere,
including in a control structure
• Therefore, the issue is the inclusion of a goto, not whether multiple
exits from control expressions are allowed 9
Miscellaneous
for(i = 0; i < n; i++) # 60 for(i = 0; i < n; i++)
{ # 61 {
if (i > 10) # 62 if (i > 10)
break; # 63 goto x; // x can be in line # 10, 110, 120, ...
else if (i > 5 && i < 10 && j = 0) # 64 else if (i > 5 && i < 10 && j = 0)
break; # 65 goto y; // y can be in line # 8, 1000, 1100, ...
else if (i > 0 and i < 5 && j =0) # 66 else if (i > 0 and i < 5 && j =0)
break; # 67 goto z; // z can be in line # 18, 810, 820, ...
else # 68 else
# 69 i++;
i++;
} # 70 }
int z =10; # 71 int. z =10;
.
# 72 .
.
10
Selection Statements
• Selection statement provides the means of choosing between
two or more execution paths in a program
• Selection statements fall into two general categories
- Two-way
- n-way or multiple selection
Two-way Selection Statements
Design Issues
• What is the form and type of the expression that controls the
selection?
• How are the then and else clauses specified?
• How should the meaning of nested selectors be specified?
11
Selection Statements
Control Expression
• Control expressions are specified in parentheses if the then
reserved word (or some other syntactic marker) is not used to
introduce the then clause
bool c = true;
if (c) { ... } (or) if c then
if (x > 5) { ... } (or) if x > 5 then
Clause Form
• In many contemporary languages, the then and else clauses
appear as either single statements or compound statements
if (x > 5) if (x > 5): if (x > 5) if (x > 5)
{ ....; ....; ....;
....; ....; ....; ....;
....; printf() end if; fi;
} 12
Selection Statements
Nesting Selectors
Ruby
Python
15
Multiple Selection Constructs
• Multiple-selection statement allows the
selection of one of any number of
statements or statement groups ->
Generalization of a selector
• Need to choose from among more than
two control paths in a program is
common
• Multiple selector can be built from two-
way selectors and gotos
- Resulting structures are
cumbersome, unreliable, and
difficult to write and read
• Need for a special structure is clear
16
Multiple Selection Constructs
Design Issues
• Type of expression on which the selector is based
• Whether single statements, compound statements, or statement
sequences may be selected
• Whether only a single selectable segment (case) can be executed
when the statement is executed
• Form of the case value specifications
• What should result from the selector expression evaluating to a
value that does not select one of the segments
- Such a value would be unrepresented among the selectable
segments
- Simply disallow the situation from arising and have the
statement do nothing at all when it does arise
17
Multiple Selection Constructs
Summary of Design Issues
• What is the form and type of the expression that controls the
selection?
• How are the selectable segments specified?
• Is execution flow through the structure restricted to include
just a single selectable segment?
• How are the case values specified?
• How should unrepresented selector expression values be
handled, if at all?
18
Multiple Selection Constructs
Examples of Multiple Selectors
20
Multiple Selection Constructs
• Switch statement does not provide implicit branches at the
end of its code segment
• This allows control to flow through more than one selectable
code segment on a single execution
switch (index) {
This code prints the error
case 1:
message on every execution.
case 3: odd += 1;
Likewise, the code for the 2
sumodd += index;
and 4 constants is executed
case 2:
every time the code at the 1
case 4: even += 1; or 3 constants is executed
sumeven += index;
default: printf("Error in switch, index = %d\n", index);
}
23
Multiple Selection Constructs
C#
24
Multiple Selection using if
• In many situations, a switch or case construct is inadequate
for multiple selection
• Eg: When selections must be made on the basis of a boolean
expression rather than some ordinal type, nested two-way
selectors can be used to simulate a multiple selector
• Notice that this example is not easily simulated with a switch
statement, because each selectable statement is chosen on
the basis of a Boolean expression
• Else-if construct is not a redundant form of switch
29
Iterative Statements
Summary of these design issues
30
The for Statement of the C-Based
Languages
37
User-Located Loop Control
Mechanisms
• Sometimes, it is convenient for a programmer to choose a
location for loop control other than the top or bottom of the
loop body
• Some languages provide this capability
• Such loops have the structure of infinite loops but include
user-located loop exits
• Question is whether a single loop or several nested loops can
be exited
Design Issues
• Should the conditional mechanism be an integral part of the
exit?
• Should only one loop body be exited, or can enclosing loops
also be exited?
38
User-Located Loop Control
Mechanisms
• C, C++, Python, Ruby, and C# have unconditional unlabeled exits
(break)
42
Unconditional Branching
• Unconditional branch statement transfers execution control to a specified
location in the program
• Goto has stunning power and great flexibility (all other control structures
can be built with goto and a selector), but it is this power that makes its
use dangerous
48
Miscellaneous
• Let, q1 = 4; q2 = 3; q3 = 1; q4 = 2
Iteration 1
q1 > q2 -> True; q2 > q3 -> True; q3 > q4 -> False
• q1 > q2 -> temp = 4; q1 = 3; q2 = 4 // q1 = 3; q2 = 4; q3 = 1; q4 = 2
Iteration 2
q1 > q2 -> False; q2 > q3 -> True; q3 > q4 -> False
• q2 > q3 -> temp = 4; q2 = 1; q3 = 4 // q1 = 3; q2 = 1; q3 = 4; q4 = 2
Iteration 3
q1 > q2 -> True; q2 > q3 -> False; q3 > q4 -> True
• q1 > q2 -> temp = 3; q1 = 1; q2 = 3 // q1 = 1; q2 = 3; q3 = 4; q4 = 2
Iteration 4
q1 > q2 -> False; q2 > q3 -> False; q3 > q4 -> True
• q3 > q4 // q1 = 1; q2 = 3; q3 = 2; q4 = 4
Iteration 5
q1 > q2 -> False; q2 > q3 -> True; q3 > q4 -> False
• q2 > q3 // q1 =1; q2 = 2; q3 = 3; q4 = 4
Iteration 6
• q1 > q2 -> False; q2 > q3 -> False; Q3 > q4 -> False // q1 = 1; q2 = 2; q3 = 3; q4 = 4
49
Guarded Commands
• Dijkstra’s guarded command control statements are
interesting, in part because they illustrate how the syntax and
semantics of statements can have an impact on program
verification and vice versa
51