0% found this document useful (0 votes)
10 views

PPL-Unit 2 Part 7

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

PPL-Unit 2 Part 7

Uploaded by

thirumal536
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Principle of programming languages

Unit-II: Control Structures – introduction, selection


statements, iterative
statements, unconditional branching, guarded commands.
Control Structures –
introduction
•some means of selecting among alternative control
flow paths of statement execution and some means of
causing the repeated execution of statements or
sequences of statements. Statements that provide
these kinds of capabilities are called control
statements.
•control structure is a control statement and the
collection of statements whose execution it controls.
Selection Statements
selection statement:
•A 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 and
n-way or multiple selection.

Two-Way Selection Statements:


The general form of a two-way selector is as follows:
if control_expression
then clause
else clause
Design Issues:
•What is the form and type of the expression that
controls the selection?
Selection Statements
The Control Expression:
•Control expressions are specified in parentheses if the
then reserved word is not used

Clause Form
Many languages use braces to form compound
statements, which serve as the bodies of then and else
clauses.
In Fortran 95, Ada, Python, and Ruby, the then and else
clauses are statement sequences,rather than
compound statements.
For example in Python
if x > y :
x=y
print "case 1"
Selection Statements
Nesting Selectors
when a selection statement is nested in the then
clause of a
selection statement, it is not clear to which if an else
clause should be associated
example in java
if (sum == 0)
if (count == 0)
result = 0;
else
result = 1;
In Java, as in many other imperative languages, the
static semantics of the language specify that the else
clause is always paired with the nearest previous
unpaired then clause
Selection Statements
Alrenative in java
if (sum == 0) {
if (count == 0)
result = 0;
}
else
result = 1;
•Perl requires that all then and else clauses be
compound
if (sum == 0) {
if (count == 0) {
result = 0;
}
} else {
result = 1;
}
Selection Statements
in Ruby as follows:
if sum == 0 then
if count == 0 then
result = 0
else
result = 1
end
End
•The end reserved word closes the nested if, it is clear
that the else
clause is matched to the inner then clause
Selection Statements
Multiple-Selection Statements:
The multiple-selection statement allows the
selection of one of any number of statements or
statement groups.
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?
Selection Statements
Examples of Multiple Selectors
•The C multiple-selector statement, switch, which is
also part of C++, Java and JavaScript.Its general
form is
switch (expression) {
case constant_expression1:statement1;
...
case constantn: statement_n;
[default: statementn+1]
}
•where the control expression and the constant
expressions are some discrete type. This includes
integer types, as well as characters and enumeration
types.
•The selectable statements can be statement
sequences, compound statements,or blocks.
Selection Statements
switch statement uses break to restrict each execution
to
a single selectable segment:
switch (index) {
case 1:
case 3: odd += 1;
sumodd += index;
break;
case 2:
case 4: even += 1;
sumeven += index;
break;
default: printf("Error in switch, index = %d\n",
index);
}
Selection Statements
In Ruby
case
when Boolean_expression then expression
...
when Boolean_expression then expression
[else expression]
End
case expression is that the Boolean expressions are
evaluated one at a time, top to bottom.
The value of the case expression is the value of the
first then expression whose Boolean expression is true.
the else clause is optional. For example
leap = case
when year % 400 == 0 then true
when year % 100 == 0 then false
else year % 4 == 0
Selection Statements
Multiple Selection Using if:
Multiple Selection ,when selections must be made on
the basis of a Boolean expression rather than some
ordinal type.
Consider the following Python selector statement
if count < 10 :
bag1 = True
elif count < 100 :
bag2 = True
elif count < 1000 :
bag3 = True
Iterative Statements
An iterative statement is one that causes a
statement or collection of statements to be
executed zero, one, or more times. An iterative
statement is often called a loop.

