Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

BIA B351F L02 Business Programming

Learning Objectives
 Discuss the contents in program specifications.
 Explain the concepts of structured programming and program constructs.
 Describe popular truth tables for NOT, OR, AND, XOR.
 Develop the skills in converting system process (or program) flow into Pseudocodes and Flowcharts.
 Explain the concepts of modular program designs.
 Compare and contrast different program testing methods.
 Describe the procedures to write a program.

1. Program Specification (or Definition / Software Requirements)


• Program Specification is the definition (blueprint) of what a computer program is expected to do. It is a
complete documentation for recording program objectives, desired outputs, needed inputs, required
processing, test plan and constraints, etc.
• Different organizations may have different standards for their own program specification, however, it
usually comes up with at least the following components:
o Program objectives - Make a clear statement of the problem you are trying to solve. Determine
your objectives.
o Input Data - Once you know the output you want, you can determine the input data and the
source of this data.
o Processing Requirements - define the processing tasks that must happen for input data to be
processed into output.
o Desired Outputs - list what you want to get out of the computer system, specify outputs before
inputs.
o Test Plan – describe how a program is tested or validated by using a given sample test data.
o Constraints and Limitations – which records the operating environment, constraints and
limitations of the program. If possible, recommendations for future development could be
suggested.
o Sample template of a program specification could be found from:
 http://www.creativetemplate.net/wp-content/uploads/2017/02/pstemplate-3.doc
 Program Specification (Example)

Uses of Program Specification


• During system analysis in SDLC, a specification helps crystalize the customer’s possibly vague ideas
and reveals contradictions, ambiguities, and incompleteness in the user requirements.
• In program design stage, a specification captures precisely the interfaces and communication standards
between program modules.
• In program verification, a specification is the statement against which a program is proved correct.
• In program validation, a specification can be used to generate test cases for black-box test, unit test, and
integration test.
• In program audit, this becomes a formal documents (as a blueprint) for program auditing – against which
the program can be audited.
• It serves as a kind of program documentation that gives the technical details of a program.

Self-Test 2-1
Search for a sample of program specification from the Internet. Analyze the common contents found
from the program specification document.

1
BIA B351F L02 Business Programming
2. Structured Programming and Program Constructs
In structured programming, the program is divided into small parts (procedures or functions) and each part
performs a specific job. The main importance is on functions rather than data. The same data can be used
and manipulated by several procedures/functions and such type of data is made global.
All programming language utilize program constructs. In imperative languages they are used to control the
order (flow) in which statements are executed (or not executed). There are a number of recognized basic
programming constructs that can be classified as follows:
 Sequence controls (sequence logic or sequential structures)
 Selection controls (selection logic or conditional flow)
 Repetition controls (iteration logic or looping)

1. Sequence controls – If nothing is specified then the program statements (or codes) would be executed
one by one (from top to bottom / from left to right) in a sequential order.
Algorithm:
Statement 1
Statement 2
:
Statement n

Coding and executions are in sequences: from top to bottom,


from left to right …

2. Selection controls – These structures implement the logic called conditional structures. The conditional
structures can be divided into following categories:
Single IF Algorithm

IF Condition applies, THEN


Statement 1
END IF

According to this if the condition is true then Statement 1 is


executed. Otherwise, Statement 1 is skipped.

IF-THEN-ELSE Algorithm:

IF Condition applies, THEN


Statement 1
ELSE
Statement 2
END IF
According to this if the condition is true then
Statement 1 is executed. Otherwise, Statement 2 is
executed.

2
BIA B351F L02 Business Programming
Nested IF Algorithm:
IF Condition 1 applies, THEN
Statement 1
ELSE IF Condition 2 applies, THEN
Statement 2
:
ELSE IF Condition n applies, THEN
Statement n
ELSE
Default Statement
END IF

According to this, only one of the Statements 1, 2, …


