Class Note for CS 403 at UA-Programming Languages (5)
Class Note for CS 403 at UA-Programming Languages (5)
Popular in Course
Popular in Department
This 19 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 14 views.
Reviews for Class Note for CS 403 at UA-Programming Languages (5)
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 21 Thursday March 21 V2 Today Iteration statements Go to statements Exception handling Reading Assignments For Next Class Finish reading Chapter 6 Chapter 85 exception handling Assignments Assignment 5 due Thursday April 4th Exams Exam 2 Moved from March 21st to Tuesday April 9th Spring 2002 CS 403 Class Notes Page 1 Iterative Statements Two general categories Countercontrolled Booleancontrolled Fixed exit Usercontrolled exit Spring 2002 CS 403 Class Notes Page 2 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 Spring 2002 CS 403 Class Notes Page 3 Design Issues 1 What is the type and scope of the loop var Should it be a discrete type Should it be restricted to integer Should it be local to the loop 2 What is the value of the loop var at loop termination Upper bound parameter Upper bound parameter 1 Undefined Notejz lf scope restricted to loop then this question goes away which could be goo 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 If you allow the loop var to be changed then the number of iterations gets ambiguous 4 Should the loop parameters be evaluated only once or once for every iteration If the loop changes the parameters then if they are evaluated on every iteration the loop itself could change the number of iterations This could make the code harder to verify Spring 2002 CS 403 Class Notes Page 4 CounterControlled Loops FORTRAN 77 Syntax DO label var start finish stepsize Stepsize can be any value but zero Parameters can be expressions Design choices 1 Loop var can be INTEGER REAL or DOUBLE 2 Loop var always has its last value 3 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 Spring 2002 CS 403 Class Notes Page 5 CounterControlled Loops Pascal Syntax for variable initial to downto final do statement Design Choices 1 Loop var must be an ordinal type of usual scope 2 After normal termination loop var is undefined 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 evaluated just once Spring 2002 CS 403 Class Notes Page 6 CounterControlled Loops Ada for var in reverse discreterange loop end Ioop 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 Spring 2002 CS 403 Class Notes Page 7 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 99 for i O 10 i i If the second expression is absent it is an infinite loop There is no notion of loop variable and expressions 2 and 3 might have nothing to do with incrementing and terminating based on a counter for cin gtgt x x 1 y x is legal but doesn t fit the basic paradigm of countercontrolled loops Spring 2002 CS 403 Class Notes Page 8 CounterControlled Loops C differs from C in two ways The control expression can also be Boolean The initial expression can include variable definitions scope is from the definition to the end of the loop body Java differs from C in that the control expression must be Boolean Arbitrary integer expressions are not allowed as control expressions SpringZOOZCSE4Oi3CilaslslNloteEs S El Hgllt I 395 H lllg39 Page 9 LogicallyControlled Loops While loops and their variants Design issue Pre test condition tested at beginning or posttest condition tested at end Spring 2002 CS 403 Class Notes Page 10 LogicallyControlled Loops Language Examples 1 Pascal has separate pretest and posttest logical loop statements whiledo and repeatuntil 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 Spring 2002 CS 403 Class Notes Page 11 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 Spring 2002 CS 403 Class Notes Page 12 UserLocated Loop Control Mechanisms 1 Ada conditional or unconditional for any loop any number of levels for loop LOOPl while loop exit when LOOP2 end loop for loop exit LOOPl when end loop LOOP2 end loop LOOPl Spring 2002 CS 403 Class Notes Page 13 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 of this iteration but does not exit the loop Spring 2002 CS 403 Class Notes Page 14 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 Spring 2002 CS 403 Class Notes Page 15 Unconditional Branching The Goto statement Problem readability Some languages do not have themeg Modula2 and Java Label forms 1 Unsigned int constants Pascal with colon FORTRAN no colon 2 Identifiers with colons ALGOL 60 C 3 Identifiers in ltlt gtgt Ada Spring 2002 CS 403 Class Notes Page 16 Exception Handling Excep on User defined Language defined An exception can be thrown by Standard language routines User routines Userwritten exception handlers can catch either type of exception Allows Graceful exit from segments of code which shouldn t be executed in the face of an exception Programmer controlled handling of exceptions raised by standard library routines Spring 2002 CS 403 Class Notes Page 17 Exception Handling Simplest Case int main cout ltlt Enter positive integer ltlt endl try double x cin gtgt x if X lt 0 throwx else sqrtx catchdouble x cerr ltlt x ltlt x ltlt endl abort Involves throwing a user exception Slightly better than just calling a function If no abort return to the next statement after the try block not to the statement after the throw Spring 2002 CS 403 Class Notes Page 18 Exception Handling Library Raised Excep on include ltexceptiongt int main int p n try while true cout ltlt Enter allocation request ltlt endl cin gtgt n p new intn catchbadalloc cerr ltlt badalloc ltlt endl Request for 1000000000 integers results in a builtin exception badalloc This request is caught and the program terminates more gracefully than with a segmentation fault etc Spring 2002 CS 403 Class Notes Page 19
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'