# A little fuzzy on ++

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi All,

I am a little fuzzy as to "++".  What does it do
before and what does it do after a variable?
And why would your use it before or after?

\$ perl -e '\$A=10; \$B=++\$A; \$C=\$A++; \$D=+++\$A; print "A = \$A
++A = \$B    A++ = \$C   +++A = \$D \n";'

A = 13   ++A = 11    A++ = 11   +++A = 13

Why is \$A 13?

By chance, when I say "\$B=++\$A" am I changing \$A first
and then assigning it to \$B?  By why 13 and the others
11.

Many thanks
-T

## Re: A little fuzzy on ++

On 03/08/15 21:27, T wrote:

""++" and "--" work as in C. That is, if placed before a variable, they
increment or decrement the variable by one before returning the value,
and if placed after, increment or decrement after returning the value."

Want to know where I got that?  From perlop, where all the operators are
very carefully, and generally fairly lucidly, explained.

Because you set it to 10, then incremented it once when you did the \$B
assignemt, again when you did the \$C and yet again when you did \$D.  Am
I being unkind when I suggest you might have worked that out, or used a
series of simple examples to demonstrate it to yourself?

As I said above, but I'm saying it again:

"if placed before a variable, they increment or decrement the variable
by one before returning the value"

In this series of tutorials you've received you have asked some good
questions; these are not among them.

--

Henry Law            Manchester, England

## Re: A little fuzzy on ++

On 08/03/2015 02:03 PM, Henry Law wrote:

Yes I do and thank you.  Still don't have a good feel
for where to look.

You are graciously sharing your knowledge.  I am taking
it in the spirit it is offered and very much appreciate it,
if I have not made that clear.

See below.

I am on a group with a bunch of PHd in the sciences.  They
say that "the only dumb question is the one you did not ask."
I realize I ask a lot of dumb questions.

I read what you said about the =++ before and after (I don't know
C by the way), but I still don't get it.

Making up an example, I still don't get it.  It does not seem to

\$ perl -e '\$A=\$B=10; print "  A = \$A  B = \$B\n";
\$C=++\$A; \$D=\$B++;
print "++A = \$C  B++ = \$D \n";
print "  A = \$A  B = \$B \n";'

A = 10  B = 10
++A = 11  B++ = 10
A = 11  B = 11

Why is "B++ = \$D" (\$D=\$B++) not 11?  By chance does

\$D=\$B++
mean
\$D=\$B; \$B=\$B+1

-T

I am still not use to things that get assigned on the
right side of the expression, or things with no right
and left side, just a single expression.

"\$A++" looks weird to me.  I am still thinking in terms
of "A\$=A\$+1", which has a right and a left side.

## Re: A little fuzzy on ++

On 08/03/2015 03:22 PM, T wrote:

This is a note I wrote myself.  Is it correct:

perl: ++ and --

See: perldoc perlop

"++" and "--" work as in C. That is, if placed before a variable, they
increment or decrement the variable by one before returning the value,

\$C=++\$A  means
first step   \$A=\$A+1;
second step  \$C=\$A;

and if placed after, increment or decrement after returning the value.

\$D=\$B++  means
first step  \$D=\$B;
second step \$B=\$B+1;

## Re: A little fuzzy on ++

[...]

This is mechanically true but I fear it won't get you
anywhere. Assignment is not a weird special case in Perl.

\$C = \$D

is an expression which, when evaluated, assigns the value of \$D to \$C
and also returns it.

++\$A

is also an expression. Evaluating it adds 1 to the value of \$A and
returns the new value.

\$A++

