Chapter 4 - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

Chapter 4

Description:

Chapter 4 Operators Wayne Machuca Mt Hood Community College – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 42
Provided by: MHC82
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4


1
Chapter 4 Operators
  • Wayne Machuca
  • Mt Hood Community College

2
Overview
  • The Assignment Operator
  • String Operator\
  • Comparison Operators
  • Logical Operators

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

4
04_01.pl
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

6
04-02.pl
7
Arithmetic Operators
  • Perl has a familiar list of operators
  • addition
  • - subtraction
  • multiplication
  • / division
  • modulus division
  • exponentation

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

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

10
04-03.pl
  • Always document your programs the same way

11
04-03.pl
  • Identify distinct sections of code

12
04-03.pl
  • Document what the code is doing

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

14
Other Operators
  • Examplescnt is the same as cnt cnt
    1cnt-- is the same as cnt cnt
    1cnt5 is the same as cnt cnt
    5cnt-5 is the same as cnt cnt -
    5cnt5 is the same as cnt cnt
    5cnt5 is the same as cnt cnt
    5cnt/5 is the same as cnt cnt / 5

15
You are going to love this
  • You can place the incrementer either before or
    after the variable name
  • You will get different resultscntis not
    the same ascnt

16
Watch
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.

18
Looping with a counter
19
String Operators
  • The Concatenator Operator
  • Use the dot (.) to join strings

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 variablesomethingab
le
21
Bad Fix
  • Add a space to identify comfort, but

22
Interesting Perl Fix
Include Curly Braces
23
Substrings
  • Perl allows you to extract parts of a
    stringsubstr(stringb, startpos, length)

24
Substring
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

26
Changing case Something strange
27
Combining Strings and Number
  • Rule 1 DontRule 2 But if you do, be careful

28
This is strange but it works
29
This fails
30
Use the dot to concatenate
31
Comparison Operators
  • To test numeric scalar values, use the following
    comparison operators
  • gt (greater than)
  • (equal to)
  • lt (less than)
  • gt (greater than or equal to)
  • lt (less than or equal to)
  • ! (not equal to)

32
Watch the comparison on a simple conditional test
33
String Comparisons are Different
Wrong
34
This is better
35
String comparison operators
  • Use these for strings
  • eq
  • ne
  • lt
  • gt
  • le
  • ge

36
Logical Operators
  • Use to evaluate whether a condition is true or
    false
  • Testing the Boolean state of a condition

37
Lets try one
38
Done
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)

40
Lab 3
  • Charge sales tax based on the customers 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

41
Lab 3
  • State names must be all caps.
Write a Comment
User Comments (0)
About PowerShow.com