DCT 1123 PROBLEM SOLVING ALGORITHM CONTROL STRUCTURE SEQUENCING
DCT 1123 PROBLEM SOLVING & ALGORITHM CONTROL STRUCTURE - SEQUENCING
CONTENTS • Input & output statement • Assignment statement • Arithmetic
Sequence Control • Sequence control refers to the linear execution of codes within a program. • In sequence control, the statements are executed one by one in consecutive order.
Example of Pseudo code of Sequence Control • This program will request the user’s date of birth and then request today’s date, calculate the age and finally will print the user’s age. • For example, today’s date is 1 -JAN- 2006, then the results will be shown as follows:
Example of Flowchart of Sequence Control • The flow chart represents a program that will request the user’s date of birth and then request today’s date, calculate the age and finally will display the user’s age.
Sequence Control • 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. • S 1 ; • S 2; • S 3; – is a sequence of 3 statements • In C++, the ; symbol is used to separate statements.
Sequence Control The statements that have a sequential control: 1. INPUT/OUPUT statements – Use INPUT statement to input data into variables from the standard input device (e. g. a keyboard). – 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. 2. Assignment statement
INPUT Statements ( Pseudo Code) 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).
OUTPUT Statements ( Pseudo Code) 1 -Syntax: 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).
OUTPUT Statements ( Pseudo Code) 2 -Syntax: OUTPUT message where message may by any string of characters enclosed with double quotas. e. g. OUTPUT “Enter 3 values” The semantics (execution) of this statement: • This statement will display the message on the screen.
OUTPUT Statements ( Pseudo Code) 3 - Syntax: 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.
OUTPUT Statements ( Pseudo Code) 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.
Assignments Statements • 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.
Assignments Statements • 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)
Assignments Statements • 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;
Assignments Statements • 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 right hand side of the assignment, and the contents of variable x (24) is assigned to y.
Assignments Statements 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.
Assignments Statements 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 5 6 X 5 X X+1
Assignments Statements 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 5 10 10
Arithmetic Operators • C++ arithmetic operators + addition - subtraction or negation * multiplication / division % mod [modulus or remainder] • +, -, *, and / can be used with integral and floating-point data types • Operators can be unary or binary 20
Arithmetic Operators Example: Storing a product in the variable total_weight = one_weight * number_of_bars; C++ Code : Example total_weight 21
Results of Operators • Arithmetic operators can be used with any numeric type • An operand is a used by the operator number or variable • 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 22
Division of Doubles • Division with at least one operator of type double produces the expected results. • Example : double divisor, dividend, quotient; divisor = 3; dividend = 5; quotient = dividend / divisor; • Result : quotient = 1. 6666… • Result is the same if either dividend or divisor is of type int 23
Division of Integers • Be careful with the division operator! • int / int produces an integer result (true for variables or numeric constants) • Example : 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! 24
Integer Remainders • % operator gives the remainder from integer division • Example : int dividend, divisor, remainder; dividend = 5; divisor = 3; remainder = dividend % divisor; • The value of remainder is 2 25
Arithmetic Expressions • 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) 26
Operator of Precedence • All expressions inside of parentheses are evaluated first • Multiplication and division from left to right • Addition and subtraction from left to right 27
Order of Precedence • All operations inside of () are evaluated first • *, /, and % are at the same level of precedence and are evaluated first • + and – have the same level of precedence and are evaluated last • When operators are on the same level – Performed from left to right (associativity) • 3 * 7 - 6 + 2 * 5 / 4 means (((3 * 7) – 6) + ((2 * 5) / 4 )) 28
Operator Shorthand • Some expressions occur so often that C++ contains to shorthand operators for them • All arithmetic operators can be used this way += count + 2; count += 2; becomes *= bonus * 2; becomes bonus *= 2; /= time / rush_factor; becomes time /= rush_factor; %= remainder % (cnt 1+ cnt 2); becomes remainder %= (cnt 1 + cnt 2); 29
- Slides: 30