Equality and relational operators in perl. If statement compares values of $number1 and $number2. Equality operator!= tests whether $number1, $number2, or $number3 are equal.
Equality and relational operators in perl. If statement compares values of $number1 and $number2. Equality operator!= tests whether $number1, $number2, or $number3 are equal.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online from Scribd
Equality and relational operators in perl. If statement compares values of $number1 and $number2. Equality operator!= tests whether $number1, $number2, or $number3 are equal.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online from Scribd
equality operator or equality of Perl Perl condition relational operator or condition relational operator Relational operators > > $x > $y $x is greater than $y < < $x < $y $x is less than $y > >= $x >= $y $x is greater than or equal to $y < <= $x <= $y $x is less than or equal to $y Equality operators = == $x == $y $x is equal to $y = != $x != $y $x is not equal to $y Fig. 2.16 Equality and relational operators.
2001 Prentice Hall, Inc. All rights reserved.
2 1 2 #!/usr/bin/perl # Fig. 2.17: fig02_17.pl Outline 3 # Using if statements with relational and equality operators 4 5 print "Please enter first integer: "; 6 $number1 = <STDIN>; 7 chomp $number1; 8 The if Prompt the user structure for two compares thenumbers, values of 9 print "Please enter second integer: "; read in the numbers, and remove variable $number1 and variable $number2 the 10 $number2 = <STDIN>; to testnewline characters with chomp. for equality. 11 chomp $number2; 12 13 print "The integers satisfy these relationships:\n"; 14 15 if ( $number1 == $number2 ) { 16 print "$number1 is equal to $number2.\n"; The relational operator < tests whether 17 } $number1 is less than $number2. 18 The body of the if structure, enclosed 19 if ( $number1 != $number2 ) { by a pair of braces ({}), executes if the 20 print "$number1 is not equal to $number2.\n"; condition evaluates to true. 21 } 22 The relational operator > tests 23 if ( $number1 < $number2 ) { The equality whether operator $number1 != teststhan is greater whether 24 print "$number1 is less than $number2.\n"; $number1 and $number2 are not $number2. 25 } equal. 26 27 if ( $number1 > $number2 ) { 28 print "$number1 is greater than $number2.\n"; 29 } 30 2001 Prentice Hall, Inc. All rights reserved. 3 31 if ( $number1 <= $number2 ) { 32 print "$number1 is less than or equal to $number2.\n"; Outline 33 } 34 35 if ( $number1 >= $number2 ) { 36 print "$number1 is greater than or equal to $number2.\n"; 37 }
The relational operator <= tests
Please enter first integer: 3 whether $number1 is less than Please enter second integer: 7 or equal to $number2. The integers satisfy these relationships: The relational operator >= tests 3 is not equal to 7. 3 is less than 7. whether $number1 is greater than 3 is less than or equal to 7. or equal to $number2.
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.
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 5 2 # Fig. 2.19: fig02_19.pl Outline 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 operatoroperator 8 evaluates evaluates stringsininstring $number 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"; 11 interpreted as numeric, the 12 $concatenate = $number . $string; # '10Top 10' string evaluates to 0. 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"; 18 When using a string in 19 $undefAdd = 10 + $undefNumber; numeric context, Perl stops 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. $undefVariable(end)\n"; When When an an undefined undefined variable value is found in is interpreted in Number is 10.0 and string is 'Top 10' numeric string context, context, it evaluates it evaluates to an to 0. empty string (“”). 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: 10
Printing an undefined variable: (end) 2001 Prentice Hall, Inc. All rights reserved.