Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICE1341 Programming Languages Spring 2005 Lecture #14 Lecture #14 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.

Similar presentations


Presentation on theme: "ICE1341 Programming Languages Spring 2005 Lecture #14 Lecture #14 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."— Presentation transcript:

1 ICE1341 Programming Languages Spring 2005 Lecture #14 Lecture #14 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University (ICU)

2 Spring 2005 2 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 8 – Statement-Level Control Structures Chapter 8 – Statement-Level Control Structures Selection Statements Selection Statements Iterative Statements Iterative Statements Last Lecture

3 Spring 2005 3 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Chapter 8 – Statement-Level Control Structures Chapter 8 – Statement-Level Control Structures Unconditional Branching Unconditional Branching Guarded Commands Guarded Commands FORTRAN Programming FORTRAN Programming

4 Spring 2005 4 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Counter-Controlled Loops Design Issues: Design Issues: 1. What are the type and scope of the loop variable? 2. What is the value of the loop variable at loop termination? 3. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control? 4. Should the loop parameters be evaluated only once, or once for every iteration? DO 20 N = 1, 100, 3 DO 20 N = 1, 100, 3 20 SUM = SUM + N Loop Variable Initial Value Terminal Value Stepsize Loop Parameters

5 Spring 2005 5 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN ‘DO’ Loop Syntax: DO label var = start, finish [, stepsize] Syntax: DO label var = start, finish [, stepsize] Stepsize can be any value but zero Stepsize can be any value but zero Parameters can be expressions Parameters can be expressions Design Choices: Design Choices: 1. Loop variable must be integer 2. Loop variable always has its last value 3. The loop variable cannot be changed in the loop, but the parameters can 4. Loop parameters are evaluated only once * AW Lecture Notes

6 Spring 2005 6 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University ALGOL 60 ‘For’ Loop Syntax: for var := do statement where can have: Syntax: for var := do statement where can have: list of expressions list of expressions expression step expression until expression expression step expression until expression expression while boolean_expression expression while boolean_expression e.g., for index := 1 step 2 until 50, e.g., for index := 1 step 2 until 50, 60, 70, 80, 60, 70, 80, index + 1 until 100 do index + 1 until 100 do (index = 1, 3, 5, 7,..., 49, 60, 70, 80, 81, 82, 83,..., 100) Parameters are evaluated with every iteration, making it very complex and difficult to read Parameters are evaluated with every iteration, making it very complex and difficult to read * AW Lecture Notes

7 Spring 2005 7 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University ‘For’ Loops in C-based Languages Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement The expressions can be statement sequences, with the statements separated by commas or null The expressions can be statement sequences, with the statements separated by commas or null e.g., for (int i=0, j=10; j==i; i++, j--) e.g., for (int i=0, j=10; j==i; i++, j--) printf(“%d, %d”, i, j); printf(“%d, %d”, i, j); for (;;) … for (;;) … In Java, the control expression must be Boolean In Java, the control expression must be Boolean Design Choices: Design Choices: 1, 2. There is no explicit loop variable 3. Everything can be changed in the loop 4. expr_1 is evaluated once, others are evaluated with each iteration others are evaluated with each iteration Flexible!

8 Spring 2005 8 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Other Counter-Controlled Loops Pascal Pascal for variable := initial (to | downto) final do Ada Ada for var in [reverse] discrete_range loop...... end loop The loop variable is implicitly declared and undeclared as the loop begins and terminates The loop variable is implicitly declared and undeclared as the loop begins and terminates e.g., Count : Float := 1.35; for Count in 1..10 loop for Count in 1..10 loop Sum := Sum + Count; end loop end loop Count is an integer loop variable Count is a float variable

9 Spring 2005 9 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Logically Controlled Loops Posttest version executes the loop body at least once Posttest version executes the loop body at least once e.g., At the above examples, what happens if n is already greater than 100 before reaching to the loop? Pascal – while … do, repeat … until Pascal – while … do, repeat … until Ada and Perl support only pretest versions Ada and Perl support only pretest versions FORTRAN 77 and 90 support neither version FORTRAN 77 and 90 support neither version while (n <= 100) { sum += n; n += 3; } do { sum += n; n += 3; } while (n <= 100) PretestPosttest