Design issues:
•How is the iteration controlled?
• Where should the control mechanism appear in the
loop statement?
Counter-Controlled Loops
•A counting iterative control statement has a variable,
called the loop variable,in which the count value is
maintained.
•It also includes some means of specifying the initial
and terminal values of the loop variable, and the
difference between sequential loop variable values,
Iterative Statements
Design Issues:
•What are the type and scope of the loop variable?
• Should it be legal for the loop variable or loop
parameters to be changed
in the loop, and if so, does the change affect loop
control?
• Should the loop parameters be evaluated only once,
or once for every iteration?
The for Statement of the C-Based Languages
The general form of C’s for statement is
for (expression_1; expression_2; expression_3)
loop body
Following is an example of a skeletal C for statement:
for (count = 1; count <= 10; count++)
...
}
Iterative Statements
The Ada for Statement
The Ada for statement has the following form:
for variable in [reverse] discrete_range loop
...
end loop;
•A discrete range is a subrange of an integer or
enumeration type, such as 1..10 or Monday..Friday.
•The reverse reserved word, when present, indicates
that
the values of the discrete range are assigned to the
loop variable in reverse order.
Iterative Statements
The for Statement of Python
The general form of Python’s for is
for loop_variable in object:
- loop body
[else:
- else clause]
The loop variable is assigned the value in the object,
which is often a range, one for each execution of the
loop body.
The else clause, when present, is executed if the loop
terminates normally.
Consider the following example:
for count in [2, 4, 6]:
print count
Iterative Statements
Counter-Controlled Loops in Functional
Languages
The general form of an F# function for simulating
counting loops, named forLoop
let rec forLoop loopBody reps =
if reps <= 0 then
()
else
loopBody()
forLoop loopBody, (reps - 1);
•In this function, the parameter loopBody is the
function with the body of the loop and the parameter
reps is the number of repetitions.
•The reserved word rec appears before the name of the
function to indicate that it is recursive. The empty
parentheses do nothing
Iterative Statements
Logically Controlled Loops
In Logically Controlled Loops collections of statements
must be repeatedly executed, but the repetition control
is based on a Boolean expression rather than a counter
Design Issues:
•Should the control be pretest or posttest?
• Should the logically controlled loop be a special form
of a counting loop or a separate statement?
Examples:
The C-based programming languages include both
pretest and posttest logically controlled loops.
The general form:
while (control_expression)
loop body
Iterative Statements
do
loop body
while (control_expression);
•In the pretest version of a logical loop (while), the
statement or statement segment is executed as
long as the expression evaluates to true.
• In the posttest version (do), the loop body is
executed until the expression evaluates to false.
•The only real difference between the do and the
while is that the do always causes the loop body to
be executed at least once.
Iterative Statements
User-Located Loop Control Mechanisms:
In some situations, it is convenient for a programmer to
choose a location for loop control other than the top or
bottom of the loop body.
C, C++, Python, Ruby, and C# have unconditional
unlabeled exits break.
Java and Perl have unconditional labeled exits break
in Java, last in Perl.
C, C++, and Python include an unlabeled control
statement, continue,This is not an exit but rather a
way to skip the rest of the loop statements on the
current iteration without terminating the loop structure.
For example
while (sum < 1000) {
getnext(value);
if (value < 0) continue;
Iterative Statements
For example
while (sum < 1000) {
getnext(value);
if (value < 0) continue;
sum += value;
}

while (sum < 1000) {


getnext(value);
if (value < 0) break;
sum += value;
}
Unconditional
Branching
•An unconditional branch statement transfers
execution control to a specified location in the
program.
•Theunconditional branch, or goto, is the most powerful
statement for controlling the flow of execution of a
program’s statements.
•goto statements can make programs very difficult to
read, and as a result, highly unreliable and costly to
maintain.
• c,c++,c# support the goto statements.
•A few languages have been designed without a goto—for
example, Java, Python, and Ruby.
Guarded Commands
•New and quite different forms of selection and loop
structures were suggested by Dijkstra (1975).This named
as guarded commands
•Guarded commands are supported by ada and csp
languages.
Dijkstra’s selection statement :
if <Boolean expression> -> <statement>
[ ] <Boolean expression> -> <statement>
[]...
[ ] <Boolean expression> -> <statement>
fi
•The closing reserved word, fi, is the opening reserved
word spelled backward.
•The smallblocks, called fatbars, are used to separate the
guarded clauses and allow the clauses to be statement
Guarded Commands
•This selection statement has the appearance of a
multiple selection, but its semantics is different.
• All of the Boolean expressions are evaluated each time
the statement is reached during execution.
•If more than one expression is true, one of the
corresponding statements can be nondeterministically
chosen for execution.
•An implementation may always choose the statement
associated with the first Boolean expression that
evaluates to true.
•If none of the Boolean expressions is true, a run-time
error occurs that causes program termination.
Consider the following example:
if i = 0 -> sum := sum + i
[ ] i > j -> sum := sum + j
Guarded Commands
•If i = 0 and j > i, this statement chooses
nondeterministically between the first and third
assignment statements.
•If i is equal to j and is not zero, a runtime error occurs
because none of the conditions is true.

Dijkstra The loop structure:


do <Boolean expression> -> <statement>
[ ] <Boolean expression> -> <statement>
[]...
[ ] <Boolean expression> -> <statement>
od
•The semantics of this statement is that all Boolean
expressions are evaluated on each iteration.
• If more than one is true, one of the associated
Guarded Commands
•When all expressions are simultaneously false, the loop
terminates.
Example: Sorting
do q1 > q2 -> temp := q1; q1 := q2; q2 := temp;
[ ] q2 > q3 -> temp := q2; q2 := q3; q3 := temp;
[ ] q3 > q4 -> temp := q3; q3 := q4; q4 := temp;
od

You might also like