or n will be executed. If Condition 1 is true then
Statement 1 will be executed. Otherwise Condition 2
will be checked. If Condition 2 is true then Statement 2
will be executed. And so on. If none of the conditions
is true then Default Statement will be performed.
Switch … Case statement serves the same purpose of
nested IF statement.
3. Repetition controls – Loops are implemented. Program statements in the loop are to be repeated again
and again until some condition is satisfied. It implements while and for loops.
Single Loop Algorithm

DO WHILE Condition applies


Statement 1
Statement 2
:
END LOOP

According to this, the loop tests the condition first


before inside-statements execution.

DO REPEAT
Statement 1
Statement 2
:
UNTIL Condition not applies

According to this, inside-statements are executed


first and then test whether the condition still holds.

Self-Test 2-2
What are program constructs? Use a drawing tool to show their characteristics.

3. Truth Tables
A truth table is a mathematical table used to determine if a compound statement is true or false.
Operator Precedence Action Example
NOT 1 Not NOT X
AND 2 All conditions must be TRUE X AND Y
OR 3 At least one condition is TRUE X OR Y
XOR, (Exclusive OR) 4 Inputs differ outputs TRUE X XOR Y
EQV (logical equivalence) 5 Same Inputs TRUE outputs X EQV Y
IMP 6 FALSE iff the first TRUE, the second FALSE X IMP Y
3
BIA B351F L02 Business Programming
 IMP(lies) operator works as both a logical and bitwise arithmetic operator. It returns FALSE (0) iff its
first operand is TRUE (non-zero) and the second operand is FALSE.
 The IMP operator is used when you want to limit access to a certain "privilege".
 For example, if you have a swimming pool but you don't want anyone entering it when the guard is not
present. So you let Q=Guard is Present and P=Pool is accessible. If Q is false (guard is not there), the
formula (P IMP Q) will always be false, ensuring safety of pool users.

Conditions
Condition of X F F T T
Condition of Y F T F T
NOT X T T F F
X AND Y F F F T
X OR Y F T T T
X XOR Y F T T F
X EQV Y T F F T
X IMP Y T T F T
Some Equivalence Laws of Propositional Logic
Let ∧ be logical AND, ∨ be logical OR. ¬ be logical NOT
(P ∧ Q) ∨ R ≡ (P ∨ R) ∧ (Q ∨ R) (P ∨ Q) ∧ R ≡ (P ∧ R) ∨ (Q ∧ R) Distributivity
P∨P≡P P∧P≡P Idempotency
P∨Q≡Q∨P P∧Q≡Q∧P Commutativity
P ∨ (Q ∨ R) ≡ (P ∨ Q) ∨ R P ∧ (Q ∧ R) ≡ (P ∧ Q) ∧ R Associativity
P ∨ Q ≡ ¬ (¬P ∧ ¬ Q) P ∧ Q ≡ ¬ (¬P ∨ ¬Q) De Morgan
P ∨ true ≡ true P ∧ true ≡ P
true ∨ P ≡ true true ∧ P ≡ P
P ∨ false ≡ P P ∧ false ≡ false
false ∨ P ≡ P false ∧ P ≡ false
¬ ¬P ≡ P Q ≡ ¬ ¬Q Double negative
Self-Test 2-3
Develop truth tables in Excel worksheet to prove:
(a) P AND Q ≡ NOT (NOT P OR NOT Q) // De Morgan’s law
(b) P OR Q ≡ NOT (NOT P AND NOT Q) // De Morgan’s law
(c) X EQV Y ≡ (X IMP Y) and (Y IMP X), if and only if conditions
(d) X IMP Y ≡ (NOT X) OR Y, if X then Y or X implies Y
A B C D E F G H A B C D E F G H
1 P Q P and Q ~P ~Q (~P or ~Q) ~(~P or ~Q) 7 P Q P or Q ~P ~Q (~P and ~Q) ~(~P and ~Q)
2 TRUE TRUE TRUE FALSE FALSE FALSE TRUE 8 TRUE TRUE TRUE FALSE FALSE FALSE TRUE
3 TRUE FALSE FALSE FALSE TRUE TRUE FALSE 9 TRUE FALSE TRUE FALSE TRUE FALSE TRUE
4 FALSE TRUE FALSE TRUE FALSE TRUE FALSE 10 FALSE TRUE TRUE TRUE FALSE FALSE TRUE
5 FALSE FALSE FALSE TRUE TRUE TRUE FALSE 11 FALSE FALSE FALSE TRUE TRUE TRUE FALSE

