1 / 27

Program Control

Program Control. Carlos Campos Vache Agazaryan. Program Control. Iteration and Recursion Functions and Procedures Exception Handlers Communication and Synchronization. Iteration. Iteration usually takes the form of loops Implementation methods Enumeration-controlled loops

velvet
Download Presentation

Program Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program Control Carlos Campos Vache Agazaryan

  2. Program Control • Iteration and Recursion • Functions and Procedures • Exception Handlers • Communication and Synchronization

  3. Iteration • Iteration usually takes the form of loops • Implementation methods • Enumeration-controlled loops • e.g. for(I=0; I<some_num; I++){...} • Logically controlled loops • e.g. I=0; while(I<some_num) {…}.

  4. Iteration Problems • Problems • Loop boundaries must be integers • Expressions are not allowed • index variable can change within the body of the loop • The value of I after termination of the loop is implementation dependent.

  5. Recursion • An algorithm technique in which a function, invokes itself. • Recursive methods need: • A base case/terminating condition (or it will loop infinitely) • A body (containing the recursive call)

  6. Recursion vs. Iteration Ex. Public class Sums{ public int addUpIteratively (int n) { int sum=0; for (int I=0; I<=n; I++){ sum +=I; } } public int addUpRecursively(int n) { int (n==0) return n; return (n + addUpRecursively(n-1)); } public static void main (String args[]) { Sums sums = new Sums(); int n = 12; System.out.println(“Iterative Sum for n= “ + n + “is “ + sums.addUpIteratively(n)); System.out.println(“Recursive Sum for n= “ + n + is “ + sums.addUpRecursively(n)); } }

  7. Procedures & Functions • Procedures • Generally perform a complete operation • They generally do not return a value

  8. Function • A group of computer instructions that performs a well-defined task inside a computer program. • Are one of the primary building blocks of an application. • Functions provide a way to break up a large program into more managable parts

  9. Function Cont’d • At the same time, functions make it possible to perform the same task at various point within the program without repeating the code. • Every code has at-least one function, and it is called main. It is called by the Operating System when you application starts.

  10. Declaring Functions • Before you can use a function, you must declare it by supplying a function prototype to the complier. To declare a function, you specify the function’s name, its return value, and a list of any parameters that are passed to it, as shown. Int CalculateAge(int nYearBorn);

  11. Defining Functions: • Function definition always consist of the following • The function’s return value • The function’s name • The function’s parameter list • The actual function body, enclosed in curly braces.

  12. Function Example#include<iostream>//funtion prototypevoid DisplayAge(int nAge);int main(){ DisplayAge(42); return 0;}void DisplayAge(int nAge){ cout<<“hello world! I’m “ << nAge <<“years old:’<<endl;}

  13. Functions and Procedures • Why use Procedures and Functions? • Avoid repeating code • Make code more readable • Easier to debug program • Can use in other programs • Pass Parameters

  14. Exception Handling • An exeption is an event that occurs during the execution of a program that disrupts the normal flow of instructions. • Example: reading a value after EOF reached

  15. Why Use Exception Handling • Most important is that is separates error-handling code from your normal code; this clarifies your code • Secondly, it stimulates consequences because errors are being handled in one place and in one manner.

  16. Exception Handling Exampletry{ //code trying to execute. }catch(IOException e) { System.out.print(e.getMessage()); }

  17. Exception Handlers • Recover from an unexpected condition and continue. • E.g. request additional space to the O.S. after out-of-memory exception • Graceful termination after an unrecoverable exception • Printing some helpful error message and exit • e.g. error at line-number - where the exception was raised in java • Local handling and propagation of exception • some exceptions have to be resolved at multiple level in the dynamic chain • e.g. exceptions can be re-raised in Java using the throw statement

  18. Exception Handling Overhead • The extra overhead associated with the java or c++ exception handling may increase the size of the executable files and slow program execution time. • Because of the nature of exception handling and the extra overhead involved, exceptions should be used only to single the occurrence of unusual or unanticipated program events.

  19. Communication • Communication between processes (direct or indirect communication) takes place by calls to send and receive primitives. • Direct process communication: • Each process that wants to communicate must explicitly name the recipient or sender • e.g. send (P, msg) - Send message to process P. • e.g. receive (Q, msg) - receive message from process Q.

  20. Communication • Steps of Direct-Communication: • A link is established automatically between processes that want to communicate - they need to know however, each other’s id. • A link is associated with exactly two processes. • Exactly one link exists between two processes.

  21. Communication • Disadvantages of direct-communication • Both the sender and receiver must name each other explicitly • changing the name of a process will require examining all other process definitions • All old references must be found and changed to the new name.

  22. Communication • Indirect Communication • Messages are sent to and received from mailboxes, or ports. • Each mailbox has a unique id. • A process can communicate via a number of different mailboxes. • Two processes can communicate only if they share a mailbox.

  23. Communicaton • Send (A, msg) - Send message to mailbox A. • Receive (A, msg) - Receive messge from mailbox A. • Steps of indirect communication • A link is established between two process only if they share a common mailbox • A link may be associated with many processes.

  24. Communication • A number of different links may exist between each pair of communicating processes, with each link corresponding to one mailbox.

  25. Synchronization • Synchronization - Threads in java • A thread can be either created by extending the thread class or implementing the runnable interface. • Thread synchronization is used to prevent other threads from accessing the same objects at the same time.

  26. Synchronization • A Thread must obtain a lock on an object before it can modify. • If another thread t1 already has a lock on that object, than t2 must wait (block) until it has been released by t1. • If a thread is executing one of it’s synchronized methods, then no other thread can execute any other synchronized method at the same time.

  27. References • Sam’s Teach Yourself • Visual C++ 6 (Mickey Williams) • Operating Systems Concepts • 6th edition (Galvin, Gagne). • Java In A Nutshell (O’reilly) • 3rd edition • Sam’s Teach Yourself Java • 3rd edition (Candenhead & Lemay) • http://www.sims.berkeley.edu/courses/is255/f02/lectures/lecture14.ppt • http://www.infm.ulst.ac.uk/~anthony/com009/lectures/Lecture7_8.ppt

More Related