10 Spring 2005 10 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University User-Located Loop Control Design Issues: 1. Should the conditional be part of the exit? C-based – unconditional Ada – conditional (exit when …) 2. Can control be transferable out of more than one loop? Java, C#, Perl – Yes while (n <= 100) { sum += n; if (sum == m) continue; n += 3; } while (n <= 100) { sum += n; if (sum == m) break; n += 3; } out: for (int i=0; i<k; i++) { while (n <= 100) { sum += n; sum += n; if (sum == m) if (sum == m) break out; break out; n += 3; n += 3; }} Java

11 Spring 2005 11 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Iteration Based on Data Structures & Unconditional Branching IBDS: Use order and number of elements of some data structures to control iteration IBDS: Use order and number of elements of some data structures to control iteration Unconditional Branching (Goto) Unconditional Branching (Goto) Problem: readability – Spaghetti Logic Problem: readability – Spaghetti Logic Some languages do not have them: e.g., Java, Modular-2 Some languages do not have them: e.g., Java, Modular-2 Loop exit statements are restricted and somewhat camouflaged goto’s Loop exit statements are restricted and somewhat camouflaged goto’s String[] wdays = { “Mon”, “Tue”, “Wed”, “Thu”, “Fri” }; … foreach (String name in wdays) Console.WriteLine(“Work Day: {0}”, name); C#

12 Spring 2005 12 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Guarded Commands (Dijstra, 1975) If more than one are true, choose one nondeterministically If more than one are true, choose one nondeterministically Runtime error when non of the conditions is true Runtime error when non of the conditions is true if i = 0 -> sum := sum + i [] i > j -> sum := sum + j [] j > I -> sum := sum + I fi  Allow verification during program development  Can be used to represent concurrency 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 If more than one are true, choose one nondeterministically; then start loop again; If none are true, exit loop If more than one are true, choose one nondeterministically; then start loop again; If none are true, exit loop

13 Spring 2005 13 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN (Formula Translating System) Designed to efficiently translate mathematical formulas into IBM 704 machine code Designed to efficiently translate mathematical formulas into IBM 704 machine code IBM 704 (1954) “The first mass-produced computer with core memory and floating- point arithmetic” Photo: Lawrence Livermore National Laboratory

14 Spring 2005 14 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN Coding Format FORTRAN programs were written in a coding form with a strict formatting rule Continuation (6) FORTRAN Statement (7-72) Identification Sequence (73-80) Statement Number (1-5) J.W. Perry Cole, ANSI FORTRAN IV, wcb

15 Spring 2005 15 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Punched Cards Coded FORTRAN programs were converted onto punched cards for loading Photos: http://www.tno.nl/instit/fel/museum/computer/en/punchcards.html

16 Spring 2005 16 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Early FORTRAN Versions FORTRAN I (1957) FORTRAN I (1957) Names could have up to six characters (IBM 704 has a 6-bit BCD character set and 36-bit word) Names could have up to six characters (IBM 704 has a 6-bit BCD character set and 36-bit word) Post-test counting loop ( DO ), Formatted I/O, User- defined subprograms, Three-way selection statement (arithmetic IF ) Post-test counting loop ( DO ), Formatted I/O, User- defined subprograms, Three-way selection statement (arithmetic IF ) No data typing statements (Implicit Type Declaration – Variables whose names begin with I, J, K, L, M, and N are integer types, all others are floating point) No data typing statements (Implicit Type Declaration – Variables whose names begin with I, J, K, L, M, and N are integer types, all others are floating point) FORTRAN II (1958) FORTRAN II (1958) Independent compilation of subroutines Independent compilation of subroutines * AW Lecture Notes

17 Spring 2005 17 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Later FORTRAN Versions FORTRAN IV (1960-62) – ANSI standard in 1966 FORTRAN IV (1960-62) – ANSI standard in 1966 Explicit type declarations Explicit type declarations Logical selection statement Logical selection statement Subprogram names could be parameters Subprogram names could be parameters FORTRAN 77 (1978) FORTRAN 77 (1978) Character string handling Character string handling Logical loop control statement Logical loop control statement IF-THEN-ELSE statement IF-THEN-ELSE statement FORTRAN 90 (1990) FORTRAN 90 (1990) Free coding format Free coding format Modules, Dynamic arrays, Pointers, Recursion, CASE statement Modules, Dynamic arrays, Pointers, Recursion, CASE statement * AW Lecture Notes

18 Spring 2005 18 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University A Sample FORTRAN Program C *** This is a sample FORTRAN IV program PROGRAM SAMPLE READ (5, 990) A, B 990FORMAT (F5.2, F5.2) SUM = A + B WRITE (6, 81) A, B, SUM 81FORMAT (1X, F5.2, 3X, F5.2, 3X, F6.2) STOPEND J.W. Perry Cole, ANSI FORTRAN IV, wcb

