1 / 41

Chapter 4 – Operators

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

marny-hogan
Download Presentation

Chapter 4 – Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 – Operators Wayne Machuca Mt Hood Community College CS133PRL - Wayne Machuca - www.mhcc.edu

  2. Overview • The Assignment Operator • String Operator\ • Comparison Operators • Logical Operators CS133PRL - Wayne Machuca - www.mhcc.edu

  3. 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

  4. 04_01.pl CS133PRL - Wayne Machuca - www.mhcc.edu

  5. 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

  6. 04-02.pl CS133PRL - Wayne Machuca - www.mhcc.edu

  7. Arithmetic Operators • Perl has a familiar list of operators • + addition • - subtraction • * multiplication • / division • % modulus division • ** exponentation CS133PRL - Wayne Machuca - www.mhcc.edu

  8. 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

  9. 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

  10. 04-03.pl • Always document your programs the same way CS133PRL - Wayne Machuca - www.mhcc.edu

  11. 04-03.pl • Identify distinct sections of code CS133PRL - Wayne Machuca - www.mhcc.edu

  12. 04-03.pl • Document what the code is doing CS133PRL - Wayne Machuca - www.mhcc.edu

  13. 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

  14. 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

  15. 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

  16. Watch CS133PRL - Wayne Machuca - www.mhcc.edu

  17. 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

  18. Looping with a counter CS133PRL - Wayne Machuca - www.mhcc.edu

  19. String Operators • The Concatenator Operator • Use the dot (.) to join strings CS133PRL - Wayne Machuca - www.mhcc.edu

  20. 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

  21. Bad Fix • Add a space to identify $comfort, but… CS133PRL - Wayne Machuca - www.mhcc.edu

  22. Interesting Perl Fix Include Curly Braces CS133PRL - Wayne Machuca - www.mhcc.edu

  23. Substrings • Perl allows you to extract parts of a stringsubstr(stringb, startpos, length) CS133PRL - Wayne Machuca - www.mhcc.edu

  24. Substring CS133PRL - Wayne Machuca - www.mhcc.edu

  25. 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

  26. Changing case – Something strange CS133PRL - Wayne Machuca - www.mhcc.edu

  27. Combining Strings and Number • Rule #1: Don’tRule #2: But if you do, be careful CS133PRL - Wayne Machuca - www.mhcc.edu

  28. This is strange but it works CS133PRL - Wayne Machuca - www.mhcc.edu

  29. This fails CS133PRL - Wayne Machuca - www.mhcc.edu

  30. Use the dot to concatenate CS133PRL - Wayne Machuca - www.mhcc.edu

  31. 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

  32. Watch the comparison on a simple conditional test CS133PRL - Wayne Machuca - www.mhcc.edu

  33. String Comparisons are Different Wrong CS133PRL - Wayne Machuca - www.mhcc.edu

  34. This is better CS133PRL - Wayne Machuca - www.mhcc.edu

  35. String comparison operators • Use these for strings • eq • ne • lt • gt • le • ge CS133PRL - Wayne Machuca - www.mhcc.edu

  36. 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

  37. Let’s try one… CS133PRL - Wayne Machuca - www.mhcc.edu

  38. Done CS133PRL - Wayne Machuca - www.mhcc.edu

  39. 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

  40. 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

  41. Lab 3 • State names must be all caps. CS133PRL - Wayne Machuca - www.mhcc.edu

More Related