Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Control Structures.

Similar presentations


Presentation on theme: "Chapter 2 Control Structures."— Presentation transcript:

1 Chapter 2 Control Structures

2 Control Structures Solving a problem using the Stepwise refinement method, the instructions that make up the algorithm and the structured chart are combinations of 3 control structures: Sequence Structure Selective Structure Iterative Structure

3 Sequence Structure The most commonly used and simple structure
Consists of two or more sub components Instructions are executed/processed one after another in a sequence, in a top to bottom manner The structure is read from left to right Normally used in conjunction with one or more of the other control structures; ie, selection and/or iteration

4 Sequence Structure The instructions are represented in the subcomponents of the structured diagram , i.e., elementary components An elementary component is one which has no lower-level components associated and is represented in a data structure diagram simply by placing its name inside a rectangle

5 Sequence Structure The sequence control structure can be used to represent 4 basic computer operations; to receive information e.g. Read customername, address put out information e.g. Display pageCount perform arithmetic e.g. add 1 to pageCount assign values e.g. set pageCount to zero

6 Sequence Structure: General
B C The component (box) A is a sequence. Boxes B and C (Elementary components) represent component parts of the sequence (sub-components) A is a sequence of B followed by C.

7 Sequence structure: Pseudocode
use a separate line for each logical element. align each statement vertically until a change in logic flow occurs Sequence block is preceded by BEGIN, terminated by END

8 Sequence Structure: Pseudocode
General format: BEGIN SEQUENCE_BLOCK_NAME sequence sequence statement; END SEQUENCE_BLOCK_NAME sequence

9 Sequence Structure: Example 1
Name Read Title Read FirstName Read Surname Print full name Sub-components of a Sequence Structure - Name

10 Sequence Structure: Pseudocode Example 1
BEGIN NAME sequence Read Title FirstName Surname Name Print full name READ title; READ firstname; READ surname; WRITE title + firstname +surname; END NAME sequence

11 Sequence Structure: Example 2
Problem Specification: Design a program to process an order. A process consists of accepting the quantity sold and price of an item and then display the sales value

12 Problem Analysis Initialization Input Definition Output Definition
none Input Definition qty, price Output Definition sales : $

13 Problem Analysis Processing Requirements Read qty, price
Calculate sales sales = qty * price Display sales Processing Control sales is displayed in 2 decimal places

14 Problem Analysis Test Data & Expected Result qty price sales 12 10.00
120.00 5 100.00 500.00 10 2.00 20.00

15 Sequence Structure: Example 2
Order Display sales sales = qty * price Read qty, price

16 Sequence Structure: Example 2
Pseudocode: BEGIN ORDER sequence END ORDER sequence Order Display sales sales = qty * price Read qty,price READ qty, price; sales = qty * price; WRITE sales;

17 Sequence Structure: Example 3
Problem Specification: Calculate the amount due on an electricity bill. The present meter reading and the previous meter readings are entered. The amount due is the difference between the two readings multiplied by the cost of a unit of electricity, currently 5 cents per unit.

18 Sequence Structure: Your task!
Do the PROBLEM ANALYSIS Draw the STRUCTURED CHART Write the PSEUDOCODE

19 Problem Analysis Initialization Input Definition Output Definition
Cost = 0.05 Input Definition current, previous Output Definition Amount due : $

20 Problem Analysis Processing Requirements Read current, previous
Calculate bill 2.1. used = current – previous 2.2. bill = used * cost Display bill Processing Control current value must be >= previous value bill is displayed in 2 decimal places

21 Problem Analysis Test Data & Expected Result current previous used
bill 1200 680 520 26.00 5000 1328 3672 183.60 10000 13400 error

22 Sequence Structure: Example 3
Electricity Bill Cost = 0.05 Read meters Calculate bill Display bill Read current Read previous used = current – previous bill = used*cost

23 Sequence Structure: Example 3
BEGIN ELECTRICITYBILL sequence cost = 0.05; BEGIN READMETERS sequence READ current; READ previous; END READMETERS sequence BEGIN CALCULATEBILL sequence used = current – previous; bill = used * cost; END CALCULATEBILL sequence WRITE bill; END ELECTRICITYBILL sequence Electricity Bill Calculate bill Display bill cost = 0.05 Read meters Read current Read previous used = current – previous bill = used*cost

24 Sequence Structure: Example 4
A ONE DOLLAR shop wants you to write a program to read in the sales price in cents which is not to exceed one dollar. The change from one dollar is then to be calculated and printed as the most compact set of change (i.e. the fewest coins). It is assumed that the denominations are 50¢, 20¢, 10¢, 5¢ and 1¢

25 Sequence Structure: Example 4
Initialization none Input Definition sales_price Output Definition Denomination: 50¢ - 9 20¢ - 9 10¢ - 9 5¢ - 9 1¢ - 9

26 Problem Analysis Processing Requirements Read sales_price
Calculate change from $1 change = 100 – sales_price Calculate set of change coin50 = change / 50 change = change – (coin50 * 50) coin20 = change / 20 change = change – (coin20 * 20) coin10 = change / 10 change = change – (coin10 * 10) coin5 = change / 5 change = change – (coin5 * 5) coin1 = change Print coin50, coin20, coin10, coin5, coin1

27 Problem Analysis Processing Control Test Data & Expected Result
sales_price must be between 1 and 100 cents inclusive Test Data & Expected Result sales_price change coin50 coin20 coin10 coin5 coin1 40 60 1 14 86 120 error

28 Sequence Structure: Example 4
Change Read sales_price change = 100 – sales_price Set of change Display change A B

29 Sequence Structure: Example 4
coin50 = change/50 coin20 = change/20 coin10 = change/10 coin1 = change coin5 = change/5 change= change – (coin50*50) change= change – (coin20* 20) change= change – (coin10*10) change= change – (coin5*5)

30 Sequence Structure: Example 4
B coin50 coin20 coin10 coin5 coin1

31 Sequence Structure: Example 4
coin5 = change / 5; change = change - (coin5*5); coin1 = change; END SETOFCHANGE sequence BEGIN DISPLAYCHANGE sequence WRITE coin50; WRITE coin20; WRITE coin10; WRITE coin5; WRITE coin1; END DISPLAYCHANGE sequence END CHANGE sequence Pseudocode BEGIN CHANGE sequence READ sales_price; change = 100 – sales_price; BEGIN SETOFCHANGE sequence coin50 = change/50; change = change - (coin50*50); coin20 = change / 20; change = change - (coin20*20); coin10 = change / 10; change = change-(coin10*10);

32 Class Exercise Using stepwise Refinement method (sequence control structure) analyse the following problem, draw the structured chart and write the pseudocode: A major department store needs a program to prepare a bill for each customer. Lets assume that each customer purchases only one item type. There will be 4 input values required; customer’s name, itemID, quantity purchased and unit price. Output will be the customer’s bill after a 10% discount is taken before taxes and a 5 percent sales tax is added.


Download ppt "Chapter 2 Control Structures."

Similar presentations


Ads by Google