J K L M N O P J K L M N O P
1 X Y X EQV Y X IMP Y Y IMP X (X IMP Y) and (Y IMP X) 7 X Y X IMP Y ~X Y ~X or Y
2 TRUE TRUE TRUE TRUE TRUE TRUE 8 TRUE TRUE TRUE FALSE TRUE TRUE
3 TRUE FALSE FALSE FALSE TRUE FALSE 9 TRUE FALSE FALSE FALSE FALSE FALSE
4 FALSE TRUE FALSE TRUE FALSE FALSE 10 FALSE TRUE TRUE TRUE TRUE TRUE
5 FALSE FALSE TRUE TRUE TRUE TRUE 11 FALSE FALSE TRUE TRUE FALSE TRUE

4. Algorithm, Pseudocodes and Flowcharts


An algorithm is a procedure for solving a problem in terms of the actions to be executed and the order in
which those actions are to be executed.
 It is informal language that helps programmers developing algorithms (or computer programs).
 It is a type of structured English that is used to specify an algorithm.
 It cannot be compiled nor executed, and there are no real formatting or syntax rules.

4
BIA B351F L02 Business Programming
 This is NO standard syntax for pseudocode, as a program in pseudocode is not an executable
program
 Flowcharts could be thought of as a graphical alternative to pseudocodes or an algorithm.
 Advantages of Pseudocode: reduced complexity, increased flexibility, ease of understanding

