0% found this document useful (0 votes)
3 views13 pages

Perl Modulus and Arithmetic Operators

This document discusses a simple Perl program that adds two numbers. It begins with a shebang line indicating the Perl interpreter. The program prompts the user to input two numbers, stores the input in variables, removes the newline characters, calculates the sum, and prints the result. It demonstrates taking user input, performing arithmetic operations, and outputting values in Perl.

Uploaded by

api-3725802
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Perl Modulus and Arithmetic Operators

This document discusses a simple Perl program that adds two numbers. It begins with a shebang line indicating the Perl interpreter. The program prompts the user to input two numbers, stores the input in variables, removes the newline characters, calculates the sum, and prints the result. It demonstrates taking user input, performing arithmetic operations, and outputting values in Perl.

Uploaded by

api-3725802
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

1

1 #!/usr/bin/perl Outline
2 # Fig. 2.1: fig02_01.pl

3 # A first program in Perl

5 print "Welcome to Perl!\n"; # print greeting

The “shebang”
The Perl comment construct
character (#!) indicates
(#) indicates that everything
The escape the path
on thesequence
current to the Perl the
represents
\n following
line interpreter
newline onpositions
# is a and Unix
comment.
theFunction and
print
screen cursor Linux
outputs
at the systems.
the string
beginning “Welcome
of the next line. to
Welcome to Perl!
Perl!\n” to the screen.

 2001 Prentice Hall, Inc. All rights reserved.


2

Escape Description
Sequence
\n Newline. Position the screen cursor
at the beginning of the next line.
\t Horizontal tab. Move the screen
cursor to the next tab stop.
\r Carriage return. Position the screen
cursor at the beginning of the
current line; do not advance to the
next line.
\$ Dollar sign. Used to insert a
dollar-sign character in a string.
\\ Backslash. Used to insert a
backslash character in a string.
\" Double quote. Inserts a double-quote
character in a double-quoted string.
\' Single quote. Inserts a single-quote
character in a single-quoted string.
Fig. 2.2 Some common escape sequences.

 2001 Prentice Hall, Inc. All rights reserved.


3
1 #!/usr/bin/perl
Outline
2 # Fig. 2.3: fig02_03.pl
3 # Prints a welcome statement in a variety of ways
4
5 print ( "1. Welcome to Perl!\n" );
6 print "2. Welcome to Perl!\n" ;
7 print "3. Welcome ", "to ", "Perl!\n";
8 print "4. Welcome "; Although these statements are on
9 print "to Perl!\n"; separate lines, the string Welcome
10 print "5. Welcome to Perl!\n"; Perl! islike
to functions
Arguments to built-in displayed
print on one line.
11 print "6. Welcome\n Whitespace characters
can be placed
to\n\n are ignored
in parentheses.
Perl!\n"; by Perl.

1. Welcome to Perl! More than one argument may be passed to


2. Welcome to Perl!
3. Welcome to Perl!
functioncharacters
Newline print. may be used to print multiple lines
4. Welcome to Perl! of text using one line of code.
5. Welcome to Perl!
6. Welcome
to

Perl!

 2001 Prentice Hall, Inc. All rights reserved.


4
1 #!/usr/bin/perl Outline
2 # Fig. 2.4: fig02_04.pl
3 # A simple addition program
4
5 # prompt user for first number
6 print "Please enter first number:\n";
7 $number1 = <STDIN>;
8 chomp $number1; # remove "\n" from input
9
Prompt to tell the user to enter the
10 print "Please enter second number:\n";
first number.
11 $number2 = <STDIN>;
12 chomp $number2; # remove "\n" from input
13
14 $sum = $number1 + $number2; The expression
Declare
Assignment <STDIN>
scalar variable
operator causes
=. program
$number1.
Function
execution to pause while the computercharacter
chomp removes the newline waits for
15
16 print "The sum is $sum.\n";
from the end of the line.
the user to enter data.

The assignment statement calculates the sum of the


Please enter first number:
45 variables $number1 and $number2 and assigns the
Please enter second number: result to variable $sum.
72
The sum is 117.

 2001 Prentice Hall, Inc. All rights reserved.


5

$number1 45\n

$number1 45

 2001 Prentice Hall, Inc. All rights reserved.


6

$number1 45

$number2 72

 2001 Prentice Hall, Inc. All rights reserved.


7

$number1 45

$number2 72

$sum 117

 2001 Prentice Hall, Inc. All rights reserved.


8

Operation Arithmet Algebraic Perl


ic expression expression
operator
Addition + x + y $x + $y
Subtraction - x Ð y $x - $y
Multiplication * xy $x * $y
Division / x / y $x / $y

Modulus % x mod y $x % $y
Exponentiation ** x y $x ** $y
Fig. 2.9 Arithmetic operators.

 2001 Prentice Hall, Inc. All rights reserved.


9

Operator(s) Operation(s) Order of evaluation (precedence)

( ) Parentheses
Evaluated first. If the parentheses
are nested, the expression in the
innermost pair is evaluated first. If
there are several pairs of parentheses
Òon the same levelÓ (i.e., not
nested), they are evaluated left to
right.
** Exponentiation Evaluated second. If there is more
than one, the operators are evaluated
from right to left.
*, / or % Multiplication Evaluated third. If there is more than
Division one, the operators are evaluated from
Modulus left to right.
+ or - Addition Evaluated last. If there is more than
Subtraction one, the operators are evaluated from
left to right.
Fig. 2.10 Precedence of arithmetic operators.

 2001 Prentice Hall, Inc. All rights reserved.


10

Assignment operator Sample Explanation Assigns


expression
Assume that: $c = 3, $d = 5, $e
= 4, $f = 6, $g = 12 and $h = 5.
+= $c += 7 $c = $c + 7 10 to $c
-= $d -= 4 $d = $d - 4 1 to $d
*= $e *= 5 $e = $e * 5 20 to $e
/= $f /= 3 $f = $f / 3 2 to $f
%= $g %= 9 $g = $g % 9 3 to $g
**= $h **= 2 $h = $h ** 25 to $h
2
Fig. 2.12 Arithmetic assignment operators.

 2001 Prentice Hall, Inc. All rights reserved.


11

Operat Name Sample Explanation


or expression
++ preincrement ++$c Increment $c by 1, then use the
new value of $c in the expression
in which $c resides.
++ postincrement $c++ Use the current value of $c in
the expression in which $c
resides, then increment $c by 1.
-- predecrement --$d Decrement $d by 1, then use the
new value of $d in the expression
in which $d resides.
-- postdecrement $d-- Use the current value of $d in
the expression in which $d
resides, then decrement $d by 1.
Fig. 2.13 Increment and decrement operators.

 2001 Prentice Hall, Inc. All rights reserved.


12
1 #!/usr/bin/perl Outline
2 # Fig. 2.14: fig02_14.pl
3 # Demonstrates the difference between pre- and postincrement
4
5 $c = 5; Print
Use
Print
thethenew
variable
postincrement
value$c.
of variable
operator
$[Link] use the current value
6 $d = 5; of $c in the expression and then increment $c by 1.
7
8 print $c, " "; # print 5
9 print $c++, " "; # print 5 then postincrement
10 print $c, "\n"; # print 6
11
12 print $d, " "; # print 5
13 print ++$d, " "; # preincrement then print 6
14 print $d, "\n"; # print 6

5 5 6
5 6 6 Use the preincrement operator to increment $c by 1 and
then use the new value of $c in the expression.

 2001 Prentice Hall, Inc. All rights reserved.


13

Operator(s) Associativity Called

( ) left to right parentheses


++ or -- none increment and decrement
** right to left exponentiation
*, / or % left to right multiplicative
+ or - left to right additive
=, +=, -=, *=, right to left assignment
/=, %= or **=
Fig. 2.15 Precedence and associativity of operators
discussed so far.

 2001 Prentice Hall, Inc. All rights reserved.

You might also like