CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
CONTENTS 10/14/2014 Input & output statement Variables, constant & literals Data Types Assignment statement
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
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
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).
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).
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.
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).
OUPUT STATEMENT (IN PSEUDO CODE).. CONT. 10/14/ 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.) 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 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. 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.
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.
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)
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.
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.
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
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
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
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
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 = … Result is the same if either dividend or divisor is of type int
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!
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
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)
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
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);
References 10/14/ Pearson Education, Inc.
10/14/2014