130 likes | 322 Views
COP4020 Programming Languages. Guarded Commands. COP4020 Programming Languages. Control statements were implemented ( in the 50s) in Fortran on the IBM704. Corrado Böhn and Giusseppe Jacopini (1966) proposed selection and iteration as
E N D
COP4020 Programming Languages Guarded Commands
COP4020 Programming Languages • Control statements were implemented ( in the 50s) in Fortran on the IBM704. • CorradoBöhn and GiusseppeJacopini (1966) proposed selection and iteration as • control statements to describe how to construct structured flow charts. • C. Böhm, G. Jacopini, "Flow diagrams, Turing Machines and Languages with only Two Formation Rules", Comm. of the ACM, 9(5): 366-371,1966 • http://www.cs.unibo.it/~martini/PP/bohm-jac.pdf • EdsgerDijkstra (1970) proposed structured programming using three control • structures: sequential execution, selection, and iteration. • http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF
COP4020 Programming Languages Control structures Sequential execution: S1:S2; S3; … ; Sn. Selection: (ADA) if x > y then S1; S2; else S3; S4; endif
COP4020 Programming Languages Control structures Multiple Selection: (ALGOL, ADA, C, Java, C++) switch (expression) { case constant_exp_1: statement_1; case constant_exp_2: statement_2; . . . caseconstant_exp_n:statement_n; [default: constant_exp_n+1: statement_n;]
COP4020 Programming Languages Control structures Example in ALGOL W case (expression) is when c hoice_list statement_sequence; when c hoice_list statement_sequence; . . . when c hoice_list statement_sequence; [whenothers c hoice_list statement_sequence; endcase
COP4020 Programming Languages Control structures Iteration statements (loop) Fortran Do 20 count = 1, 10 S1. S2. 20 continue ADA For count in 1 .. 10 loop S1; S2; endloop
COP4020 Programming Languages Control structures Iteration statements (loop) while while (expression) Loop body; Repeat Do S1; S2; until (expression);
COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) In the paper Guarded Commands, Nondeterminacy, and Formal Derivation of programs, Dijkstra proposed new and quite different forms of selection and loop control structures. <guarded command> ::= <guard> <guard list> <guard> ::= <booleanexpession> <guarded list> ::= <statement> {; <statement} <guarded command set> ::= <guarded command> {▯ <guarded command>} <alternative construct> ::= if <guarded command set> fi<repetitive construct > ::= do <guarded command set> od<statement> ::= <alternative construct> ∣ <repetitive construct> ∣"other statements".
COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) The alternative command If <boolean expression> <statement> ▯ <boolean expression> <statement> ▯ … ▯ <boolean expression> <statement> fi Semantics All the boolean expressions are evaluated each time the construct is reached during execution. If more than one of the boolean expressions or guards are true, one of the corresponding statements is chosen at random for execution. If none of the guards is true, a run time error occurs that causes program termination.
COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) The alternative command (Example) If i = 0 sum := sum + i ▯ i > j sum := sum + j ▯ j > i sum := sum + j fi if i = 0 an d j > i the construct chooses nondeterministically between the first and the last statements If i = j and i != 0 a run time error occurs. Example 2: (find the largest of two numbers) If x ≥ y max := x ▯ y ≥ x max := y fi The orden of execution is irrelevant.
COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) The iterative command do <boolean expression> <statement> ▯ <boolean expression> <statement> ▯ … ▯ <boolean expression> <statement> od Semantics All the boolean expressions are evaluated each iteration. If more than one of the boolean expressions or guards are true, one of the corresponding statements is nonderterministically chosen for execution. Once the statements are executed the guards are evaluated again. If all the guards is false, the loop terminates.
COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) The iterative command (Example: sorting) q1 , q2 , q3 , q4 := Q1, Q2, Q3, Q4; do q1 > q2 T:= q1; q1:= q2; q2:= T; ▯ q2 > q3 T:= q2; q2:= q3; q3:= T; ▯ q3 > q4 T:= q3; q3:= q4; q4:= T; od. Initial values Q1, Q2, Q3, Q4 are asiggned simultaneously to variables q1 , q2 , q3 , q4 .
COP4020 Programming Languages The End