270 likes | 384 Views
PROBLEM SOLVING & ALGORITHM (DCT 1123). CHAPTER 4: CONTROL STRUCTURES - SEQUENCING. CONTENTS. Input & output statement Assignment statement. Control Structures. In your algorithm (or program), you can use different types of statements.
E N D
PROBLEM SOLVING & ALGORITHM (DCT 1123) CHAPTER 4: CONTROL STRUCTURES - SEQUENCING
CONTENTS • Input & output statement • Assignment statement
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
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. Example
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: 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).
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- 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”
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.
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 • 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 • 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 • Assigning an immediate constant to a variable. 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. int x; x = 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.) 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: X 5 Then to execute X X + 1, we proceed as follows: X X 5 X X + 1 5 6
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
Arithmetic • Arithmetic is performed with operators • + for addition • - for subtraction • * for multiplication • / for division • Example: storing a product in the variabletotal_weighttotal_weight = one_weight * number_of_bars; • Example
Results of Operators • 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 • Division with at least one operator of type doubleproduces 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
Division of Integers • 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 • % 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 • 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 • All expressions inside of parentheses are evaluated first • Multiplication and division from left to right • Addition and subtraction from left to right
Operator Shorthand • 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 • 2003 Pearson Education, Inc.