Note 20 for CS 403 at UA-Programming Languages
Note 20 for CS 403 at UA-Programming Languages
Popular in Course
Popular in Department
This 4 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 13 views.
Reviews for Note 20 for CS 403 at UA-Programming Languages
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
Lecture 20 Tuesday March 18 Today Iteration statements Exception handling Reading Assignments For Next Class pp 45 49122 23 281 84 343 A5 376 22 Assignments Assignment 5 due one week from today Sprung 2on2 cs 403 class Naes P 21 A Second Case Anotnei39 tt case att 3 t att 5 e Precedence rules a a a 5 7 Nu Waytu apply aSSDEiativitv e Ely previeus slide We generai strategies for leading tne a s Strategy i At tne beginning at tne same time Strategy 2 Senaiiyene artertne other Strategy i Assume a is 2 7 answer is iB Strategy 2 Tnere are We pessibie evaluatien erders e Terrns 1i am where a is the rst ai 2i 32H where a2 is the second ai 39 l3 11l 3 39 14 12l 5 39 15 13 14 e Pessibie evaluatien erders that mold result in different answers 12545 a 2 answer is 21i 12455 a 2 answer is lsi smznnz cs AUZClassNates P e A What About Consider a 4 c Assumea2c1 By precedence we get ltltamiac Result of a 2 2 4ri 7 Atter a is used it becomes 3 Consider a 4 c a originally 2 a set to 3 before it is used 34 1 11 induces a side effect makes a change on the term of the expression to which it is applied Sprung 2on2 cs 403 class Naes P e 2 Control Flow Categories for Discussion Sequence already done Selection already done Iteration Procedures Recursion Need to define a preliminary term before launching this discussion control structure smznnz cs AUZClassNates P e 5 Side Effects in Expressions Use these slide rather than previous ones on the subject Several possibilities as to what might happen with Example a 3 a e Precedence rules a3 a 7 Undefined evaluation oi39def extends to include evaluating individual variables Strategy 1 All instances of a could be loaded at the beginning Strategy 2 The second instance of a could be loaded afterthe Suppose a is 2 e thn Strategyi Answeris 8 e thn Strategy 2 Answeris 9 5mm 2mm cs 403 class Naes P Iterative Statements Two general categories Countercontrolled Booleancontrolled Fixed exit Usercontrolled exit 5 mm cs 403 class was P CounterControlled Loops Example fori 1 to 100 do sum sum ai Terminology Loop variable i in this case Loop parameters Starting and ending values for i Sprung 2on2 cs 403 Class Notes P e 7 Design Issues i What is the type ahd scope ofthe loop var Shuuld it be a discrete ype7 Shuuld it be restricted tn intEgEr7 Shuuld it be ieeai tn the luup7 2 What is the value ofthe loop yar at loop ermll lallol W Upper bedhd parameter7 Upper bedhd parameter l7 uhderihem uptemir seppe restrietedtp iepp then this question gues away which cuuld be gun 3 Should it be legal forthe loop yarorioop parameters to be chahged ih the loop body and it so does the chahge affect loop cohtron irypd alluvv the ipep yarte be changed then the hdrhberer iteratiehs gets ambiguuus 4 Should the loop pararheters be evaluated ohiy Ol ice orohce for every iteration it the pup changes the parameters theh irthey are evaluated on every iteratiun the pup itseir cuuld change the hdrhberer iteratiehs This cuuld make the code harder tn yeriiy Sprung 2on2 cs 403 Class Notes P e 2 CounterControlled Loops FORTRAN 77 Syntax Do label var start nish stepsize e stepsize can be ahyyaide butzero 7 Parameters can be expressions Design choices i Loop yar can be INTEGER REAL or DOUBLE 2 Loop yar always has itsiast value 3 The loop yarcahhot be changed in the loop but the parameters Cal l because they are evaluated ohiy Ol ice it does hot affect loop control 4 Loop parameters are evaluated only once 5mm 2mm cs 403 Class Notes P CounterControlled Loops Pascal Syntax for variable initial Lo downto final do statement Design Choices 1 Loop var must be an ordinal type of usual scope 2 A er 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 smznnz cs AazcbssNees P e m CounterControlled Loops Ada for varin reverse discreterange loop end loop 0 Design choices i Type ofthe ioop yaris that ofthe discrete range its scope is the loop body it is implicitly declared 2 The loop yardoes hot eXlSI outside the loop 3 The loop var cannot be changed in the loop but the discrete rahge Cal l it does hot affectloop control 4 The discrete range is evaluated idst ohce smznnz cs AazcbssNees P e n CounterControlled Loops C syntax for expr1 expr2 expr3 statement The expressions can be whole statements or even statement sequences with the statements separated by commas eg for 1 e o 3 e 10 3 1 1 lfthe second expression is absent it is an in nite loop There is no notion of loop variablequot and expressions 2 and 3 might have nothing to do with incrementing and terminating based on a counter for cin gtgt x x 1y x is legal but doesn t fit the basic paradigm of counter ontrolled loops 2on2 cs 403 Class Notes P I CounterControlled Loops C differs 39om C in two ways The control expression can also be Boolean The initial expression can include variable defll lltlol lS scope l5 from the defll lltlol l to the el ld ofthe loop body Java differs from C in that the control expression must be Boolean Arbitrary ll ltegel expressions are not allowed as control expressions Ex in CH you mlght do the following forp head p p prgt hext USll lg p forthe control expression would not be allowed ll l Java Sprung 2mm cs 403 Class Notes P 213 UserLocated Loop Control Mechanisms 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 smznnz cs 402st P 216 LogicallyControlled Loops While loops and thelrvarlants Design issue Pretest condition tested at beginning or posttest condition tested at end Sprung 2mm cs 403 Class Notes P 214 UserLocated Loop Control Mechanisms 1 Ada conditional or unconditional for any loop any number of levels for loop LOOPl whlle loop exlt when l l LOOPZ end loop for loop exlt LOOPl when end loop LOOPZ end loop LOOPl smznnz cs 402st P 217 LogicallyControlled Loops Language Examples 1 Pascal has separate pretest and posttest logical loop statements whlledo and repeatuntll 2 C and C also have both but the control expression for the posttest version is treated just like in the pretest case whlle do and do whlle 3 Java is like 0 except the control expression must be Boolean and the body can only be entered at the beginning Java has no goto 4 Ada has a pretest version but no posttest FORTRAN 77 and 90 have neither 0quot 5mm 2mm cs 403 Class Notes P l 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 There is also has a continue statement for loops it skips the remainder ofthls iteration but does not exit the loop 5 2mm cs 403 Class Notes P l UserLocated Loop Control Mechanisms 3 FORTRAN 90 EXIT Unconditionai ror any ioop any numberofieyeis FORTRAN 90 also has CYCLE which has the Same Semantics as C s Continue Sprung zuuz cs 403 cuss Nutzs P 219 Exception Handling Simplest Case rhtrharhot cout ltlt Enterpositive integer ltlt ehdi Wi double x cm gtgt xi rm lt0throwx eise serum catchdoubie x cerr ltlt i ltlt x ltlt endi aborl involves throwrhg a userexception Sirghtiy better than rust calling a Motion 7 if nu aburt return El the next statement after the try biuck nut El the statement after the thruvv Spring zuuz cs 403 cuss Nutzs P e 22 Unconditional Branching The Gate statement Problem readability Some languages do not have themeg Modula2 and Java Label forms 1 Uhsrghedrht constants Pascaiwrth colon FORTRAN ho colon 2 identifiers wrth coions ALGOL 60 c 3 identifiers ii i ltlt gtgt Ada Sprung zuuz cs 403 cuss Nutzs P 2 2n Exception Handling Library Raised Exception inciuue ltexeepnehgt int main int p Ht Wt whrie true EDUI ltlt Erteraiiecatreh request ltlt ehui rh gtgt h p new rhtihi i i catcmbauiaiiuc icerr ltlt nauiaiiu ltlt ehuii i Request ror 1 000000000 integers resuits iii a buiitein exception badjiioc This request is caught and the program terminates more graceruiiythah with a segmentation rauit etc Spring zuuz cs 403 cuss Nutzs P e 23 Exception Handling Exception User de ned Language de ned An exception can be thrown by Standard language routines User routines User Viitten exception handlers can catch either type of exception Allows Grace Jl exit from segments ofcode which shouldn t be executed in the face of an exception Programmer controlled handling of exceptions raised by standard library routines 5mm 2mm cs 403 cuss Nutzs P l
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'