?
Solved

C# increment and decrement operators

Posted on 2009-05-10
6
Medium Priority
?
610 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 300 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 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