is an expression which causes 1 to be added to the value of \$A but
returns the old value. It's vastly more popular for some unknown reason
(presumably because someone happened to use it in an example where the
returned value didn't matter about 40 years). Since the right-hand side
of an assignment expression is itself an expression,

\$C = ++\$A

or

\$C = \$A++

are syntactically valid. As is

\$D = \$C = ++\$A

## Re: A little fuzzy on ++

It works exactly the same as in e.g. C and many other programming
languages.

Because sometime you want to pre- and sometimes to post-increment the
value.

Because the original value was 10 and you incremented it 3 times.

That is the semantic of pre-increment, yes.

Well, let's dissect your code and write it with some space for
clarification:

\$A = 10;
\$A has the value of 10.

\$B = ++\$A;
\$A is incremented to 11 and \$B is assigned the value of \$A which is 11.

\$C = \$A++;
\$C is assigned the value of \$A which is 11 and \$A is incremented to 12.

\$D =+  ++\$A;
\$A is incremented to 13 and \$D which is currently not initialized is
incremented by the value of \$A, i.e. to 0+13 which is 13.

jue

## Re: A little fuzzy on ++

On 08/03/2015 03:15 PM, J�rgen Exner wrote:

I did not realize things could happen on the right side
of the expression.  I am stuck on the left side always
being the result.

Hi Jue,

¡Ay, caramba!

Thank you for helping me with this!  I am getting there.

-T

“Always acknowledge a fault. This will throw those in authority
off their guard and give you an opportunity to commit more.”
― Mark Twain

## Re: A little fuzzy on ++

On Tuesday, 4 August 2015 04:04:42 UTC+5:30, T  wrote:

And while you are at it, also consider the twist introduced by perl due to

the ++ operator when the operands are non-numeric. .e.g.,

perl -le '\$s="xz";print ++\$s'; # gives "ya"
when value is "zz" ++ will roll over to "aaa"

## Re: A little fuzzy on ++

On 08/08/2015 10:45 AM, sharma__r@hotmail.com wrote:

Hi Sharma,

I am writing this down!  And it even carries "aaa"!

I tried doing something like that and got a syntax error.
I thought I was just pushing it too far.   Now I know the
right way!

Thank you,
-T

## Re: A little fuzzy on ++

On 08/08/2015 02:05 PM, T wrote:

And maintains case!

\$ perl -le '\$s="xz";print ++\$s'
ya

\$ perl -le '\$s="Xz";print ++\$s'
Ya

\$ perl -le '\$s="ZZ";print ++\$s'
AAA

\$ perl -le '\$s="zz";print ++\$s'
aaa

## Re: A little fuzzy on ++

Most simple example I can think of:

my \$foo = 2;
my \$bar = 2;

print \$foo, "\n";
print \$foo++, "\n";
print \$foo, "\n";

print \$bar, "\n";
print ++\$bar, "\n";
print \$bar, "\n";

Justin.

--
Justin C, by the sea.

## Re: A little fuzzy on ++

On 08/04/2015 12:59 AM, Justin C wrote:

Thank you!

## Re: A little fuzzy on ++

On 03/08/15 21:27, T wrote:

OK, now I've had my little moan I feel better; so let me address this
part of the question.  IMO it's a matter of programming style.

Say you have a counter which you're incrementing as some loop runs, and
you want to stop the loop when you get to some number (maybe that's the
maximum number of things you can handle at one time, or whatever).  You
could code this:

my \$counter = 0;
while( 1 ) {                   # Iterate endlessly
do_stuff();
\$counter = \$counter + 1;     # or \$counter++!
# or ++\$counter; it doesn't matter
# here because you don't care when
# the increment is done
last if \$counter > MAXIMUM;  # MAXIMUM is a constant, set earlier
}

... and that works (absent some typo which I've missed!)

But in a more Perlish style you can achieve the same result with much
less typing.  I'd code this (though there are others who would do it
differently; they'll be in the door any moment now)

my \$counter = 0;
do_stuff() while ++\$counter <= MAXIMUM;

This neatly incorporates the counting-up and the comparison all in one
go, and the prefix ++ is used because I want to count up _before_ I
compare the count to MAXIMUM.

Oh, and check operator precedence and associativity in perlop! "++" is
above ">=", which is why you don't have to code

do_stuff() while (++\$counter) >= MAXIMUM;

--

Henry Law            Manchester, England

## Re: A little fuzzy on ++

On 08/04/2015 01:00 AM, Henry Law wrote:

Thank you!

## Re: A little fuzzy on ++

On 3/8/2015 23:27, T wrote:

well  T  the following will shed some light

my \$a = 1;

say ++\$}};    # 5

## Re: A little fuzzy on ++

On 08/04/2015 02:45 PM, George Mpouras wrote:

Interesting!  Thank you!