Friday, January 20, 2017

Lecture 6 - Pseudo Codes


Pseudo code is Structured English like way of representing the solution to a problem. It is considered a "first draft" because Pseudo-code eventually has to be translated in to a programming language. Although Pseudo-code is like English and has some precision to it, it does not have the very definite precision of a programming language. A computer can not execute Pseudo-code.

When using Pseudo-code it is easy to plan a program, concentrate on the logic and do not worry about the rules of a specific language. It is also easy to change Pseudo-code when a error is discovered in logic; Once it is coded in a programming language, most people find that it is more difficult to change logic.

Pseudo-code can be translated in to a variety of programming languages, such as pascal or COBOL. it is helpful to introduce pseudo-code in relation to flow charting,

Data Declaration

It is important to define all data used by a program within the pseudo code as the following is one of the fundamental concept in programming.

PROGRAM = DATA + ALGORITHM ( Logic )

There are 3 main things to consider when declaring data in programs.

  1.     Data Type - specify the type of value that can be stored
  2.     Data Usage - whether the data item is used as a variable or a constant
  3.     Data Scope - specify the limit of access within the program

Data Types

Simple Data
Data items which can store single values such as integer, real, character etc.


Name : String

Age : Integer

Marks : Real



Complex Data
Data item which allows you to store collection of values. These include arrays ( collection of same type ) and records ( collection of different types )


ClassMarks(10) : array of integer

SrudentData: RECORD of

Name :string

Age : integer

Marks : real

END;



Data Usage


Variables
Variables are data items, of any type, whose contents may change in value as the program executes. The content of the data item Counter is incremented by 1, in the following statement :

Counter=Counter+1

Message = "Hello world"


The previous content of the data item Message, presumably a variable of character type, is replaced by the string of characters Hello world.


Constants
Constants are either literal values or data items whose contents do not change as the program executes. For example, in the two statements above:

Counter=Counter+1
Message = "Hello world"


The values 1 and "Hello world" are literal constants of type integer and character respectively. Character or string constants are typically enclosed within quotation marks. Some programming languages provide for a means of declaring a named data item to be a constant, e.g. Pi to represent the value 3.142. In such a case, whenever the data item Pi is used in a ca the value 3.142 will be substituted when the program executes.

The pseudo-code used for a value to be optionally assigned to a data item when it is defined :

Example:

Pi : real value 3.142

This is not equivalent to creating Pi as a constant. The value option merely places an initial value in the variable and that value may be subsequently altered by the operation of a program instruction.


Data Scope

Global
Data which is accessible by the procedure in which it is defined and by all procedures directly subordinate to that procedure.

Local
Data which is accessible only by the procedure in which it is defined;-a local data item with the same name as a global data item will have precedence over that global item within the local procedure.


Data Declaration Within a Program
Data items are declared at the beginning of a procedure. the syntax for this declaration is:

Global Local data data-name.... : type [value initial-value]

Example:

Global data
Pi : real value 3.142



Local data
Counter, Total : integer
Message : string value "please enter your name"
EndofFile : boolean



Control Constructs
The key to elegant algorithm design lies in restricting the control structure to the three basic logic constructs defined by the structured programming paradigm. The three basic control structures illustrated by the pseudo-code are:

Sequence
A series of instructions executed one after the other

Example:

lnput Number Of ltems Purchased
lnput Price Per ltem
TotalCost= Number Of Items Purchased *  Price Perltem
Display Total Cost


Selection
A selection control structure is the presentation of a condition and the choice between two actions, the choice depending on whether the condition is true or false. this construct represents the decision-making abilities of the computer .

lF condition THEN
Command sequence 1
ELSE
Command sequence 2
ENDIF


Example:

IF average >50 THEN
DISPLAY "Pass"
Else
DISPLAY "Fail"
End IF


If the choice is between more than two alternative, then we need a multiple selector. There are two different types of multiple selectors.

Nested IFs




CASE statement

CASE of Variable

1 : Command sequence 1
2 : Command sequence 2

ELSE : Default command sequence

END CASE



Repetition
The repetition control structure can be defined as the presentation of a set of instructions to be performed rapidly as long as the condition is true the basic idea of repetitive code is that a block of statements is executed again and again, until a terminating condition occurs. This construct represent namely to repeat a group of actions. it is written in pseudo-code as:


While Loop

WHlLE condition P is True DO
 Statement block
END DO



Repeat / Until Loop

REPEAT
Statement block
UNTIL Condition P is False


For Loop

FOR X='1 to10 DO
Statement block
END DO

Procedure / Function
Procedure. A selection of program that carries out some well-defined operation on data specify by parameter. it can be called from any ware in the program and different parameters can be provided for each call.


Procedure Swap ( Num l, Num 2 : integer )
Local data
Temp: integer
BEGIN
Temp:= Numl
Num'l := Num2
Num2 := Temp
END



Function: The program unit that, given values for input parameters, computes the value. The different between a Procedure and the function is that the function will return a single value after called, where as a procedure do a particular task but not necessarily return a value, since the function will return a value, than the data type of the functions should be declared.


FUNCTION average( Num l, Num 2 : integer ) : real
Local data
Total : integer
BEGIN
Total := Numl + Num2
average .= Total I 2.0
END



Important points regarding Functions / Procedures

  •     To return a value from a function it should be assigned to the function name.
  •     Each module can define it's own data.
  •     The data item passed into the function at the time of call are called parameters.
  •     Parameters may be input or output based on their role.
  •     Input parameters only carry value into the module.(e.g., Num1, Num2 in average
  •     function )
  •     Output parameters take a result back to the caller. ( e.g., num1, Num2 in swap
  •     procedure )

1 comment:

Important Notice!

Dear students and friends. When you commenting please do not mention your email address. Because your email address will be publicly available and visible to all. Soon, it will start sending tons of spams because email crawlers can extract your email from feed text.

To contact me directly regarding any inquiry you may send an email to info@bcslectures.website and I will reply accordingly.