- Perl ›
- Operators ›
- Arithmetic operator ›
- here
Decrement operator " - "
Using the decrement operator , assigned to variable You can subtract 1 from the number. Decrement operators can be added before or after a variable.
# Decrement (postscript) $num- ; # Decrement (prefix) - $num;
The decrement operator has the same meaning as the following description using subtraction operator.
$num = $num - 1;
Decrement operators are sometimes used in for statement.
for (my $i = 10; $i >= 0; $i- ) { print "$i\n"; }
This example program outputs 10 to 0.
Difference between prefix decrement and post - decrement
The prefix decrement and the suffix decrement have the same meaning when used alone, but have different meanings when the return value is used, such as when they are assigned.
Prefix decrement
For prefix decrements, the decremented value is returned.
my $num1 = 1; my $num2 = - $num;
"$Num1" is decremented to 0. This value of 0 is returned and assigned to "$num2".
I explained in the case of assignment, but I will also give an example of passing a value to subroutine. The fun is passed a decremented value .
func (-$i);
Postfix decrement
For prefix decrements, the value before decrementing is returned.
my $num1 = 1; my $num2 = $i- ;
"$Num1" is decremented to 0, but the value before decrementing, 1 is assigned to $num2.
The above two have different values in $k. In the first case, $k contains 0. In the second case, $k contains 1.
I explained in the case of assignment, but I will also give an example of passing a value to a subroutine. The fun is passed the value before it was decremented .
func ($i- );
Increment operator
The increment operator is the opposite of the decrement operator and is an operator that can add one. The increment operator is explained in detail below.