410 likes | 511 Views
Chapter 4 – Operators. Wayne Machuca Mt Hood Community College. Overview. The Assignment Operator String Operator Comparison Operators Logical Operators. The Assignment Operator. In most computer languages, variables are assigned in a process called assignment Variable = Expression
E N D
Chapter 4 – Operators Wayne Machuca Mt Hood Community College CS133PRL - Wayne Machuca - www.mhcc.edu
Overview • The Assignment Operator • String Operator\ • Comparison Operators • Logical Operators CS133PRL - Wayne Machuca - www.mhcc.edu
The Assignment Operator • In most computer languages, variables are assigned in a process called assignment • Variable = Expression • Example $firstName = “Herman”; • The value “Herman” is assigned to a space in memory identified as $firstName CS133PRL - Wayne Machuca - www.mhcc.edu
04_01.pl CS133PRL - Wayne Machuca - www.mhcc.edu
Multiple Assignment • Perl has an interesting capability of assigning a single value to multiple variables at the same time. • It is different in that each variable is identified and valued at the same time • The assignment still goes right to left CS133PRL - Wayne Machuca - www.mhcc.edu
04-02.pl CS133PRL - Wayne Machuca - www.mhcc.edu
Arithmetic Operators • Perl has a familiar list of operators • + addition • - subtraction • * multiplication • / division • % modulus division • ** exponentation CS133PRL - Wayne Machuca - www.mhcc.edu
Modulus % • Remember long division? Modulus is used in a division to return the remainder. 33 / 7 = 4 remainder 5 • Example $remainder = 33 % 7; $remainder will equal 5 CS133PRL - Wayne Machuca - www.mhcc.edu
Exponentiation • Exponentiation means to raise a value to the power of • You have seen 53 = 125 • In Perl you write $exponent = 5 ** 3; $exponent will equal 125 CS133PRL - Wayne Machuca - www.mhcc.edu
04-03.pl • Always document your programs the same way CS133PRL - Wayne Machuca - www.mhcc.edu
04-03.pl • Identify distinct sections of code CS133PRL - Wayne Machuca - www.mhcc.edu
04-03.pl • Document what the code is doing CS133PRL - Wayne Machuca - www.mhcc.edu
Other Operators • Perl supports incrementer (increase) and decrementer (decrease) operators • Makes the code look more efficient • Effective in working with increasing and decreasing counters CS133PRL - Wayne Machuca - www.mhcc.edu
Other Operators • Examples$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; CS133PRL - Wayne Machuca - www.mhcc.edu
You are going to love this • You can place the incrementer either before or after the variable name • You will get different results++$cnt;is not the same as$cnt++; CS133PRL - Wayne Machuca - www.mhcc.edu
Watch CS133PRL - Wayne Machuca - www.mhcc.edu
Basic Looping • We will use the while statement to discuss looping from the context of using a counter. • We will cover looping structures in depth later. CS133PRL - Wayne Machuca - www.mhcc.edu
Looping with a counter CS133PRL - Wayne Machuca - www.mhcc.edu
String Operators • The Concatenator Operator • Use the dot (.) to join strings CS133PRL - Wayne Machuca - www.mhcc.edu
Variable Interpolation • “If a variable is contained in a string enclosed in double quotes, the variable is replaced by the value of the variable.” Perl will try to resolve the variable$somethingable CS133PRL - Wayne Machuca - www.mhcc.edu
Bad Fix • Add a space to identify $comfort, but… CS133PRL - Wayne Machuca - www.mhcc.edu
Interesting Perl Fix Include Curly Braces CS133PRL - Wayne Machuca - www.mhcc.edu
Substrings • Perl allows you to extract parts of a stringsubstr(stringb, startpos, length) CS133PRL - Wayne Machuca - www.mhcc.edu
Substring CS133PRL - Wayne Machuca - www.mhcc.edu
Changing Case • You have several commands to allow you to change the case of a string • uc – Convert to upper case • lc -- Convert to lower case • ucfirst – Convert the first char to upper • lcfirst – Convert the first char to lower CS133PRL - Wayne Machuca - www.mhcc.edu
Changing case – Something strange CS133PRL - Wayne Machuca - www.mhcc.edu
Combining Strings and Number • Rule #1: Don’tRule #2: But if you do, be careful CS133PRL - Wayne Machuca - www.mhcc.edu
This is strange but it works CS133PRL - Wayne Machuca - www.mhcc.edu
This fails CS133PRL - Wayne Machuca - www.mhcc.edu
Use the dot to concatenate CS133PRL - Wayne Machuca - www.mhcc.edu
Comparison Operators • To test numeric scalar values, use the following comparison operators • > (greater than) • = = (equal to) • < (less than) • >= (greater than or equal to) • <= (less than or equal to) • != (not equal to) CS133PRL - Wayne Machuca - www.mhcc.edu
Watch the comparison on a simple conditional test CS133PRL - Wayne Machuca - www.mhcc.edu
String Comparisons are Different Wrong CS133PRL - Wayne Machuca - www.mhcc.edu
This is better CS133PRL - Wayne Machuca - www.mhcc.edu
String comparison operators • Use these for strings • eq • ne • lt • gt • le • ge CS133PRL - Wayne Machuca - www.mhcc.edu
Logical Operators • Use to evaluate whether a condition is true or false • Testing the Boolean state of a condition CS133PRL - Wayne Machuca - www.mhcc.edu
Let’s try one… CS133PRL - Wayne Machuca - www.mhcc.edu
Done CS133PRL - Wayne Machuca - www.mhcc.edu
Lab 3 • Add to Lab 3 the following: • If the customer did not order any quantity of an item, do not print that item on the invoice • Keep the item number accurate for the individual order (3 items should be listed as 1,2 and 3) CS133PRL - Wayne Machuca - www.mhcc.edu
Lab 3 • Charge sales tax based on the customer’s address. • OR = 0%, CA = 7.25%, WA = 8% • Add salesTax and grandTotal amounts to the invoice • Display a message at the end of the program identifying the item that sold the greatest poundage. • “Product (name) sold the most with (number) of pounds” CS133PRL - Wayne Machuca - www.mhcc.edu
Lab 3 • State names must be all caps. CS133PRL - Wayne Machuca - www.mhcc.edu