19 Spring 2005 19 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN Data Types (1) Integer Integer Implicit Typing: Variables whose names begin with I~N Implicit Typing: Variables whose names begin with I~N Explicit Declaration Explicit Declaration e.g., INTEGER A, TOTAL Real – Single precision floating point number Real – Single precision floating point number Implicit Typing: Variables whose names begin with other than A~H or O-Z Implicit Typing: Variables whose names begin with other than A~H or O-Z Explicit Declaration Explicit Declaration e.g., REAL J, R Double precision floating point number Double precision floating point number e.g., DOUBLE PRECISION A, X

20 Spring 2005 20 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN Data Types (2) Complex e.g., COMPLEX A, B, C Complex e.g., COMPLEX A, B, C A = (3.5, -7.24) A = (3.5, -7.24) B = (-8.21, 5.67) B = (-8.21, 5.67) C = A + B C = A + B Character Character e.g., CHARACTER C, NAME*20, ADDR*30 CHARACTER*20 STR1, STR2 CHARACTER*20 STR1, STR2 Character constants: e.g., ‘C’, ‘Go ICU’ Character constants: e.g., ‘C’, ‘Go ICU’ Hollerith Strings: e.g., 1HC, 5HGoICU Hollerith Strings: e.g., 1HC, 5HGoICU Logical (Boolean) Logical (Boolean) e.g., LOGICAL X, Y, FLAG Logical constants:.TRUE. or.FALSE. Logical constants:.TRUE. or.FALSE.

21 Spring 2005 21 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Variable Initializations and Arrays Variable Initializations Variable Initializations e.g., DATA A/1.0/, L/2/, B,C/4.0, 5.0/ DATA ARYX(3)/1.333/, T(1),T(2),T(3)/3*0.0/ DATA ARYX(3)/1.333/, T(1),T(2),T(3)/3*0.0/ DATA C(1)/1HS/, C(2)/1HD/, TAG/3HYes DATA C(1)/1HS/, C(2)/1HD/, TAG/3HYes Arrays Arrays e.g., DIMENSION A(5), B(10,7), N(3,5,20) INTEGER X(7,5) INTEGER X(7,5) REAL MATRIX(-6:4, 7, -5:10), K REAL MATRIX(-6:4, 7, -5:10), K DIMENSION K(20) DIMENSION K(20) J.W. Perry Cole, ANSI FORTRAN IV, wcb

22 Spring 2005 22 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN Operators Arithmetic Operators: +, -, *, /, ** (exponentiation) Arithmetic Operators: +, -, *, /, ** (exponentiation) Mixed mode expressions – evaluated in integer mode if all operands are integer, evaluated in real mode otherwise Mixed mode expressions – evaluated in integer mode if all operands are integer, evaluated in real mode otherwise e.g., 86.3 * K + R / 16.5 ** J  All are evaluated in real Relational Operators:.LT.,.LE.,.GT.,.GE.,.EQ.,.NE. Relational Operators:.LT.,.LE.,.GT.,.GE.,.EQ.,.NE. e.g., IF (RESULT.LT. 0.0) STOP Logical Operators:.AND.,.OR.,.NOT. Logical Operators:.AND.,.OR.,.NOT. e.g., IF (N.EQ. 1.OR..NOT. R.LT. 0.0) GOTO 75 Multiple Assignment Statements Multiple Assignment Statements e.g., A = I = V = W =.2 * R + X  A truncated value will be assigned to A and I  A truncated value will be assigned to A and I

23 Spring 2005 23 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Program Flow Control GotoGOTO (10, 20, 30) KCOUNT GotoGOTO (10, 20, 30) KCOUNT Logical IFIF (K.LT. 1) K = K + 1 Logical IFIF (K.LT. 1) K = K + 1 Arithmetic IFIF (A / T – S) 10, 20, 30 Arithmetic IFIF (A / T – S) 10, 20, 30 Block IFIF (M.GT. 15) THEN M = M / 3 Block IFIF (M.GT. 15) THEN M = M / 3 ELSE M = M * 3 ENDIF Do loopsDO 10 I = 1, N, 2 Do loopsDO 10 I = 1, N, 2 SUM = SUM + I SUM = SUM + I 10 CONTINUE 10 CONTINUE


Download ppt "ICE1341 Programming Languages Spring 2005 Lecture #14 Lecture #14 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."

Similar presentations


Ads by Google