Structured English is very similar to Pseudo code, but it tends not to use so many mathematical symbols.
Often people start with Structured English, convert it to Pseudo Code and then write Executable Code.
Structured English Pseudocode Executable Code (VBA)
BEGIN
BEGIN Dim name As String
READ name
INPUT name name = InputBox ("Your name is?")
IF name EQUAL "Harry" THEN
IF name == "Harry" THEN IF name = "Harry" THEN
WRITE "Why don't you marry
OUTPUT "Why don't you marry Pippa?" Debug.Print("Why don't _
Pippa?"
ELSE & you marry Pippa?")
ELSE
OUTPUT "Are you Royal enough?" ELSE
WRITE "Are you Royal enough?"
END IF Debgug.Print("Are you _
END IF & Royal enough?")
END
END End if

The rules of Pseudocode are reasonably straightforward.


 Use consistent (capitalized) keywords. Examples of common keywords:
START, BEGIN, END, STOP, DO, WHILE, DO WHILE, FOR, UNTIL, DO UNTIL,
REPEAT, END WHILE, END UNTIL, END REPEAT, IF THEN, IF, ELSE, IF
ELSE, END IF, THEN, ELSE THEN, ELSE IF, SO, CASE, EQUAL, LT, LE, GT,
GE, NOT, TRUE, FALSE, AND, OR, XOR, GET, WRITE, PUT, UPDATE, CLOSE,
OPEN, CREATE, DELETE, EXIT, FILE, READ, EOF, EOT, WITH, RETURN, …

 All statements showing "dependency" are to be indented. These include: WHILE, DO, FOR, IF,
SWITCH, SUB, FUNCUION, PROCEDURE, …
 Do not include data declarations in pseudocode.
Pseudocode (general) Pseudocode (VBA-like Code)
READ name, hourlyRate, hoursWorked, Rate SET total TO 0
grossPay = hourlyRate * hoursWorked SET grade counter TO 1
deduction = grossPay * Rate WHILE grade counter <= 10
netPay = grossPay – deduction INPUT the next grade
WRITE name, grossPay, deduction, netPay ADD the grade INTO the total
END WHILE
SET class average TO total divided by 10
PRINT class average.
IF HoursWorked > NormalMaximum THEN WHILE Total < Limit
DISPLAY overtime message COMPUTE Total AS Total + Births - Deaths
ELSE END WHILE
DISPLAY regular time message
ENDIF
IF student's grade >= 40 FOR counter = 0 to 100 step 2
PRINT "Passed" SET sum as sum + counter
ELSE NEXT
PRINT "Failed" PRINT sum
END IF

Flowcharts are written with program flow from the top of a page to the bottom. Each command is placed in
a box of the appropriate shape, and arrows are used to direct program flow. A flowchart can therefore be
used to:
o Define and analyze processes
o Build a step-by-step picture of the process for analysis, discussion, or communication
o Define, standardize or find areas for improvement in a process
The following shapes are often used in flowcharts:

5
BIA B351F L02 Business Programming

Process Input / Output Decision

Connector Start / Terminal Arrows


Advantages of Using Flowcharts
 Communication: Flowcharts are better way of communicating the logic of a system to all concerned.
 Effective analysis: With the help of flowchart, problem can be analyzed in more effective way.
 Proper documentation: Program flowcharts serve as a good program documentation, which is needed for
various purposes.
 Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program
development phase.
 Proper Debugging: The flowchart helps in debugging process.
 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.
SET count to 0 SET count to 0
WHILE count < 10 REPEAT
ADD 1 to count ADD 1 to count
WRITE count WRITE count
END WHILE UNTIL count >= 10
WRITE “The end” WRITE “The end”

Self-Test 2-4
State the major differences between Pseudocode and Flowchart.
5. Modular Program Design
Modular programming is the process of sub-dividing a computer program into independent, interchangeable
modules (or sub-programs / procedures / subroutines / functions).

Modular programming has a main module and many auxiliary modules. The main module is compiled as an
executable (EXE), which calls the auxiliary module functions. Auxiliary modules exist as separate
executable files, which load when the main EXE runs. Each module has a unique name assigned in the
PROGRAM statement. Function names across modules should be unique for easy access if functions used
by the main module must be exported.

6
BIA B351F L02 Business Programming
 Modules in modular programming enforce
logical boundaries between components and
improve maintainability.
 They are incorporated through interfaces.
 They are designed in such a way as to minimize
dependencies between different modules.
 Teams can develop modules separately and do
not require knowledge of all modules in the
system.
 Object-oriented programming (OOP) is
compatible with the modular programming
concept to a large extent.
 Modular programming enables multiple
programmers to divide up the work and
debug pieces of the program independently.
 Modular components could be classes, methods,
blocks of codes, or even calls to classes that are
external of the program.

The benefits of using modular programming include:


 Faster development.
 Several programmers can work on individual programs at the same time.
 Easy debugging and maintenance.
 Easy to understand as each module works independently to another module.
 Less code has to be written.
 The scoping of variables can easily be controlled.
 Modules can be re-used, eliminating the need to retype the code many times.
Self-Test 2-5
Which of the following about modular programming is correct?
(a) It breaks a large program into smaller independent modules;
(b) It allows debugging program pieces independently;
(c) It divides work for multiple programmers;
(d) It allows program code reuse;

6. Program testing
Functional Testing – is the type of testing done against the business requirements of application. It is a black
box type of testing.
 Unit testing – Testing of individual software components or modules. Typically done by the
programmer and not by testers, as it requires detailed knowledge of the internal program design and
code. It may require developing test driver modules or test harnesses.
 Integration testing – Testing of integrated modules to verify combined functionality after integration.
Modules are typically code modules, individual applications, client and server applications on a
network, etc. This type of testing is especially relevant to client/server and distributed systems.
 System testing – Entire system is tested as per the requirements. Black-box type testing that is based
on overall requirements specifications, covers all combined parts of a system.
 Acceptance testing -Normally this type of testing is done to verify if system meets the customer
specified requirements. User or customer do this testing to determine whether to accept application.

7
BIA B351F L02 Business Programming

Functional Non Functional


Performance (Load / Stress)
Unit testing
testing

Integration testing Security testing

System testing Usability testing

Acceptance testing Comparitability testing

Non-functional testing is the testing of a program application or system for its non-functional requirements:
the way a system operates, rather than specific behaviours of that system.
 Performance testing – although this term often used interchangeably with ‘stress’ and ‘load’ testing,
they have different scopes of testing. Performance Testing is measuring how a system behaves under
an increasing load (both numbers of users and data volumes), Load Testing is verifying that the
system can operate at the required response times when subjected to its expected load, and Stress
Testing is finding the failure point(s) in the system when the tested load exceeds that which it can
support. All these tests are designed to check whether system meets performance requirements.
 Security testing – Can system be penetrated by any hacking way. Testing how well the system
protects against unauthorized internal or external access. Checked if system, database is safe from
external attacks.
 Usability testing – User-friendliness check. Application flow is tested, Can new user understand the
application easily, Proper help documented whenever user stuck at any point. Basically system
navigation is checked in this testing.
 Compatibility testing – Testing how well software performs in a particular
hardware/software/operating system/network environment and different combination s of above.

Self-Test 2-6
What are the major differences between functional and non-functional testing in programming? Give
some examples for each testing approach.

7. How to start writing a program


 Analyze the problems (or user requirements) first
 Understand the required input, process, output, and test plan from the program specification
 Use Top-down (modular design) approach
 Design programs/procedures/functions
 Use algorithmic thinking to develop detailed logic steps
 Convert your logic steps into pseudocodes, object diagrams, flowcharts, etc.
 Fine tune the pseudocodes / object diagram as program specifications
 Translate the program specifications into program codes
 Keep it Simple – Keep Single-entry Single-exit (SESE), avoid Spaghetti-Codes (in which control
flow is tangled up like Spaghetti)
 Debug your program codes. Trial and Error! Be patience! Be aware of the three major types of the
program errors.
 Run your test plan and use sensible test data for program testing

8
BIA B351F L02 Business Programming
Note:
 Algorithm – a clear and unambiguous specification of steps needed to solve a problem. Algorithm
should be correct and efficient.
 Pseudocode – expresses computer actions using keywords and depicts logic structures using
indentation.
 Control structures (Sequence, Selection and Repetition) – a standard progression of logical steps to
control the sequence of statement execution.

Self-Test 2-7
What are the major differences between Algoritm and Pseudocode? What are the three major errors in
programming?

8. Revision Question
1. Write a set of pseudocodes to sum the first 50 odd integers.
2. Write pseudo code that will perform the following. (a) Read in 5 separate numbers. (b) Calculate the
average of the five numbers. (c) Find the smallest (minimum) and largest (maximum) of the five
entered numbers. (d) Write out the results found from steps (b) and (c) with a message describing
what they are.
3. Draw a flowchart to show the maximum value of three input numbers.
4. Discuss the advantages and disadvantages of modular program design.
5. Distinguish between unit test and integration test. What is a stress test in programming? Why does it
so important for software development?
6. What are the outputs of these VBA code fragments?
Sub logic()
Dim a As Integer
Dim b As Integer
a = 10
b = -10
Debug.Print "a = " & a, "b = " & b
If a <> 0 Or b <> 0 Then
Debug.Print ("OR Operator Result is : True")
Else
Debug.Print ("OR Operator Result is : False")
End If

If Not (a <> 0 Or b <> 0) Then


Debug.Print ("NOT Operator Result is : True")
Else
Debug.Print ("NOT Operator Result is : False")
End If

If (a <> 0 Xor b <> 0) Then


Debug.Print ("XOR Operator Result is : True")
Else
Debug.Print ("XOR Operator Result is : False")
End If
End Sub

You might also like