Upload
marianna-shields
View
222
Download
1
Embed Size (px)
Citation preview
Lesson 2
McManusCOP1006 1
Computational◦ problems involving some kind of mathematical
processing Logical
◦ Problems involving relational or logical processing Repetitive
◦ Problems involving repeating a set of mathematical and/or logical instructions.
McManusCOP1006 2
The building blocks of equations and expressions◦ Constants◦ Variables◦ Operators◦ Functions
McManusCOP1006 3
A value◦ a specific alphabetical and/or numeric value ◦ Does not change during the processing of all
the instructions in a solution Can be of any data type
◦ Numeric, alphabetical or special symbols Examples
◦ 3, 5, 10, “Mulder”, “Scully”, “+”, “-”, “/”◦ Note: “” indicates datum rather than variable
name
McManusCOP1006 4
In some programming languages, constants can be named◦ Provides protection to the constant value and
other areas of the program.◦ Cannot be changed once given initial value
Example◦ SalesTaxRate = 6◦ Commission_Rate = 6◦ Note: no spaces within variable names
McManusCOP1006 5
May change during processing A name is assigned to each variable used in
a solution.◦ The name references the memory area where the
value is stored. May be of any data type Examples
◦ Age, LastName, Address, PayRate
McManusCOP1006 6
Use mnemonic terms, that is, the name means something◦ Use PayRate not Z
Do not use spaces◦ Either combine terms (PayRate) or include an
underscore to separate (Pay_Rate) Do not use special symbols Be consistent!
◦ Some languages are case sensitive◦ PayRate, Pay_Rate, PAYRATE, or PAY_RATE
Use appropriate naming convention
McManusCOP1006 7
Data◦ Raw, organized facts
Information◦ Meaningful output
McManusCOP1006 8
Most common types◦ Numeric◦ Character◦ Logical
Most languages include other data types◦ Date◦ Currency◦ User-defined
McManusCOP1006 9
Data Type Data Set ExamplesInteger All whole numbers 3456
-43
Real All real numbers (whole + decimal)
3456.78
0.000123
Character (uses quotation marks)
All letters, numbers, and special symbols
“A”, “a”, “1”, “5”, “+”, “%”
String (uses quotation marks)
Combinations of more than one character
“Mulder”“Scully”“123-45-6789”
Logical True or False TrueFalse
McManus COP1006 10
Include all types of numbers◦ Integers
3, 5, -5, 0, -1 and 32,767◦ Real numbers (floating point numbers)
3.1459, -5745 Scientific Notation
2.3E5 or 5.4E-3, where E stands for the Power of 10
◦ Can be Positive or Negative
McManusCOP1006 11
Alphanumeric data set◦ Consists of
all single-digit numbers, letters, and special characters available to the computer
◦ contained within quotation marks
McManusCOP1006 12
EBCDIC – Extended Binary Coded Decimal Information Code◦ Used on IBM mainframes
ASCII - American Standard Code for Information Interchange◦ 256 characters (1 byte)
First 128 – standard Second 128 – specific to computer
Unicode (2 bytes)◦ Replacing ASCII as standard on PC’s
McManusCOP1006 13
See Appendix C
Made up of one or more characters. Contained within quotation marks Cannot be used within calculations
McManusCOP1006 14
“123” is not the same thing as 123◦ The first one is a string◦ The second one is a number
Becomes important when deciding when a piece of data is a string or a number◦ Ex. Social Security Numbers
The first one contains 11 characters and is left aligned whereas the second contains 9 numbers and is right aligned
McManusCOP1006 15
The computer’s particular coding scheme (ASCII, Unicode, or EBCDIC) converts the character to its associated ordinal number◦ Note: Uppercase letters have a smaller ordinal
values than lowercase letters “A” = 193 whereas “a” = 129 (Unicode 16-bit) “A” = 65 whereas “a” = 97 (ASCII 8-bit)
Once converted, each character, one at a time, can be compared
McManusCOP1006 16
Joins character data or string data together
Operators: + and &◦ Dependent on programming language◦ & can mix data types◦ + cannot mix data types (also known as Join)
Examples◦ FName & “ ” & MI & “ ” & LName◦ “James” + “ ” + “T.” + “ ” + “Kirk” = “James T. Kirk”◦ “James” & “ ” & “T.” & “ ” & “Kirk” = “James T. Kirk”
Note the space at the end of the T.
McManusCOP1006 17
Consists just two pieces of data◦ True and False◦ Different programming languages also allow
Yes and No 1 (True) and 0 (False) On and Off
McManusCOP1006 18
Dependent on programming language, numerous other pre-defined data types are available.◦ Examples
Dates 01/01/1900 January 1, 1900 Times 03:35:05 PM 15:35:05 Currency 3000.00
◦ Note: This is how the data will be stored, not how it is formatted.
McManusCOP1006 19
User-defined Data types◦ Defined by the user as a new data type
Examples Records
Employee_RecordFnameLnameSSN
End
McManusCOP1006 20
Programmer designates the data type Data types cannot be mixed
◦ Called data typing All data to be used in calculations must be
declared as a numeric data type Each data type uses a data set or domain
◦ Integers – typically -32768 -> 0 -> 32767 but is programming language dependent
◦ Characters – all alphanumeric characters included in the computer’s coding scheme
◦ Logical – the words “true” and “false”
McManusCOP1006 21
Small sets of instructions that perform specific tasks and return values.
Two types:◦ Pre-defined
Built into a computer language or application.◦ User-defined
McManusCOP1006 22
Benefits◦ Reduces the amount of code that needs to be
written, thus reducing errors of repetition◦ Shortens the development time◦ Improves readability
McManusCOP1006 23
Data usually placed within parentheses that the function uses without altering the data◦ In Sqrt(n), the n represents the parameter, in this
case a number◦ In Value(s), the s represents a string
McManusCOP1006 24
Mathematical String Conversion Statistical Utility Specific to each programming language…a
partial list follows
McManusCOP1006 25
McManusCOP1006 26
Function Form Example Result
Square Root Sqrt(n) Sqrt(4) 2
Absolute Abs(n) Abs(-3), Abs(3) 3
Rounding Round(n,n1) Round (3.7259,2) 3.73
Integer Integer(n) Integer(5.7269) 5
Random Random Random 0.239768
Sign of a number Sign(n) Sign(7.39) 1
McManusCOP1006 27
Function Form Example Result
Middle String Mid(s, n1, n2)Mid(S,3,2) where S = "Thomas" "om"
Left String Left(s, n)Left(S,3) where S = "Thomas" "Tho"
Right String Right(s, n)Right(S, 3) where S = "Thomas" "mas"
Length String Length(s)Length(S) where S = "Thomas" 6
McManusCOP1006 28
Function Form Example Result
Valued Conversion Value(s) Value("-12.34") -12.34
String Conversion String(n) String(-12.34) "-12.34"
McManusCOP1006 29
Function Form Example Result
Average Average(list) Average(5, 3, 8, 6) 5.5
Maximum Max(list) Max(5, 3, 8, 6) 8
Minimum Min(list) Min(5, 3, 8, 6) 3
Summation Sum(list) Sum (5, 3, 8, 6) 22
McManusCOP1006 30
Function Form Example Result
Date Date Date 9/14/02
Time Time Time 9:22:38
Error is a special function that appears in most languages and upon execution return control to the program when (not if) system errors occur.
The data connectors within expressions and equations.
Two types of operations◦ Unary
Negation◦ Binary
Addition, Subtraction, Multiplication, Floating Point Division and Integer Division
McManusCOP1006 31
Mathematical◦ +, -, *, /◦ \, Mod ◦ ^, and ◦ functions
Relational◦ <, >, <=, >=, =, <>
Logical◦ And, Or, Not, XOR, EQV
McManusCOP1006 32
Integer Division (\)◦ Regular Floating Point Division, except only the
whole number part of the answer is returned.◦ If given a Floating Point number, the number is
rounded—first—before division occurs. Modulo Division (Mod) or Modulus
◦ The remainder part of the answer is returned.
McManusCOP1006 33
Returns either the keywords True or False or
Returns a non-zero or zero value.◦-9, -1, 1, 2 are equivalent to True◦0 is equivalent to False
Is stored in two bytes
McManusCOP1006 34
Perform comparisons variable - relational operator - variable
◦Ex.If Y > Z then ...
◦ = equal◦ <> not equal◦ < less than◦ > greater than ◦ <= less than or equal to◦ >= greater than or equal to
McManusCOP1006 35
And, Or, XOR, Not, EQV Boolean Expressions are used
◦ To create more complicated Boolean Expressions.◦ If a = b AND c < d Then …
They are defined by using Truth Tables…◦ Know the significance of each!
McManusCOP1006 36
Significance: The only time the result is True is if both A and B are True.
McManusCOP1006 37
A = B = then A AND BTrue True TrueTrue False FalseFalse True FalseFalse False False
Significance: The only time the result is False is if both A and B are False.
McManusCOP1006 38
A = B = then A OR BTrue True TrueTrue False TrueFalse True TrueFalse False False
Also called the “logical complement” or “logical negation”
Significance: Whatever the value of A is, NOT A will be the opposite.
McManusCOP1006 39
A = NOT A
True FalseFalse True
Significance: The only time the result is True is if A and B are different and False if both A and B are the same.
McManusCOP1006 40
A = B = then A XOR BTrue True FalseTrue False TrueFalse True TrueFalse False False
Significance: The only time the result is True is if A and B are the same and False if both A and B are the different.
McManusCOP1006 41
A = B = then A EQV BTrue True TrueTrue False FalseFalse True FalseFalse False True
Data and Operators are combined to form expressions and equations
To evaluate these in the proper order, a hierarchy of operations, or Order of Precedence, is used.◦ Note: Whenever you want to clarify an equation,
use the parentheses!
McManusCOP1006 42
McManusCOP1006 43
OPERATOR TYPE NAME ( ) Parentheses Parentheses ^ Arithmetic Exponent (Powers) * / Arithmetic Multiplcation, floating-point division
\, Mod Arithmetic Integer division, Modulus + - Arithmetic Addition, subtraction
= <> <= >=
> <
Comparison (all have same level of precedence
Equal to, not equal to, less than or equal to, greater than or equal to, greater than, less than
NOT Logical Logical Negation AND Logical Logical And OR Logical Logical Or
XOR Logical Logical Exclusive Or EQV Logical Logical Equivalent
ORDER OF PRECEDENCE
Evaluate the following: A = True, B = False, C = True, D = False
A or B or C and D and A and B and (not C) or (not D) To do this, we create what’s called an
Evaluation Tree…
McManusCOP1006 44
McManusCOP1006 45
A or B or C and D and A and B and (not C) or (not D)
False True
False False False FalseTrue
True True
Expressions◦ Process the data and the operands, through the
use of the operators◦ Does not store the resultant (answer)◦ Examples
Price * SalesTaxRate(Hours – 40) * Wage * 1.5
Length * Width
McManusCOP1006 46
Same as an expression, except the equation stores the resultant (answer) in a memory location
“takes on the value of” not equals◦ Examples
SalesTax = Price * SalesTaxRateOvertimePay = (Hours – 40) * Wage * 1.5
Area = Length * Width◦ SalesTax is the memory location where the
answer will be stored Often called Assignment Statements
McManusCOP1006 47
Destructive Read-In◦The value stored
in the variable on the left-hand side of the equals sign is replaced by the result of the expression on the right-hand side.
Non-Destructive Read-In◦Values used on the
right-hand side are protected from being changed.
McManusCOP1006 48
SalesTax = Price * SalesTaxRate
D R-I ND R-I ND R-I
Algebraic Expressions cannot be represented in the computer.
They must be converted to a form the computer will recognize.
Thus, Straight Line Form
McManusCOP1006 49
2
2
)(
)(
)(
YX
YX
YXY
= (X – Y) / ( (X + Y)^2 / (X – Y )^2 )
straight line form
McManusCOP1006 50