Class Note for CS 403 at UA-Programming Languages (2)
Class Note for CS 403 at UA-Programming Languages (2)
Popular in Course
Popular in Department
This 7 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Alabama - Tuscaloosa taught by a professor in Fall. Since its upload, it has received 18 views.
Reviews for Class Note for CS 403 at UA-Programming Languages (2)
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
97 CS 403 Programming Languages Class 11 September 26 2000 Today s Agenda Finish Chapter 7 QAssignment Read chapter 8 for Tuesday QReminder CS Dept website has student contact information student websites Please update your contact information email address personal webpage URL cs 403 ciass 1n side at 2 Homework Assignment OChapter 5 page 255 256 13 Row major amp column major 1 2 12 free 14 QChapter 6 page 284ff 5 include Scott Meyer s arguments on this 10 14 19 QTurn in on paper or by email 930AM on Tuesday October 3rd cs 4m ciass m side at 3 Of course 403 is important The cartoon that was on this page was taken from ht wwwa com 39139 rooms toonJItml cs 403 ciass 1n side at 4 Chapter 7 Statementlevel Control Structures cs 4m ciass m side at 5 Levels of Control Flow 1 Within expressions 2 Among program units 3 Among program statements cs 403 ciass 1n side at a Levels of Control Flow Evolution FORTRAN lcontrol statements were based directly on IBM 704 hardware Much research and argument in the1960s about the issue One important result It was proven that all owcharts can be coded with only twoway selection and pretest logical loops cs 4m class m Sllde x 7 Control Structures Def A control structure is a control statement and the statements whose execution it controls Compound statements introduced by ALGOL 60 in the form of begin end A block is a compound statement that can define a new scope with local variables cs 403 class 1 Blue at a Selection Statements Design Issues 1 What is the form and type ofthe control expression 2 What is the selectable segment form single statement statement sequence com pound statement 3 How should the meaning of nested selectors be specified cs 4m class m Sllde x 9 Selection Statements SingleWay Examples FORTRAN IF IF booleanexpr statement Problem can select only a single statement to select more agoto must be used as in the following example FORTRAN example It Nor condition com 20 20 CONTINUE cs 403 class 1 Sllde x m Twoway Selector Examples ALGOL 60 if if booleanexpr then statement the then clause else statement the else clause The statements could be single or compound cs4m class m alder 11 Nested Selectors egPascal 1f then f then e1s 39f Which then gets the else Pascal39s rule else goes with the nearest then ALGOL 6039s solution disallow direct nesting 1f then f then cs 403 class 1 Sllde x 12 Multiple Selection Constructs 0 Design Issues 1 What is the form and type of the control expression 2 What segments are selectable single compound sequential 3 Is the entire construct encapsulated 13 Is execution ow through the structure restricted to include just a single selectable segment 5 What is done about unrepresented expression values 5 4m Class 1D Ell 4t 13 Modern Multiple Selectors O 3 Ada39s case is similar to Pascal39s case except 1 Constant lists can include Subranges eg 10 t t15 Boolean 0R operators eg1tt5 l 7 l15tt20 2 Lists of constants must be exhaustive Often accomplished with others clause This makes it more reliable 5 403 Class 1n Sllde x 14 Multiple Selectors 0 can appear as direct extensions to twoway selectors using elseif clauses ALGOL 68 FORTRAN 77 Modula 2 Ada Ada 1f then elslf then elslf then else end f 9 Far more readable than deeply nested it39s 9 Allows a boolean gate on every selectable group 5 4m Class 1D Ell 4t 15 Iterative Statements O The repeated execution of a statement or compound statement is accomplished either by iteration or recursion here we look at iteration because recursion is unitlevel control 9 General design Issues for iteration control statements 1 How 39 iteration controlled 2 Where is the control mechanism in the loop 5 403 Class 1n Sllde x 15 CounterControlled Loops 0 Design Issues 1 What is the type and scope of the loop var What is the value of the loop var at loop termination 3 Should it be legal for the loop var 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 5 4m Class in same at 17 CounterControlled Loops O 1 FORTRAN 77 and 90 Syntax Do label var start finish stepsize Stepsize can be any value but zero Parameters can be expressions Design choices Loop var can be INTEGER REAL or DOUBLE Loop var always has its last value The loop var cannot be changed in the loop but the parameters can because they are evaluated only once it does not affect loop control 4 Loop parameters are evaluated only once 5 403 less In Sllde 4 1E PPR 0 CounterControlled Loops O FORTRAN 90 s Other no G Syntax name no variable nitial terminal stepsize END no name 9 Loop var must be an INTEGER cs4mciass 1D slider 19 CounterControlled Loops 2 ALGOL 60 9 Sntax for var ltistofstuffgt do statement where ltistofstu gt can have list of expressions expression step expression untll expression expression whlle boolean expression for index 2 1 seep 2 uneii so 60 70 30 index 1 uneii 100 do index 1 3 5 7 49 60 70 8081 82 5 403 class In Slide x 2m CounterControlled Loops ALGOL 60 Design choices 9 Control expression can be in or real its scope is whatever it is declared to be 9 Control var has its last assigned value after loop termination 9 The loop var cannot be changed in the loop but the parameters can and when they are it affects loop control 9 Parameters are evaluated with every iteration making it very complex and dif cult to read cs4mciass 1D aidexzi CounterControlled Loops 3 Pascal O Sntax for variable statement 9 Design Choices 1 Loop var must be an ordinal type of usual scope 2 After normal termination loop var is unde ned 3 The loop var cannot be changed in the loop the loop parameters can be changed but they are evaluated just once so it does not affect loop control 4 Loop variable evaluatedjust once tial co downed nal do 5 403 class In Slide x 22 CounterControlled Loops 4 Ada OSyntax for var in reverse discreterange loop end loop cs4mciass 1D aidexza CounterControlled Loops O Ada Design choices 1 Type of the loop var is that of the discrete range its scope is the loop body it is implicitly declared 2 The loop var does not exist outside the loop 3 The loop var cannot be changed in the loop but the discrete range can it does not affect loop control 4 The discrete range is evaluated just once 5 403 class In Slide x 24 CounterControlled Loops 50 9 Syntax 02expr1 expr2 expr3 statement 0 The expressions can be whole statements or even statement sequences with the statements separated by commas 0 The value of a multiplestatement expression is the value of the last statement in the expression eg for 1 0 j 101 1 6 If the second expression is absent it is an in nite loop 5 4m Class 1D Ell 4t 25 CounterControlled Loops C Design Choices There is no explicit loop var Irrelevant 0 Everything can be changed in the loop 0 Pretest The rst expression is evaluated once but the other two are evaluated with each iteration This loop statement is the most exible ca 403 class 1 Sllde x as CounterControlled Loops The control expression can also be Boolean The initial expression can include variable de nitions scope is from the de nition to the end of the function in which I s de ned 6 O Differs from C in two ways 1 2 c54m class m 3mm CounterControlled Loops Control expression must be Boolean Scope of variables de ned in the initial expression is only the loop body 7 O Differs from c in two ways 1 2 ca 403 class 1 Sllde 4 2E Exercise Page 325 10 ORewrite in Ada amp CC amp Java k j 13 27 loop if k gt 10 then goto out k k 1 i 3 k 1 goto loop out OCompare at your table c54m class m sameng LogicallyControlled Loops Design Issues 1 Pretest or postest 2 Should this be a special case of the counting loop statement or a separate statement ca 403 class 1 Sllde at an LogicallyControlled Loops 9 Language Examples 1 Pascal has separate pretest and posttest logical loop statements whlledo and epeacuncll 2 C and C also have both but the control expression for the posttest version is treated just like in the pretest case while do and do while 3 Java is like C except the control expression must be Boolean and the body can only be entered at the beginningJava has no goto 4 Ada has a pretest version but no posttest 5 FORTRAN 77 and 90 have neither cs4m class m amen UserLocated Loop Control Mechanisms 0 Design issues 1 Should the conditional be part of the exit 2 Should the mechanism be allowed in an already controlled loop 3 Should control be transferable out of more than one loop cs 403 class 1n glue 4 32 UserLocated Loop Control Mechanisms 1 Ada conditional or unconditional for any loop any number of levels for loop Loopl whlle loop exl when l l l LOOPZ end loop for loop exl LDOPl when end loop LDOPZ end loop LOOPl cs4m class m Enema UserLocated Loop Control Mechanisms 2 C C and Java break Unconditional for any loop or switch one level only Java s can have a label OThere is also has a continue statement for loops it skips the remainder of this iteration but does not exit the loop cs 403 class 1n glue 4 34 UserLocated Loop Control Mechanisms 3 FORTRAN 90 EXIT Unconditional for any loop any number of levels FORTRAN 90 also has CYCLE which has the same semantics as C39s continue cs4m class m stems Iteration Based on Data Structures 0 Concept use order and number of elements of some data structure to control iteration 6 Control mechanism is a call to a function that returns the next element in some chosen order if there is one else exit loop 6 C39s for can be used to build a userde ned iterator eg for phdr p pnexp H 9 Perl has a builtin iterator for arrays and hashes eg foreach sname names prim sname cs 403 class 1n glue 4 an Unconditional Branching 6 Problem readability 9 Some languages do not have themeg Modula2 and Java 9 Label forms 1 Unsigned int constants Pascal with colon FORTRAN no colon 2 Identi ers with colons ALGOL 60 C 3 Identi ers in ltlt Ada 4 Variables as labels PLl Can be assigned values and passed as parameters Highly flexible but make programs impossible to read and dif cult to implement 54Class m smelly Restrictions on Pascal39s gotos 0 A statement group is either a compound statement or the body of a repeatuntil O The target of a goto cannot be a statement in a statement group that is not active Means the target can never be in a statement group that is at the same level or is nested more deeply than the one with the goto An important remaining problem the target can be in any enclosing subprogram scope as long as the statement is not in a statement group This means that a goto can terminate any number of subprograms 5 403 Class 1 Slide x 33
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'