Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)

Similar presentations


Presentation on theme: "CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)"— Presentation transcript:

1 CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)

2 CONTENTS 10/14/2014 Input & output statement Variables, constant & literals Data Types Assignment statement

3 CONTROL STRUCTURES 10/14/2014 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

4 SEQUENCING 10/14/2014 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. Example

5 SEQUENCING (CONT.) 10/14/2014 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).

6 INPUT STATEMENT 10/14/2014 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).

7 OUPUT STATEMENT (IN PSEUDO CODE) 10/14/2014 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.

8 OUPUT STATEMENT (IN PSEUDO CODE).. CONT. 10/14/2014 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).

9 OUPUT STATEMENT (IN PSEUDO CODE).. CONT. 10/14/2014 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”

10 OUPUT STATEMENT (IN PSEUDO CODE) (CONT.) 10/14/2014 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.

11 OUPUT STATEMENT (IN PSEUDO CODE).. CONT. 10/14/2014 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.

12 ASSIGNMENT STATEMENT 10/14/2014 An assignment statement is the mechanism for initializing or changing the value stored in a designated segment of memory during the execution of a program. An assignment statement consists of: - A variable that has been previously declared appearing alone on the left side of the statement. - The assignment operator = - A constant, variable, or mathematical expression that is located to the right of the assignment operator.

13 ASSIGNMENT STATEMENT 10/14/2014 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)

14 ASSIGNMENT STATEMENT 10/14/2014 Assigning an immediate constant to a variable. int x; x = 24; The declaration allocates 4 bytes of memory to the variable x, and assigns the default value 0. The assignment statement changes the value stored in x to 24. Note! Declaration and assignment can be combined in the single statement. int x = 24; An assignment statement is used to move (copy) a value from one memory location to another. int x, y; x = 24; y = x; In the statement y = x; the identifier x appears on the righthand side of the assignment, and the contents of variable x (24) is assigned to y.

15 ASSIGNMENT STATEMENT (CONT.) 10/14/2014 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.

16 ASSIGNMENT STATEMENT (CONT.) 10/14/2014 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

17 ASSIGNMENT STATEMENT.. CONT. 10/14/2014 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

18 ARITHMETIC 10/14/2014 Arithmetic is performed with operators  + for addition  - for subtraction  * for multiplication  / for division Example: storing a product in the variable total_weight total_weight = one_weight * number_of_bars; Example

19 RESULTS OF OPERATORS 10/14/2014 Arithmetic operators can be used with any numeric type An operand is a number or variable used by the operator Result of an operator depends on the types of operands  If both operands are int, the result is int  If one or both operands are double, the result is double

20 DIVISION OF DOUBLES 10/14/2014 Division with at least one operator of type double produces the expected results. double divisor, dividend, quotient; divisor = 3; dividend = 5; quotient = dividend / divisor;  quotient = 1.6666…  Result is the same if either dividend or divisor is of type int

21 DIVISION OF INTEGERS 10/14/2014 Be careful with the division operator!  int / int produces an integer result (true for variables or numeric constants) int dividend, divisor, quotient; dividend = 5; divisor = 3; quotient = dividend / divisor;  The value of quotient is 1, not 1.666…  Integer division does not round the result, the fractional part is discarded!

22 INTEGER REMAINDERS 10/14/2014 % operator gives the remainder from integer division int dividend, divisor, remainder; dividend = 5; divisor = 3; remainder = dividend % divisor; The value of remainder is 2

23 ARITHMETIC EXPRESSIONS 10/14/2014 Use spacing to make expressions readable  Which is easier to read? x+y*z or x + y * z Precedence rules for operators are the same as used in your algebra classes Use parentheses to alter the order of operations x + y * z ( y is multiplied by z first) (x + y) * z ( x and y are added first)

24 OPERATOR PRECENDENCE 10/14/2014 All expressions inside of parentheses are evaluated first Multiplication and division from left to right Addition and subtraction from left to right

25 OPERATOR SHORTHAND 10/14/2014 Some expressions occur so often that C++ contains to shorthand operators for them All arithmetic operators can be used this way  += count = count + 2; becomes count += 2;  *= bonus = bonus * 2; becomes bonus *= 2;  /= time = time / rush_factor; becomes time /= rush_factor;  %= remainder = remainder % (cnt1+ cnt2); becomes remainder %= (cnt1 + cnt2);

26 References 10/14/2014 2003 Pearson Education, Inc.

27 10/14/2014


Download ppt "CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)"

Similar presentations


Ads by Google