www.5starnotes.com
m
GE 6151 – FUNDAMENTALS OF COMPUTER PROGRAMMING
co
PART –B (UNIT - 1)
te
Abacus Astrolabe Pascaline Stepped Reckoner Difference Engine Analytical Engine Punch Cards ENIAC (Electrical Numerical Integrator and Calculator) Von Neumann Machine
no
s.
1.B.1. Explain the evolution of computers. (8) (JAN 2009)
1.B.2. With suitable diagram explain about computer organization. (8) ( JAN 2009)
w. 5s t
ar
A computer can process data, pictures, sound and graphics. They can solve highly complicated problems quickly and accurately. Input Unit:Computers need to receive data and instruction in order to solve any problem. Therefore we need to input the data and instructions into the computers. The input unit consists of one or more input devices.Keyboard is the one of the most commonly used input device. Other commonly used input devices are the mouse, floppy disk drive, magnetic tape, etc. All the input devices perform the following functions. Accept the data and instructions from the outside world. Convert it to a form that the computer can understand. Supply the converted data to the computer system for further processing. Block Diagram of Computer
ww
www.5starnotes.com
www.5starnotes.com
www.5starnotes.com
ww
w. 5s t
ar
no
te
s.
co
m
Types of Storage Devices: 1. Primary Storage: 1. Stores and provides very fast. 2. This memory is generally used to hold the program being currently executed in the computer, the data being received from the input unit, the intermediate and final results of the program. 3. The primary memory is temporary in nature. The data is lost, when the computer is switched off. 4. In order to store the data permanently, the data has to be transferred to the secondary memory. The cost of the primary storage is more compared to the secondary storage. 2. Secondary Storage: 1. It stores several programs, documents, data bases etc. 2. The programs that run on the computer are first transferred to the primary memory before it is actually run. 3. Whenever the results are saved, again they get stored in the secondary memory. 4.The secondary memory is slower and cheaper than the primary memory. Some of the commonly used secondary memory devices are Hard disk, CD, etc., Memory Size: All digital computers use the binary system, i.e. 0’s and 1’s. Each character or a number is represented by an 8 bit code. The set of 8 bits is called a byte. A Character occupies 1 byte space. A numeric occupies 2 byte space. Byte is the space occupied in the memory. The size of the primary storage is specified in KB (Kilobytes) or MB (Megabyte). One KB is equal to 1024 bytes and one MB is equal to 1000KB. The size of the primary storage in a typical PC usually starts at 16MB. PCs having 32 MB, 48MB, 128 MB, 256MB memory are quite common. Output Unit: The output unit of a computer provides the information and results of a computation to outside world. Printers, Visual Display Unit (VDU) are the commonly used output devices. Other commonly used output devices are floppy disk drive, hard disk drive, and magnetic tape drive. Arithmetic Logical Unit: All calculations are performed in the Arithmetic Logic Unit (ALU) of the computer. It also does comparison and takes decision. The ALU can perform basic operations such as addition, subtraction, multiplication, division, etc and does logic operations viz, >, <, =, ‘etc. Whenever calculations are required, the control unit transfers the data from storage unit to ALU once the computations are done, the results are transferred to the storage unit by the control unit and then it is send to the output unit for displaying results. Control Unit: It controls all other units in the computer. The control unit instructs the input unit, where to store the data after receiving it from the user. It controls the flow of data and instructions from the storage unit to ALU. It also controls the flow of results from the ALU to the storage unit. The control unit is generally referred as the central nervous system of the computer that control and synchronizes its working. Central Processing Unit: The control unit and ALU of the computer are together known as the Central Processing Unit (CPU). The CPU is like brain performs the following functions: • It performs all calculations. • It takes all decisions. • It controls all units of the computer. www.5starnotes.com
www.5starnotes.com
www.5starnotes.com
co
1.B.3. With suitable example, explain about Number Systems. (16) (JAN 2009)
m
A PC may have CPU-IC such as Intel 8088, 80286, 80386, 80486, Celeron, Pentium, Pentium Pro, Pentium II, Pentium III, Pentium IV, Dual Core, and AMD etc.
no
te
s.
A number system is a set of rules and symbols used to represent a number. There are several different number systems. Some examples of number systems are as follows: Binary (base 2) Octal (base 8) Decimal (base 10) Hexadecimal (base 16) Decimal and Hexadecimal numbers can each be represented using binary values. This enables decimal, hexadecimal, and other number systems to be represented on a computer which is based around binary (0 or 1 / off or on). The base (or radix) of a number system is the number of units that is equivalent to a single unit in the next higher counting space. In the decimal number system, the symbols 0-9 are used in combination to represent a number of any sizes. For example, the number 423 can be viewed as the following string of calculations: (4 x 100) + (2 x 10) + (3 x 1) = 400 + 20 + 3 = 423.
ar
1.B.4. Explain different types of computers. (8) (JAN/ FEB 2009)
ww
w. 5s t
Personal Computers: A personal computer (PC) is a self-contained computer capable of input, processing, output, and storage. A personal computer is designed to be a single-user computer and must have at least one input device, one output device, a processor, and memory. The three major groups of PCs are desktop computers, portable computers, and handheld computers. Desktop Computers: A desktop computer is a PC designed to allow the system unit, input devices, output devices, and other connected devices to fit on top of, beside, or under a user’s desk or table. This type of computer may be used in the home, a home office, a library, or a corporate setting. Portable Computers: A portable computer is a PC small enough to be moved around easily. As the name suggests, a laptop computer fits comfortably on the lap. As laptop computers have decreased in size, this type of computer is now more commonly referred to as a notebook computer. Manufacturers recently began introducing a new type of computer called the tablet PC, which has a liquid crystal display (LCD) screen on which the user can write using a special-purpose pen, or stylus. Tablet PCs rely on digital ink technology that allows the user to write on the screen. Another type of portable computer, called a wearable computer, is worn somewhere on the body, thereby providing a user with access to mobile computing capabilities and information via the Internet. Handheld Computers: An even smaller type of personal computer that can fit into the hand is known as a handheld computer (also called simply handheld, pocket PC, or Palmtop). In recent years, a type of handheld computer called a personal digital assistant (PDA) has become widely used for performing calculations, keeping track of schedules, making appointments, and writing memos. Some handheld computers are Internet-enabled, meaning they can access the Internet without wire connections. For example, a smart phone is a cell phone that connects to the Internet to www.5starnotes.com
www.5starnotes.com
www.5starnotes.com
w. 5s t
ar
no
te
s.
co
m
allow users to transmit and receive e-mail messages, send text messages and pictures, and browse through Web sites on the phone display screen. Workstations: A workstation is a high-performance single-user computer with advanced input, output, and storage components that can be networked with other workstations and larger computers. Workstations are typically used for complex applications that require considerable computing power and high-quality graphics resolution, such as computer-aided design (CAD), computerassisted manufacturing (CAM), desktop publishing, and software development. Midrange Servers: Linked computers and terminals are typically connected to a larger and more powerful computer called a network server, sometimes referred to as a host computer. Although the size and capacity of network servers vary considerably, most are midrange rather than large mainframe computers. (i) Midrange server – formerly known as a minicomputer, a midrange server is a powerful computer capable of accommodating hundreds of client computers or terminals (users) at the same time. (ii) Terminal – a device consisting of only a monitor and keyboard, with no processing capability of its own. Mainframe Computers: Larger, more powerful, and more expensive than midrange servers, a mainframe computer is capable of accommodating hundreds of network users performing different computing tasks. These computers are useful for dealing with large, ever-changing collections of data that can be accessed by many users simultaneously. Government agencies, banks, universities, and insurance companies use mainframes to handle millions of transactions each day. Supercomputers: A supercomputer is the fastest, most powerful, and most expensive of all computers. Many are capable of performing trillions of calculations in a single second. Primary applications include weather forecasting, comparing DNA sequences, creating artificially intelligent robots, and performing financial analyses. 1.B.5. Discuss about the evolution of computers. (8) (FEB 2009)
ww
Refer 1.B.1
www.5starnotes.com
1.B.6. With suitable diagram, explain the basic organization of computers. (16) (FEB 2009) Refer 1.B.2
www.5starnotes.com
www.5starnotes.com
m
1.B.7. What are the characteristic of a computer? Discuss briefly explain the various generations of computers. (16) (JAN 2010)
Speed Accuracy. Automation. Endurance. Versatility. Storage. Cost Reduction.
s.
co
Characteristics of Computers
ww
w. 5s t
ar
no
te
Generation of computers Each phase of computer development is known as a separate generation of computers. The computer can be classified into four generations according to their type of electronic circuits such as vacuum tube, transistor, IC etc. (a) The First Generation Computers (1949-55) Main Features: The computers of this generation used vacuum tubes. These computers used machine language for giving instructions. They used the concept of stored program. These computers were 5000 times faster than the MARK-I. The first generation computers were welcomed by Government and Universities. Limitations: These computers were very big in size. The ENIAC machine was 30 x 50 feet in size and 30 tons in weight. So, these machines required very large space for their workings. Their power consumption was very high. These computers had slow operating speed and small computing capacity. These computers had a very small memory. (b) The Second Generation Computers (1956-65) Main Features: The computers of this generation replaced vacuum tubes with transistors. Magnetic cores were invented for storage. Different magnetic storage devices were developed in this generation. Commercial applications were developed during this period. Eighty percent of these computers were used in business and industries. (c) Third Generation Computers (1966-75) Main Features: The third generation computers replaced transistors with’ Integrated Circuits’. These Integrated Circuits are also known as chips. The size of main memory was increased and reached about 4 megabytes. Magnetic disk technology had been improved and drive having capacity upto 100 MBPS came into existence. The CPU becomes more powerful with the capacity of carrying out 1 million instructions per second.
www.5starnotes.com
www.5starnotes.com
no
te
s.
co
m
This generation computers were relatively inexpensive and faster. The application area also increased in this generation. The computers were used in other areas like education, small businesses survey, analysis along with their previous usage (e) Fourth Generation Computers (1976-Present) Main Features: The fourth generation computers replaced small scale integrated circuits and medium scale integrated circuits with the microprocessors chip. Semiconductor memories replaced magnetic core memories. The hard-disks are available of the sizes upto 200 GB. The RAID technology (Redundant Array of Inexpensive Disks) gives storage upto thousands of GB. Computer cost came down rapidly in this generation. Application of computers is increased in various areas like visualization, parallel computing, multimedia etc. (e) The Fifth Generation Computers Mankind along with the advancement in science and technology is working hard to bring the Vth Generation of computer. These computers will have the capability of thinking on their own like an man with the help of Artificial Intelligence (AI). the 21st century will be better, faster, smaller and smarter computers.
Decimal to Binary:
ar
1.B.8. Convert the decimal number 59.8125 into binary and octal. (16) (JAN 2010)
Given number – 59.8125
ww
w. 5s t
The following table lists the steps showing the conversion of decimal part (59) to binary equivalent. Decimal number
Divisor
Quotient
Remainder
59
2
29
1
29
2
14
1
14
2
7
0
7
2
3
1
3
2
1
1
1
2
0
1
Now read the remainder in the above table in upward direction as 111011.
www.5starnotes.com
www.5starnotes.com Multiplier
Result
Decimal part
.8125
2
1.625
1
.625
2
1.25
1
.25
2
.5
.5
2
1.0
s.
co
Decimal number
0
te
1
Now read the remainder in the above table in downward direction as 0.1101
no
Therefore (59.8125)10 = (111011.1101)2 Decimal to octal:
Quotient
59
ar
The following table lists the steps showing the conversion of decimal part (59) to octal equivalent.
8
7
3
7
8
0
7
Divisor
Remainder
w. 5s t
Decimal number
Now read the remainder in the above table in upward direction which is 73
ww
The following table lists the steps showing the conversion of fractional part (.8125) to octal equivalent. Decimal number
Multiplier
Result
Decimal part
.8125
8
6.5
6
.5
8
4.0
4
Now read the remainder in the above table in downward direction which is 0.64 Therefore (59.8125)10 = (73.64)
www.5starnotes.com
m
The following table lists the steps showing the conversion of fractional part (.8125) to binary equivalent.
Input Unit Central Processing Unit. Secondary Storage Unit. Output Unit.
1.B.10. Explain the characteristics of computers. (Jan 2013)
te
Refer 1.B.7
s.
co
1. B.9. Explain the different components of a computer system with block diagram. (16) (JAN 2010)
m
www.5starnotes.com
1.B.11. Discuss the evolution of computers. (Jan 2013)
no
Refer 1.B.1
1.B.12. Explain the basic organization of computer with suitable block diagram(Jan 2013)
ar
Refer 1.B.2
1.B.13. Give the different generation of languages in the evolutions of computer software(June 2012)
w. 5s t
I Generation Machine Language, II Generation Assemble Language, Mnemonics, III Generation High Level Language, BASIC, PASCAL, COBOL, FORTRON, IV generation 4GL, V Generation Artificial Intelligence. 1.B.14. What are octal numbers? Write down the algorithm to convert a decimal number to an octal number and convert the decimal number 399 to its octal equivalent. The octal numeral system, or oct for short, is the base-8 number system, and uses the digits 0 to 7. Numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right).
ww
1.B.15. What is volatile memory? Explain the different types of volatile memory. Volatile memory: also known as volatile storage is computer memory that requires power to maintain the stored information, unlike non-volatile memory which does not require a maintained power supply. It has been less popularly known as temporary memory.
www.5starnotes.com
www.5starnotes.com Types of volatile memory:
m
RAM (Random Access Memory)
s.
co
Random access memory, generally called RAM is the system's main memory, i.e. it is a space that allows you to temporarily store data when a program is running. Unlike data storage on an auxiliary memory such as a hard drive, RAM is volatile, meaning that it only stores data as long as it supplied with electricity. Thus, each time the computer is turned off, all the data in the memory are irremediably erased.
1. DRAM (Dynamic RAM) 2. SRAM (Static RAM)
no
te
Dynamic RAM (DRAM) and static RAM (SRAM) chips are the "working storage" in every computer. DRAM is the main memory in a computer and SRAM is used for high-speed caches and buffers. Both types are "byte addressable," which means that data can be read and written one byte at a time. Their major drawback is that DRAM and SRAM require power to hold their content. 1. B.16. How computers can be classified? Explain
ar
Refer 1.B.4
1.B.17. Illustrate the process of addition and subtraction in Is and 2s complement system with suitable examples. (8) (JAN 2012)
ww
w. 5s t
1's Complement Arithmetic The Formula N n N where: n is the number of bits per word N is a positive integer N is -N in 1's complement notation For example with an 8-bit word and N = 6, we have: N 2 In Binary An alternate way to find the 1's complement is to simply take the bit by bit complement of the binary number. For example: N N , given the 1's complement we can find the magnitude of the number by taking it's 1's complement. The largest number that can be represented in 8-bit 1's complement is 011111112 = 127 = $7F. The smallest is 100000002 = -127. Note that the values 000000002 and 111111112 both represent zero.
www.5starnotes.com
www.5starnotes.com
co
m
Addition End-around Carry. When the addition of two values results in a carry, the carry bit is added to the sum in the rightmost position. There is no overflow as long as the magnitude of the result is not greater than 2n-1.
w. 5s t
ar
no
te
s.
2's Complement Arithmetic The Formula NN* n where: n is the number of bits per word N is a positive integer N* is -N in 2's complement notation For example with an 8-bit word and N = 6, we have: N 2 In Binary An alternate way to find the 2's complement is to start at the right and complement each bit to the left of the first "1". For example: N N Conversely, given the 2's complement we can find the magnitude of the number by taking it's 2's complement. The largest number that can be represented in 8-bit 2s complement is 011111112 = 127. The smallest is 100000002 = -128. Addition When the addition of two values results in a carry, the carry bit is ignored. There is no overflow as long as the is not greater than 2n-1 nor less than -2n. 1.B.18. Explain the evolution of computers. (8) (JAN 2012)
Refer 1.B.1
ww
1.B.19. Explain the basic computer organization with suitable diagram. (8) (JAN 2012)
Refer 1.B.2
www.5starnotes.com
www.5starnotes.com 1.B.20. With examples illustrate the conversion of Decimal to Binary, Octal and Hexadecimal numbers (8) (JAN 2012)
m
www.5starnotes.com
w. 5s t
ar
no
te
s.
co
Decimal to Binary, Octal and Hexadecimal numbers
1.B.21. Write the pseudo code to multiply two matrices.(8) (JAN 2009)
pseudo code
for i<-1 to n do c[i,i] <- 0
for L<-1 to n-L do j<-i+L
ww
c[i,j] <- infinity
for k<-i to j-1 do temp <- c[i,k] + c[k+1,j] + d(i-1)*d(k)*d(j)
if temp < c[i,j] then c[i,j] <- temp pai[i,j] <- k return c[i,n]
www.5starnotes.com
www.5starnotes.com
co
m
1.B.22. Write algorithm to print the Fibonacci series: (0,1,1,2,3,5,8,13,…). (8) (JAN/ FEB 2009) Fibonacci Series is the sequence of numbers generated by adding the two numbers preceding it. Initially one starts with 0 and 1 and continues generating numbers main() { int c=1,f=0,s=1;
s.
int i; printf("%d%20d\n",f,f); printf("%d%20d\n",s,s);
te
for (i=2; i%26lt;5||f%26lt;s; i++) {
no
c=f+s; f=s;
printf("%d%20d\n",i,c);
w. 5s t
}
ar
s=c;
}
1.B.23. Write the uses of flowchart and algorithm with suitable examples.(8) (JAN 2009/MAY 2009)
Flowchart:
ww
a. Communication: Flowcharts are better way of communicating the logic of a system to all concerned. b. Effective analysis: With the help of flowchart, problem can be analyzed in more effective way. c. Proper documentation: Program flowcharts serve as a good program documentation,which is needed for various purposes. d. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. e. Proper Debugging: The flowchart helps in debugging process. f. Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part.
www.5starnotes.com
te
s.
co
m
www.5starnotes.com
Algorithm:
can be implemented within a function.
No ambiguity
ar
Properties of an algorithm
no
An algorithm is a description of a procedure which terminates with a result. Simple algorithms
There should not be any uncertainty about which instruction to execute next.
w. 5s t
The algorithm should conclude after a finite number of steps. The algorithm should be general. Example:
Biggest among two numbers Addition of N numbers
Finding Factorial of a number
ww
Finding Fibonacci series.
www.5starnotes.com
www.5starnotes.com
w. 5s t
ar
no
te
s.
co
m
1.B.24. Draw and explain the various symbols of flowchart. (JAN 2010)
1.B.25. Explain flowchart in detail?. (FEB 2010)
Definitions: A flowchart is a schematic representation of an algorithm or a stepwise process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. Flowcharts are used in designing or documenting a process or program. A flow chart, or flow diagram, is a graphical representation of a process or system that details the sequencing of steps required to
ww
create output. A flowchart is a picture of the separate steps of a process in sequential order.
The benefits of flowcharts are as follows: a. Communication: Flowcharts are better way of communicating the logic of a system to all concerned. b. Effective analysis: With the help of flowchart, problem can be analyzed in more effective way.
www.5starnotes.com
www.5starnotes.com c. Proper documentation: Program flowcharts serve as a good program documentation, which
m
is needed for various purposes.
co
d. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. e. Proper Debugging: The flowchart helps in debugging process.
s.
f. Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part. Advantages
Logic Flowcharts are easy to understand. They provide a graphical representation of actions to
te
be taken.
Logic Flowcharts are well suited for representing logic where there is intermingling among many
no
actions. Disadvantages
Logic Flowcharts may encourage the use of GoTo statements leading to software design that is
ar
unstructured with logic that is difficult to decipher. Without an automated tool, it is timeconsuming to maintain Logic Flowcharts. Logic Flowcharts may be used during detailed logic design to specify a module. However, the presence of decision boxes may encourage the use of
w. 5s t
GoTo statements, resulting in software that is not structured. For this reason, Logic Flowcharts may be better used during Structural Design.
LIMITATIONS OF USING FLOWCHARTS Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy.
Alterations and Modifications: If alterations are required the flowchart may require redrawing
ww
completely.
Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem. The essentials of what is done can easily be lost in the technical details of how it is done.
www.5starnotes.com
GUIDELINES FOR DRAWING A FLOWCHART
te
s.
co
m
www.5starnotes.com
no
a. In drawing a proper flowchart, all necessary requirements should be listed out in logical order. b. The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart.
ar
c. The usual direction of the flow of a procedure or system is from left to right or top to bottom. d. Only one flow line should come out from a process symbol. e. Only one flow line should enter a decision symbol, but two or three flow lines, one for each
ww
w. 5s t
possible answer, should leave the decision symbol.
f. Only one flow line is used in conjunction with terminal symbol.
www.5starnotes.com
s.
co
m
www.5starnotes.com
g. Write within standard symbols briefly. As necessary, you can use the annotation symbol to describe
te
data or computational steps more clearly.
no
h. If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication.
ar
i. Ensure that the flowchart has a logical start and finish.
j. It is useful to test the validity of the flowchart by passing through it with a simple test data.
w. 5s t
1.B.26. Write an algorithm to find the largest of three numbers. (JAN 2013) 1. Start
2. Get three numbers a, b, c 3. if(a>b) &&(a>c) then 4. Print a is greater
5. if(b>a)&& (b>c) then
ww
6. Print c is greater 7. Else
8. Print b is greater 9. Stop
www.5starnotes.com
www.5starnotes.com 1.B.27. Write an algorithm to compute the factorial of a number n. (8) (JAN 2012)
s.
co
m
1.Take a number as a input from the user. 2. Initialize a variable fact=1 3. fact=fact*number 4. number=number-1 5. repeat step 3 to 5 if number>0 6. print the value of fact variable 1.B.28. Write the pseudo code to add two matrices. (8) (JAN 2012) Matrix Addition Pseudo code
te
INIT add FOR each x from 0 to 15 FOR each y from 0 to 15
no
SET input1[x][y]=NULL SET input2[x][y]=NULL
INCREMENT y END FOR
w. 5s t
INCREMENT x
ar
SET tot[x][y]=NULL
END FOR
SET r=0, c=0
READ c and r
FOR each x from 0 to less than r
FOR each y from 0 to less than c READ input1[x][y] INCREMENT y
ww
END FOR
INCREMENT x END FOR FOR each x from 0 to less than r FOR each y from 0 to less than c READ input2[x][y]
www.5starnotes.com
www.5starnotes.com INCREMENT y
m
END FOR
co
INCREMENT x END FOR FOR each x from 0 to less than r
s.
FOR each y from 0 to less than c COMPUTE tot[x][y]=input1[x][y]+input2[x][y] PRINT tot[x][y]
te
INCREMENT y END FOR INCREMENT x
no
END FOR END
www.5starnotes.com
ar
1.B.29. Explain sequence logic, selection logic, and iteration logic design structure in pseudocode. (JAN 2011) Pseudocode is a tool for planning, defining, or documenting the contents of a program
w. 5s t
routine or module. As the name implies, pseudocode is similar to (and often based on) real code. Pseudocode is an excellent tool for planning or designing program logic and computational algorithms. Because it resembles real code, programmers find it easy to use and to understand. Pseudocode is not a good tool for describing control structures, particularly when several nested decisions are involved. Because pseudocode resembles a programming language, it is not well suited to planning or designing manual procedures. In part, because pseudocode so closely resembles real code, some designers tend to write (rather than plan or design) the code. Writing
ww
the code twice (once in pseudocode and once in real code) is a waste of time. When the designer worries about coding details, crucial design considerations can easily be overlooked. Also, programmers resent such over specification. Even if the pseudocode is well done, programmers sometimes resent it. Specifying algorithms in what is essentially a high-level language limits the programmer’s flexibility. Additionally, the programmer can fail to distinguish between the analyst’s coding technique and the analyst’s design, and the result may be criticism (even rejection) of a perfectly good design based on inappropriate criteria.
www.5starnotes.com
www.5starnotes.com Sequence
m
Perhaps the easiest way to define sequential logic is by coding an algebra-like expression, for example,
co
COUNT = 0 or STOCK = STOCK + QUANTITY
s.
Such details as the sequence of operations and the rules for using parentheses should be
consistent with the language to be used for writing the actual code. Data names should be
te
taken from the data dictionary.
Input and output instructions are explicitly defined in pseudocode; for example, READ data FROM source
no
and WRITE data TO destination
where data is a list of variables, a data structure, or a record, and source and destination
Blocks of logic
ar
refer to a file or a database.
w. 5s t
A block can contain any set of sequence, decision (selection), and/or repetition (iteration) logic. Once defined, the block’s instructions can be referenced by coding a PERFORM instruction:
PERFORM block
For example, the instructions COUNT = 0
ACCUMULATOR = 0
might be assigned the block name INITIALIZE. Subsequently, the instruction PERFORM
ww
INITIALIZE executes all the instructions in the block. To distinguish between formal subroutines and internal procedures, some analysts use: PERFORM block USING list for a subroutine, where list designates a list of variables passed between the calling routine and the subroutine.
www.5starnotes.com
www.5starnotes.com Decision or selection
m
The general form of a decision (selection) block is:
co
IF condition THEN PERFORM block-1
s.
ELSE PERFORM block-2 ENDIF
te
For example, IF HOURS > 40 THEN
no
PERFORM OVERTIME ELSE
ENDIF
ar
PERFORM REGULAR
By convention, the THEN block is executed if the condition is true and the ELSE block is executed if the condition is false. Note the ENDIF. A feature of most pseudocodes is that each
w. 5s t
block of logic is clearly delimited. A decision block always begins with IF and ends with ENDIF, so there is no ambiguity. Note also the use of indentation. It makes the block easy to read. It is possible to nest decision logic. For example, IF condition-1 THEN
IF condition-2 THEN
ww
PERFORM block-a ELSE
PERFORM block-b ENDIF ELSE PERFORM block-c ENDIF
www.5starnotes.com
www.5starnotes.com IF and ENDIF clearly delimit both decision blocks.
Repetition or iteration
co
www.5starnotes.com
m
Note how indentation highlights the relationship among these instructions. Note also how
DO WHILE logic tests for the terminal condition at the top of the loop. For example, WHILE condition DO
s.
PERFORM block ENDWHILE
Note the use of indentation and the way WHILE and ENDWHILE delimit the block. The
te
REPEAT UNTIL structure tests for the terminal condition at the bottom of the loop: REPEAT PERFORM block
no
UNTIL condition
Some analysts use a pseudocode structure much like a DO loop to define a countcontrolled
DO index = initial TO limit PERFORM block ENDDO
ar
loop:
w. 5s t
Again, note the indentation and the ENDDO.
The case structure
A common programming problem involves selecting among several alternative paths.
Although nested decision statements could be used to define such logic, the case structure is a better option when nesting goes beyond three or four levels.
ww
The general form of a case structure is: SELECT variable CASE (value-1) block-1 CASE (value-2) block-2 . . .
www.5starnotes.com
www.5starnotes.com DEFAULT CASE block-n
m
ENDSELECT
co
The option selected by the case structure depends on the value of the control variable following the keyword SELECT. If the variable contains value-1, then block-1 is executed; if it contains
value-2, then block-2 is executed, and so on. The DEFAULT CASE is coded to define the logic
s.
to be executed if the control variable contains none of the listed values. A case structure is
delimited by ENDSELECT. Once again, indentation makes the structure easy to read. For example, the following pseudocode routine accepts a code (the TRANSACTION TYPE) and,
SELECT TRANSACTION TYPE CASE (modify) PERFORM MODIFY STOCK
no
CASE (add) PERFORM ADD RECORD
te
based on the value of the code, passes control to one of three lower-level routines.
CASE (delete) PERFORM DELETE RECORD
DEFAULT CASE PERFORM TRANSACTION ERROR
ww
w. 5s t
ar
ENDSELECT
www.5starnotes.com
www.5starnotes.com