Professional Documents
Culture Documents
Week 5
Week 5
Assignments
• Symbol
– Most contemporary languages: =
– ALGOL 60, Ada: :=
• Fortran, Ada
– Assignment can only appear as a stand-alone statement with one
variable on the left-hand side.
• Conditional target (Perl):
($flag ? $count1 : $count2) = 0;
• Compound assignment
sum += value
• Unary assignment operators (C-based languages, Perl, JavaScript)
sum = ++count; sum = count++;
– Associativity
-count++ (-count)++
© M. Winter 5.1
COSC 2P05 – Programming languages
© M. Winter 5.2
COSC 2P05 – Programming languages
Control Statements
Control statements alter the flow
•Early languages such as Fortran used GOTOs
•Two control statements necessary to implement any algorithm: selection
and iteration
•Design issue
– Does the control structure have multiple entries?
•Multiple exists
– Multiple exists in control structures correspond to GOTOs.
© M. Winter 5.3
COSC 2P05 – Programming languages
Selection Statements
A selection statement provides the means of choosing between two or
more execution paths in a program.
© M. Winter 5.4
COSC 2P05 – Programming languages
– Clause form
• Single statement or compound statement
• Perl: always compound statement
• Fortran 95, Ada, Python, Ruby: Clauses are statement sequences
terminated by a reserved word (Python uses indentation).
– Nesting selectors
<if_stmt> → if <logic_expr> then <stmt>
| if <logic_expr> then <stmt> else
<stmt>
result = 0; result = 0
else else
result = 1; result = 1
end
end
© M. Winter 5.5
COSC 2P05 – Programming languages
• Multiple-Selection Constructs
– 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?
– C-multiple-selector construct switch (C, C++, Java, JavaScript)
switch (expression) {
case constant_expression_1: statement_1;
. . .
case constant_n: statement_n;
[default: statement_n+1]
}
Use of break !
© M. Winter 5.6
COSC 2P05 – Programming languages
© M. Winter 5.7
COSC 2P05 – Programming languages
Iterative Statements
An iterative statement is one that causes a statement or collection of
statements to be executed zero, one, or more times.
•Design issues:
– How is the iteration controlled?
– Where should the control mechanism appear in the loop
statement?
•Body of an iterative construct
•Pre-test vs Post-test
© M. Winter 5.8
COSC 2P05 – Programming languages
• Counter-Controlled Loops
– 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?
– For Statements of C-based languages
for (expression_1; expression_2; expression_3)
loop body
• All components are optional
• Equivalent to a while loop
© M. Winter 5.9
COSC 2P05 – Programming languages
© M. Winter 5.10
COSC 2P05 – Programming languages
• Logically controlled loops.
– Design issues:
• Should the control be pretest or post-test?
• Should the logically controlled loop be a special form of a counting
loop or a separate statement?
– C-based languages
while (control_exp) do
loop body loop body
while (control_exp);
– User-located Loop Control Mechanism
• C,C++, Python, Ruby, and C# have an unconditional unlabeled exit
(break).
• Java and Pearl have an unconditional labeled exit (break):
outerLoop:
for (row = 0; row < numRows; row++)
for (col = 0; col < numCols; col++) {
sum += mat[row][col];
if (sum > 1000.0) break outerLoop;
}
© M. Winter 5.11
COSC 2P05 – Programming languages
© M. Winter 5.12
COSC 2P05 – Programming languages
reset $list;
print ("First number: " + current($list) + "");
while ($current_value = next($list))
print ("Next number: " + $current_value + "<br \>");
–Java enhanced version of the for statement
for (String myElement : myList) { . . . }
–C# foreach loop
foreach (String name in names)
Console.WriteLine(name);
© M. Winter 5.13
COSC 2P05 – Programming languages
Unconditional Branching
An unconditional branch statement , or goto, transfers execution control
to a specified location in the program.
•goto is the most powerful statement for controlling the flow of execution
of a program’s statements.
•Careless use of the goto can lead to serious problems.
•Without usage restrictions, imposed by either language design or
programming standards, goto statements can make programs very
difficult to read, and as a result, highly unreliable and costly to maintain.
•Java, Python, and Ruby do not have a goto statement.
© M. Winter 5.14
COSC 2P05 – Programming languages
Guarded Commands
• Introduced by Dijkstra (1975)
• Basis for the linguistic mechanism developed later for concurrent
programming.
© M. Winter 5.15