Control Structures (A)

Post on 14-Jan-2016

69 views 1 download

Tags:

description

Control Structures (A). Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing. Control Structures. In your algorithm (or program), you can use different types of statements. - PowerPoint PPT Presentation

transcript

Control Structures (A)

Topics to cover here:

• Introduction to Control Structures in the algorithmic language

• Sequencing

Control Structures

• In your algorithm (or program), you can use different types of statements.

• Each statement starts with a specific keyword (except for the assignment statement (see later)).

• There are 3 categories of control structures:1- Sequencing2- Selection3- Repetition

1- Sequencing

• It is a sequential control given in a compound statement or a block.

• A compound statement (or a block) is a sequence of statements ordered in a way to give a solution: e.g.

S1 S2 S3is a sequence of 3 statementsIn C++, the ; symbol is used to separate

statements.

1- Sequencing (Cont.)

• The statements that have a sequential control:

1- INPUT/OUPUT statements

2- Assignment statement

INPUT/OUPUT statements

INPUT statement (in pseudo code):

Use Input statement to input data into variables from the standard input device (e.g. a keyboard).

INPUT Statement

Syntax: INPUT List of variableswhere, List of variables contains one or more variables

e.g. INPUT x INPUT a, b

The semantics (execution) of this statement:You can enter the values you want for the variables in the statement from the keyboard and the computer will assign these values into the variables (stores them in memory).

OUPUT Statement (in pseudo code)

• The OUTPUT statement has many uses.

• Use OUTPUT statement to output

- The values of variables stored in memory.- A message (i.e. a string of characters).- The value of an expression.

OUPUT Statement (in pseudo code).. cont.

Syntax: 1- OUTPUT List of variableswhere, List of variables contains one or more variables

e.g. OUTPUT x OUTPUT a, b

The semantics (execution) of this statement:This statement allows the computer to access the locations of the variables mentioned in the statement and displays their contents on an output device (e.g. a screen).

OUPUT Statement (in pseudo code).. cont.

2- OUTPUT message where message may by any string of characters enclosed with double quotas.

Use such statement- for making your output clearer- for helping the user of your program to interact easily with it. e.g. OUTPUT “Enter 3 values”

OUPUT Statement (in pseudo code) (Cont.)

The semantics (execution) of this statement:This statement will display the message on the screen.

3- OUTPUT expression where expression is any arithmetic expression

e.g. OUTPUT 3 + 6 OUTPUT x – y

The semantics (execution) of this statement:First, the expression is evaluated, then the result will be displayed on the screen. For the first example, it will display 9.

OUPUT Statement (in pseudo code).. cont.

NOTE

You can mix between the different types of the OUTPUT statements.

e.g.

OUTPUT “Length = “ , length

The semantics (execution) of this statement:

This statement will display the message

Length = on the screen and on the same line it will display the value of the variable length.

Assignment Statement (in pseudo code)

• Storing a new value in a memory location is called assignment.

• We use the operator as assignment operator.

Syntax: Variable Expression

The semantics (execution) of this statement:1- The Expression on the right of is evaluated 2- The result of the expression is assigned to the variable

on the left of .e.g. X 10 (This means that X has 10 now ) Y X + 5 (This means that Y has 11 now, if X is 6)

Assignment Statement (Cont.)

NOTE: The right hand side (RHS) of the assignment statement should be of the same data type of the left hand side (LHS).e.g.1- T trueThis will be correct if T is of Boolean type.

2- A x + y * 2This will be correct if A has a numeric data type (e.g. integer, or real) and the value of the expression on (RHS) has the same numeric data type.

Assignment Statement (Cont.)

• How to execute a statement like X X + 1 ?

Suppose we have:

X 5

Then to execute X X + 1, we proceed as follows:

X X 5

X X + 1

5 6

Assignment Statement .. cont.

• Dereferencing:If we want to copy a value from one memory location (say, X) into another location (say, Y), we say that we dereference a variable.e.g.

