COSC 2 P 05 Programming languages Assignments Symbol

COSC 2 P 05 – Programming languages 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 ? $count 1 : $count 2) = 0; • • Compound assignment sum += value • Unary assignment operators (C-based languages, Perl, Java. Script) sum = ++count; sum = count++; – Associativity -count++ © M. Winter (-count)++ 5. 1

COSC 2 P 05 – Programming languages • • Assignment as an expression (C-based languages, Perl, Java. Script) while (ch = get. Char() != EOF) { … } – Expression side-effect! a = b + (c = d / b) – 1 – Multiple-target assignment sum = count = 0 – C syntax hides errors if (x = y) { … } List assignments, multiple-target assignments (Perl, Ruby, Lua) ($first, $second, $third) = (20, 40, 60); ($first, $second) = ($second, $first); © M. Winter 5. 2

COSC 2 P 05 – 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 2 P 05 – Programming languages Selection Statements A selection statement provides the means of choosing between two or more execution paths in a program. • Two-Way Selection Statements if control_expression then clause else clause – Design issues: • What is the form and type of the expression that controls the selection? • How are then and else clauses specified? • How should the meaning of nested selectors be specified? – Control expression: Boolean data type vs arithmetic expression © M. Winter 5. 4

COSC 2 P 05 – 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> if (sum == 0) if (count == 0) result = 0; else result = 1; © M. Winter if sum == if count result else result end 0 then == 0 then = 0 = 1 5. 5

COSC 2 P 05 – 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. Script) 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 2 P 05 – Programming languages – C# disallows the implicit execution of more than one segment; break or goto case n. – Ada case when Boolean_expression then expression. . . when Boolean_expression then expression [else expression] end Subranges and choice lists; 10 | 15 | 20; allowed. © M. Winter 5. 7

COSC 2 P 05 – 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 2 P 05 – 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 2 P 05 – Programming languages – For Statements of Ada for variable in [ reverse ] discrete_range loop. . . end loop; • A discrete range is a subrange of integer or an enumeration type. • Implicit variable declaration with scope the loop body. • Variable cannot be modified in the body. – For Statements of Python for loop_variable in object: - loop body [else: - else clause] for count in [2, 4, 6]: print count • Else clause is executed if the loops terminates normally. © M. Winter 5. 10

COSC 2 P 05 – 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 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): outer. Loop: for (row = 0; row < num. Rows; row++) for (col = 0; col < num. Cols; col++) { sum += mat[row][col]; if (sum > 1000. 0) break outer. Loop; } © M. Winter 5. 11

COSC 2 P 05 – Programming languages • • C, C++, and Python include an unlabeled control statement, continue. while (sum < 1000) { getnext(value); if (value < 0) continue; sum += value; } Iteration based on Data Structures – A general data-based iteration statement uses a user- defined data structure and a user-defined function (the iterator) to go through the structure’s elements. – Using C’s For loop: for (ptr = root; ptr == null; ptr = traverse(ptr)){. . . } – Predefined iterators are used to provide iterative access to PHP’s unique arrays. © M. Winter 5. 12

COSC 2 P 05 – 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 my. Element : my. List) {. . . } – C# foreach loop foreach (String name in names) Console. Write. Line(name); © M. Winter 5. 13

COSC 2 P 05 – 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 2 P 05 – Programming languages Guarded Commands • • Introduced by Dijkstra (1975) Basis for the linguistic mechanism developed later for concurrent programming. if [] [] [] Fi <Boolean expression> -> <statement>. . . <Boolean expression> -> <statement> – All of the Boolean expressions are evaluated each time. – If more than one expression is true, one of the corresponding statements can be nondeterministically chosen for execution. © M. Winter 5. 15
- Slides: 15