Solved

C# increment and decrement operators

Posted on 2009-05-10
6
589 Views
Last Modified: 2012-05-06
I'm just starting to read a C# book.  I would like to find out why running a program gave me the results it did.

/* This is a program that I'm writing
   to see if I can figure out */

class TryThis
{

  public static void Main()
  {

    // postfix increment
    int length = 3;
    int newLength = length++;
    System.Console.WriteLine("Postfix increment example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);

    // prefix increment
    length = 3;
    newLength = ++length;
    System.Console.WriteLine("Prefix increment example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);

    // postfix decrement
    length = 3;
    newLength = length--;
    System.Console.WriteLine("Postfix decrement example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);

    // prefix decrement
    length = 3;
    newLength = --length;
    System.Console.WriteLine("Prefix decrement example");
    System.Console.WriteLine("length = " + length);
    System.Console.WriteLine("newLength = " + newLength);

  }

}

This is the result:


C:\Documents and Settings>trythis
Postfix increment example
length = 4
newLength = 3
Prefix increment example
length = 4
newLength = 4
Postfix decrement example
length = 2
newLength = 3
Prefix decrement example
length = 2
newLength = 2

C:\Documents and Settings>

0
Comment
Question by:William Richardson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 12

Expert Comment

by:geowrian
ID: 24351558
Postfix can be rewritten like this:
int length = 3;
int newLength = length;
length = length + 1;

Prefix can be rewritten like this:
length = 3;
length = length + 1;
newLength = length;

basically, anywhere you see a prefixed ++, you can perform that operation on a new line above it. Anywhere you see a postfixed ++, you can perform that operation immediately after it.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24351618
Prefix notation (++variable/--variable) means that you increment a variable before you use it in the statement. Postfix notation (variable++/variable--) means that you use the variable, then you increment it.
int x = 1;
 
Console.WriteLine(++x);  // outputs 2 -- increment before use
Console.WriteLine(x++);  // outputs 2 -- use, then increment
Console.WriteLine(++x);  // outputs 3 -- increment before use
 
Console.WriteLine(x--);  // outputs 3 -- use, then decrement
Console.WriteLine(--x);  // outputs 1 -- decrement, then use

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24351630
Post/pre-fix inc/decrement operators are one of the most common sources of confusion for new programmers. The difference is extremely minimal, but the results can be catastrophic. For example, if I say the following:

int someNum1 = 50;
int someNum2 = someNum1++;

You would expect a call to
Console.WriteLine(someNum2);
to print out 51, correct? Actually, it will print out 50; however, if you print out someNum1 after that assignment, you'll find that it now holds 51.

The two main differences are as follows:
Pre-Inc/Decrement will evaluate the expression FIRST, and THEN assign the values. The operation
someNum2 = ++someNum1;
says add "1" to the variable called "someNum1", then takes it value, and copy it over to the variable called someNum2.

Post-Inc/Decrement will evaluate the expression AFTER the assigning the values. The operation
someNum2 = someNum1++;
says copy the value of the variable called someNum1 to the variable called someNum2. Then, AFTER this is done, add 1 to the value of the variable called someNum1.

However, this gets even more tricky. All this stuff just comes back to "operator precedence". The post-inc/decrement operators have a very low precedence, meaning that they are evaluated very late. Their precedence is so low, they are evaluated even AFTER the assignment has taken place! However, remember that parentheses () will force evaluation of their containing expression. So, contrary to what you'd expect, the following series of statements:

int someNum1 = 50;
int someNum2 = (someNum1++);
Console.WriteLine(someNum2.ToString());

Will actually display 51 to the screen. This is because the parentheses on the second line forces the evaluation of someNum1 BEFORE assigning its value.

This isn't as scary as it sounds, hang in there :)
Hope I helped,
Nate
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:William Richardson
ID: 24354865
length = 3;
    newLength = ++length;

Prefix increment example
length = 4
newLength = 4

On the expression newLength = ++length; is length getting a new value assigned to it with the prefix increment?  And when you have the a prefix increment/decrement is the precedence just taking place with the prefix first because there are two operators next to each other?  And with postfix increment/decrement is it just taking the value over the assignment operator before it assigns the incremented/decremented value to length?

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24355305
Don't think of postfix/prefix notation as two operators--it is one operator. In your most recent example, because you are using prefix notation (++ before the variable), the variable "length" will be incremented, then assigned to the variable "newLength."
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 75 total points
ID: 24355336
Think of prefix notation like: I want to increase/decrease the current value before I use it.

Think of postfix notation like: I want to use the current value, then increase/decrease it
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question