1
Standard algebraic equality operator or relational operator
Perl Example equality of Perl or condition relational operator
Meaning of Perl condition
>
>
$x > $y
< >
< >=
$x < $y $x >= $y
<
<=
$x <= $y
$x is greater than $y $x is less than $y $x is greater than or equal to $y $x is less than or equal to $y
== !=
$x == $y $x != $y
Relational operators
Equality operators = =
Fig. 2.16 Equality and relational operators.
2001 Prentice Hall, Inc. All rights reserved.
$x is equal to $y $x is not equal to $y
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#!/usr/bin/perl # Fig. 2.17: fig02_17.pl # Using if statements with relational and equality operators print "Please enter first integer: "; $number1 = <STDIN>; chomp $number1; print "Please enter second integer: "; $number2 = <STDIN>; chomp $number2;
Outline
Prompt the user for two The if structure compares thenumbers, values of read in the numbers, and remove the variable $number1 and variable $number2 characters with chomp. to testnewline for equality.
print "The integers satisfy these relationships:\n"; if ( $number1 == $number2 ) { print "$number1 is equal to $number2.\n"; } if ( $number1 != $number2 ) { print "$number1 is not equal to $number2.\n"; } if ( $number1 < $number2 ) { print "$number1 is less than $number2.\n"; } if ( $number1 > $number2 ) { print "$number1 is greater than $number2.\n"; }
2001 Prentice Hall, Inc. All rights reserved.
The relational operator < tests whether $number1 is less than $number2. The body of the if structure, enclosed by a pair of braces ({}), executes if the condition evaluates to true. The relational operator > tests The equality operator != teststhan whether whether $number1 is greater $number1 and $number2 are not $number2. equal.
2
31 if ( $number1 <= $number2 ) { 32 print "$number1 is less than or equal to $number2.\n"; 33 } 34 35 if ( $number1 >= $number2 ) { 36 print "$number1 is greater than or equal to $number2.\n"; 37 }
Please enter first integer: 3 Please enter second integer: 7 The integers satisfy these relationships: 3 is not equal to 7. 3 is less than 7. 3 is less than or equal to 7. Please enter first integer: 22 Please enter second integer: 12 The integers satisfy these relationships: 22 is not equal to 12. 22 is greater than 12. 22 is greater than or equal to 12. Please enter first integer: 7 Please enter second integer: 7 The integers satisfy these relationships: 7 is equal to 7. 7 is less than or equal to 7. 7 is greater than or equal to 7.
2001 Prentice Hall, Inc. All rights reserved.
The relational operator <= tests whether $number1 is less than or equal to $number2. The relational operator >= tests whether $number1 is greater than or equal to $number2.
Outline
3
4
Operators
Associativity Type
()
left to right parentheses
++
--
none
** *
/
+ <
<= >
%
>=
increment and decrement right to left exponential left to right multiplicativ e left to right additive none relational
== != none equality = += -= *= /= %= **= right to left assignment Fig. 2.18 Precedence and associativity of operators discussed so far.
2001 Prentice Hall, Inc. All rights reserved.
1 #!/usr/bin/perl 2 # Fig. 2.19: fig02_19.pl 3 # Program to illustrate numeric and string context, and undef 4 5 $string = "Top 10"; 6 $number = 10.0; 7 print "Number is 10.0 and string is 'Top 10'\n\n"; The concatenation The binary addition operator operator 8 evaluates evaluates $number stringsininstring numeric 9 $add = $number + $string; # 10 (not 20) context, context. in this If case nothing “10”. can be 10 print "Adding a number and a string: $add\n"; interpreted as numeric, the 11 string evaluates to 0. 12 $concatenate = $number . $string; # '10Top 10' 13 # (not '10.0Top 10') 14 print "Concatenating a number and a string: $concatenate\n"; 15 16 $add2 = $concatenate + $add; # 20 (not 30, not 1020) 17 print "Adding the previous two results: $add2\n\n"; When using a string in 18 numeric context, Perl stops 19 $undefAdd = 10 + $undefNumber; 20 print "Adding 10 to an undefined variable: $undefAdd\n"; conversion at the first 21 character that cannot be used 22 print "Printing an undefined variable: in numeric context. When undefined variable is found in in $undefVariable(end)\n"; When an an undefined value is interpreted
Outline
Number is 10.0 and string is 'Top 10' Adding a number and a string: 10 Concatenating a number and a string: 10Top 10 Adding the previous two results: 20 Adding 10 to an undefined variable: Printing an undefined variable: 2001 Prentice Hall, Inc. All rights reserved.
10 (end)
numeric context, it evaluates 0. string context, it evaluates to an to empty string (“”).
5