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.

Slides:



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

Programming Languages and Paradigms
Statement-Level Control Structures
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
ICE1341 Programming Languages Spring 2005 Lecture #12 Lecture #12 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Chapter 8 Statement-Level Control Structures. 1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
ISBN Chapter 8 Statement-Level Control Structures.
Introduction to Fortran Fortran Evolution Drawbacks of FORTRAN 77 Fortran 90 New features Advantages of Additions.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Controlling Program Flows
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
CSE 452: Programming Languages Expressions and Control Flow.
1 Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence.
ISBN Lecture 08 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
Statement-Level Control Structures
Introduction to FORTRAN
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Structure of Programming Language
April 14, ICE 1341 – Programming Languages (Lecture #13) In-Young Ko Programming Languages (ICE 1341) Lecture #13 Programming Languages (ICE 1341)
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
FORTRAN FORmula TRANslator -Anand Trivedi. HISTORY  Designed and written from scratch in by an IBM team lead by John W. Backus as the first.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Control Structures Programs have 4 basic control structures:
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
Arithmetic Expressions
Chapter 8: Statement-Level Control Structures
Control Structures sequence of execution of high-level statements.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
C HAPTER 8 Statement-Level Control Structures. CCSB314 Programming Language C HAPTER 8 T OPICS Introduction Selection Statements Iterative Statements.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
W E E K F I V E Statement-Level Control Structures.
Structure of Programming Language Statements. Expression.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Chapter 8 Statement-Level Control Structures. 2 Chapter 8 Topics  Introduction  Selection Statements  Iterative Statements  Unconditional Branching.
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Def: A control structure is a control statement and
Structure of Programming Language
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Chapter 8: Control Structures
Statement-Level Control Structures
Statement-Level Control Structures
Control Structures In Text: Chapter 8.
Control Structures Programs utilize 4 basic control structures
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
Statement-Level Control Structures
Chapter 8: Statement Level Control Structures
Presentation transcript:

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)

Spring 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

Spring 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

Spring 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

Spring 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

Spring 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

Spring 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!

Spring 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 loop for Count in loop Sum := Sum + Count; end loop end loop Count is an integer loop variable Count is a float variable

Spring 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

Spring 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

Spring 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#

Spring 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

Spring 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

Spring 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

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Punched Cards Coded FORTRAN programs were converted onto punched cards for loading Photos:

Spring 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

Spring ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Later FORTRAN Versions FORTRAN IV ( ) – ANSI standard in 1966 FORTRAN IV ( ) – 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

Spring 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

Spring 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

Spring 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.

Spring 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

Spring 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

Spring 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