Presentation2

Download Report

Transcript Presentation2

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

e.g. for(I=0; I<some_num; I++){...}
– Logically controlled loops

e.g. I=0; while(I<some_num) {…}.
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.
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)
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));
}
}
Procedures & Functions

Procedures
– Generally perform a complete operation
– They generally do not return a value
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

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.

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);
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.
Function Example
#include<iostream>
//funtion prototype
void DisplayAge(int nAge);
int main()
{
DisplayAge(42);
return 0;
}
void DisplayAge(int nAge)
{
cout<<“hello world! I’m “ << nAge <<“years old:’<<endl;
}
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
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

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.

Exception Handling Example
try{
//code trying to execute.
}catch(IOException e)
{
}
System.out.print(e.getMessage());
Exception Handlers



Recover from an unexpected condition and continue.
– E.g. request additional space to the O.S. after outof-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
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.
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.
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.
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.
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.
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.
Communication

A number of different links may exist
between each pair of communicating
processes, with each link corresponding
to one mailbox.
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.
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.
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/i
s255/f02/lectures/lecture14.ppt
http://www.infm.ulst.ac.uk/~anthony/c
om009/lectures/Lecture7_8.ppt