X 5Y 10X Y // now X has the value 10

X Y

105 10

Examples on Simple Algorithms

Example 1

Write an algorithm to determine the total cost of apples given the number of kilos of apples purchased and the cost per kilo of apples.

First, we have to analyze the problem to understand what is the input, output of the problem, and which formula to use to solve the problem (if any).

Example1 .. cont.

1- Analysis stage:• Problem Input:

- Quantity of apples purchased (in kilos)

- Cost per kilo of apples (in dinar/fils per kilo)

• Problem Output:

- Total cost of apples (in dinar/fils)

• Formula: Total cost = Number of kilos of apples × Cost per kilo

Example1 .. cont.

2- Algorithm DesignWe write the algorithm by using the pseudo code keywords (written in bold type). For algorithm writing, please refer to the attachment given with the syllabus that is handed to you, previously.

ALGORITHM applesINPUT quantity, costtotal_cost quantity * costOUTPUT “Total cost = “ , total_cost

END apples

Example1 .. cont.

3- Testing the algorithmWe give a sample data to see whether the algorithm solves the

problem correctly or not.To give a sample data, proceed as follows:1- Prepare a table to contain all variables of the algorithm.2- Give any data you like as input.3- Calculate any formula in the algorithm using these data.4- Show the outpute.g.

quantity cost total_cost1. 3 0.9 -

2. 2.7

The output:Total cost = 2.7

Example 2 **

•The problem statement:Write an algorithm that converts a given length from feet and inches into centimeters.

1- Analysis stage:• Problem Input: - Length (in feet and inches)

• Problem Output: - Length in centimeters

• Formula:Length in centimeters = 2.54 × Length in feet & inches

Example 2 .. cont.

2- Algorithm Design

ALGORITHM Convert

INPUT length

centimeters 2.54 * length

OUTPUT “Length in centimeters= “,

centimeters

END Convert

Example 2 .. cont.

3- Testing the algorithm length centimeters 2.5 ---

6.35 The output: Length in centimeters = 6.35------------------------------------------------------------------------- length centimeters 0.4 --- 1.016The output: Length in centimeters = 1.016

Example 3

•The problem statement

Write an algorithm that calculates the wage of an employee, if the number of hours that he worked and the rate of each hour are given.

1- Analysis stage:• Problem Input:

- Hours (the employee has worked)

- Rate of money per hour• Problem Output:

- The wage that the employee will receive.• Formula:

Wage = Hours × Rate of money per hour

Example 3 .. cont.

2- Algorithm Design

ALGORITHM Pays

INPUT hours, rate

wage hours * rate

OUTPUT “Employee wage = “,

wage

END Pays

Example 3 .. cont.

3- Testing the algorithm

hours rate wage

30 1.5 ---

45

The output

Employee wage = 45 --------------------------------------------------------------------------------------------------------------------------------------------------------

hours rate wage

37 2.3 ---

85.1

The output

Employee wage = 85.1

Example 4

• The problem statementSuppose that a store makes discount on its items. Design an algorithm that calculates the amount of money that a customer has to pay on purchasing an item adding to it the required tax, where the following data are given: the price of the item before discount, the discount rate, and the tax rate.

1- Analysis stage:• Problem Input: - Original Price of the item - Discount rate for the item - Tax rate

Example 4 .. cont.

•Problem Output:

- Price of the item after discount

• Formulae:

discount = original price × discount rate

price = original price – discount

tax = price × tax rate

price = price + tax

Example 4 .. cont.

2- Algorithm DesignALGORITHM Purchases INPUT price, discount_rate, tax_rate discount price * discount_rate price price - discount tax price * tax_rate price price + tax OUTPUT “Price = “, priceEND Purchases

Example 4 .. cont.

3- Testing the algorithm

price discount_rate tax_rate discount tax

30 0.20 0.16 --- ---

6

24

3.84

27.84

The output:

Price = 27.84