1 / 28

Control Structures (A)

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.

betsy
Download Presentation

Control Structures (A)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structures (A) Topics to cover here: • Introduction to Control Structures in the algorithmic language • Sequencing

  2. 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

  3. 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.

  4. 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).

  5. INPUT Statement Syntax: INPUTList 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).

  6. 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.

  7. 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).

  8. OUPUT Statement (in pseudo code).. cont. 2- OUTPUTmessage 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”

  9. OUPUT Statement (in pseudo code) (Cont.) The semantics (execution) of this statement: This statement will display the message on the screen. 3- OUTPUTexpression 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.

  10. 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.

  11. 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)

  12. 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.

  13. 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

  14. 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  5 Y  10 X  Y // now X has the value 10 X Y 10 5 10

  15. 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).

  16. 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

  17. 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

  18. 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

  19. 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

  20. Example 2 .. cont. 2- Algorithm Design ALGORITHM Convert INPUT length centimeters  2.54 * length OUTPUT “Length in centimeters= “, centimeters END Convert

  21. 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

  22. 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

  23. Example 3 .. cont. 2- Algorithm Design ALGORITHM Pays INPUT hours, rate wage  hours * rate OUTPUT “Employee wage = “, wage END Pays

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

More Related