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- Sequencing 2- Selection 3- 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 S3 is a sequence of 3 statements In 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 variables where, 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 variables where, 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 OUTPUT 3+6 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 true This will be correct if T is of Boolean type. 2- A x + y * 2 This 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: X5 Then to execute X X + 1, we proceed as follows: X 5 6 X5 XX+1 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. X5 Y 10 XY // now X has the value 10 X 5 10 Y 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 Design We 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 apples INPUT quantity, cost total_cost quantity * cost OUTPUT “Total cost = “ , total_cost END apples Example1 .. cont. 3- Testing the algorithm We 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 output e.g. quantity cost total_cost 1. 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.016 The 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 37 2.3 wage --85.1 The output Employee wage = 85.1 Example 4 • The problem statement Suppose 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 Design ALGORITHM Purchases INPUT price, discount_rate, tax_rate discount price * discount_rate price price - discount tax price * tax_rate price price + tax OUTPUT “Price = “, price END Purchases Example 4 .. cont. 3- Testing the algorithm price discount_rate tax_rate 30 0.20 0.16 discount --6 tax --- 24 3.84 27.84 The output: Price = 27.84