PPL-Unit 2 Part 7
PPL-Unit 2 Part 7
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;
}