Show Example 1
优质
小牛编辑
127浏览
2023-12-01
假设变量$ a保持10,变量$ b保持20,然后是Perl算术运算符 -
Sr.No. | 操作符和说明 |
---|---|
1 | + ( Addition ) 在运算符的任一侧添加值 Example - $ a + $ b将给出30 |
2 | - (Subtraction) 从左手操作数中减去右手操作数 Example - $ a - $ b将给出-10 |
3 | * (Multiplication) 将运算符两侧的值相乘 Example - $ a * $ b将给出200 |
4 | / (Division) 用左手操作数除左手操作数 Example - $ b/$ a将给出2 |
5 | % (Modulus) 用左手操作数除左手操作数并返回余数 Example - $ b%$ a将给出0 |
6 | ** (Exponent) 对运算符执行指数(幂)计算 Example - $ a ** $ b将给出功率20的10 |
例子 (Example)
尝试以下示例以了解Perl中可用的所有算术运算符。 将以下Perl程序复制并粘贴到test.pl文件中并执行该程序。
#!/usr/local/bin/perl
$a = 21;
$b = 10;
print "Value of \$a = $a and value of \$b = $b\n";
$c = $a + $b;
print 'Value of $a + $b = ' . $c . "\n";
$c = $a - $b;
print 'Value of $a - $b = ' . $c . "\n";
$c = $a * $b;
print 'Value of $a * $b = ' . $c . "\n";
$c = $a/$b;
print 'Value of $a/$b = ' . $c . "\n";
$c = $a % $b;
print 'Value of $a % $b = ' . $c. "\n";
$a = 2;
$b = 4;
$c = $a ** $b;
print 'Value of $a ** $b = ' . $c . "\n";
执行上述代码时,会产生以下结果 -
Value of $a = 21 and value of $b = 10
Value of $a + $b = 31
Value of $a - $b = 11
Value of $a * $b = 210
Value of $a/$b = 2.1
Value of $a % $b = 1
Value of $a ** $b = 16