Date post: | 31-Dec-2015 |
Category: |
Documents |
Upload: | marny-hogan |
View: | 37 times |
Download: | 1 times |
04/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu 11
Chapter 4 – Operators
Wayne MachucaMt Hood Community
College
2204/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
OverviewOverview
The Assignment OperatorThe Assignment Operator
String Operator\String Operator\
Comparison OperatorsComparison Operators
Logical OperatorsLogical Operators
3304/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
The Assignment OperatorThe Assignment Operator
In most computer languages, variables are In most computer languages, variables are assigned in a process called assigned in a process called assignmentassignment
Variable = ExpressionVariable = Expression
ExampleExample$firstName = “Herman”;$firstName = “Herman”;
The value “Herman” is assigned to a The value “Herman” is assigned to a space in memory identified as $firstNamespace in memory identified as $firstName
4404/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
04_01.pl04_01.pl
5504/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Multiple AssignmentMultiple Assignment
Perl has an interesting capability of Perl has an interesting capability of assigning a single value to multiple assigning a single value to multiple variables at the same time.variables at the same time.
It is different in that each variable is It is different in that each variable is identified and valued at the same timeidentified and valued at the same time
The assignment still goes right to leftThe assignment still goes right to left
6604/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
04-02.pl04-02.pl
7704/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Arithmetic OperatorsArithmetic Operators
Perl has a familiar list of operatorsPerl has a familiar list of operators + addition+ addition - subtraction- subtraction * multiplication* multiplication / division/ division % modulus division% modulus division ** exponentation** exponentation
8804/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Modulus %Modulus %
Remember long division? Remember long division? Modulus is used in a division to return the Modulus is used in a division to return the remainder.remainder.
33 / 7 = 4 remainder 533 / 7 = 4 remainder 5
ExampleExample $remainder = 33 % 7; $remainder = 33 % 7; $remainder will equal 5 $remainder will equal 5
9904/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
ExponentiationExponentiation
Exponentiation means to raise a value to Exponentiation means to raise a value to the power ofthe power of
You have seen 5You have seen 533 = 125 = 125
In Perl you writeIn Perl you write$exponent = 5 ** 3;$exponent = 5 ** 3;
$exponent will equal 125 $exponent will equal 125
101004/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
04-03.pl04-03.pl
Always document your programs the same Always document your programs the same wayway
111104/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
04-03.pl04-03.pl
Identify distinct sections of codeIdentify distinct sections of code
121204/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
04-03.pl04-03.pl
Document what the code is doingDocument what the code is doing
131304/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Other OperatorsOther Operators
Perl supports incrementer (increase) and Perl supports incrementer (increase) and decrementer (decrease) operatorsdecrementer (decrease) operators
Makes the code look more efficientMakes the code look more efficient
Effective in working with increasing and Effective in working with increasing and decreasing countersdecreasing counters
141404/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Other OperatorsOther Operators
ExamplesExamples$cnt++; is the same as $cnt = $cnt + 1;$cnt++; is the same as $cnt = $cnt + 1;$cnt--; is the same as $cnt = $cnt – 1;$cnt--; is the same as $cnt = $cnt – 1;
$cnt+=5; is the same as $cnt = $cnt + 5;$cnt+=5; is the same as $cnt = $cnt + 5;$cnt-=5; is the same as $cnt = $cnt - 5;$cnt-=5; is the same as $cnt = $cnt - 5;$cnt%=5 is the same as $cnt = $cnt % 5;$cnt%=5 is the same as $cnt = $cnt % 5;$cnt*=5 is the same as $cnt = $cnt * 5;$cnt*=5 is the same as $cnt = $cnt * 5;$cnt/=5 is the same as $cnt = $cnt / 5;$cnt/=5 is the same as $cnt = $cnt / 5;
151504/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
You are going to love thisYou are going to love this
You can place the incrementer either You can place the incrementer either before or after the variable namebefore or after the variable name
You will get different resultsYou will get different results
++$cnt;++$cnt;is not the same asis not the same as$cnt++;$cnt++;
161604/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
WatchWatch
171704/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Basic LoopingBasic Looping
We will use the while statement to discuss We will use the while statement to discuss looping from the context of using a looping from the context of using a counter.counter.
We will cover looping structures in depth We will cover looping structures in depth later.later.
181804/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Looping with a counterLooping with a counter
191904/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
String OperatorsString Operators
The Concatenator OperatorThe Concatenator Operator Use the dot (.) to join stringsUse the dot (.) to join strings
202004/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Variable InterpolationVariable Interpolation
““If a variable is contained in a string If a variable is contained in a string enclosed in double quotes, the variable is enclosed in double quotes, the variable is replaced by the value of the variable.”replaced by the value of the variable.”
Perl will try to resolve the variable$somethingable
212104/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Bad FixBad Fix
Add a space to identify $comfort, but…Add a space to identify $comfort, but…
222204/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Interesting Perl FixInteresting Perl FixInclude Curly Braces
232304/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
SubstringsSubstrings
Perl allows you to extract parts of a stringPerl allows you to extract parts of a string
substr(stringb, startpos, length)substr(stringb, startpos, length)
242404/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
SubstringSubstring
252504/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Changing CaseChanging Case
You have several commands to allow you You have several commands to allow you to change the case of a stringto change the case of a string uc – Convert to upper caseuc – Convert to upper case lc -- Convert to lower caselc -- Convert to lower case ucfirst – Convert the first char to upperucfirst – Convert the first char to upper lcfirst – Convert the first char to lowerlcfirst – Convert the first char to lower
262604/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Changing case – Something strangeChanging case – Something strange
272704/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Combining Strings and NumberCombining Strings and Number
Rule #1: Don’tRule #1: Don’tRule #2: But if you do, be carefulRule #2: But if you do, be careful
282804/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
This is strange but it worksThis is strange but it works
292904/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
This failsThis fails
303004/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Use the dot to concatenateUse the dot to concatenate
313104/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Comparison OperatorsComparison Operators
To test numeric scalar values, use the To test numeric scalar values, use the following comparison operatorsfollowing comparison operators > (greater than)> (greater than) = = (equal to)= = (equal to) < (less than)< (less than) >= (greater than or equal to)>= (greater than or equal to) <= (less than or equal to)<= (less than or equal to) != (not equal to)!= (not equal to)
323204/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Watch the comparison on a simple Watch the comparison on a simple conditional testconditional test
333304/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
String Comparisons are DifferentString Comparisons are Different
Wrong
343404/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
This is betterThis is better
353504/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
String comparison operatorsString comparison operators
Use these for stringsUse these for strings eq eq ne ne lt lt gt gt le le ge ge
363604/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Logical OperatorsLogical Operators
Use to evaluate whether a condition is true Use to evaluate whether a condition is true or falseor false
Testing the Boolean state of a conditionTesting the Boolean state of a condition
373704/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Let’s try one…Let’s try one…
04/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu 3838
DoneDone
393904/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Lab 3 Lab 3
Add to Lab 3 the following:Add to Lab 3 the following:
If the customer did not order any quantity If the customer did not order any quantity of an item, do not print that item on the of an item, do not print that item on the invoiceinvoice
Keep the item number accurate for the Keep the item number accurate for the individual order (3 items should be listed individual order (3 items should be listed as 1,2 and 3)as 1,2 and 3)
404004/19/2304/19/23 CS133PRL - Wayne Machuca - www.mhcc.eduCS133PRL - Wayne Machuca - www.mhcc.edu
Lab 3Lab 3
Charge sales tax based on the customer’s Charge sales tax based on the customer’s address.address. OR = 0%, CA = 7.25%, WA = 8%OR = 0%, CA = 7.25%, WA = 8%
Add salesTax and grandTotal amounts to the Add salesTax and grandTotal amounts to the invoiceinvoice
Display a message at the end of the program Display a message at the end of the program identifying the item that sold the greatest identifying the item that sold the greatest poundage.poundage. ““Product (name) sold the most with (number) of Product (name) sold the most with (number) of
pounds”pounds”