Results 1 to 4 of 4

Thread: [RESOLVED] c# Incrementors

  1. #1

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Resolved [RESOLVED] c# Incrementors

    Hi,

    Why do the following code fragments not behave the same way? In Java I'm quite sure the two would behave the same.

    Code:
    for (; BufferPos < BufferLen; BufferPos++)
                        {
                            if (ReadBuffer[BufferPos] == search[0])
                            {
                                found = true;
                                break;
                            }
                        }
    Code:
    while (BufferPos < BufferLen)
                        {
                            if (ReadBuffer[BufferPos++] == search[0])
                            {
                                found = true;
                                break;
                            }
                        }

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: c# Incrementors

    It has to do with when the variable is incremented... On that second one, if you use ++BufferPos, you might find that it then does work as expected.

    Let's say BufferPos is 10... in the first example, BufferPos is incremented at the top of the loop, to 11, so you get ReadBuffer[11] ....

    In the second example, it will use BufferPos FIRST, THEN increment it after the fact.... so you end up using BufferPos[10]... BufferPos[11] doesn't get used until the next loop through.

    to see how it varies try something along these lines:

    Code:
    int a,b,c;
    
    a = 1;     //a = 1
    b = a++; // a = 2; b = 1 (a is incremented after the value is assigned to b)
    c = ++a; // a = 3; c = 3 (a is incremented first, then assigned to c)
    That was something I had trouble getting my head around at the time.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: c# Incrementors

    In a 'for' loop, the loop variable isn't incremented at the beginning of the loop. It's incremented at the end, and that's your problem.

    In the first code snippet the loop variable is incremented at the end of the loop, but when you find a match you hit a 'break' so the end of the loop isn't reached on that iteration. In the second code snippet you increment the variable immediately after use, before you hit the 'break'.

    I think you'll find that Java would work the same way. It certainly should because a 'break' statement should break out of the loop immediately.
    Last edited by jmcilhinney; Feb 3rd, 2010 at 08:40 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: c# Incrementors

    Yeah it does work as expected in c#. I didn't see my issue until after I've looked at it again after my 2 hour commute home.

    The reason while loop version was failing is that BufferPos was incremented when I found a match which it shouldn't have.

    One of those freak logic issues that is so below the radar that I miss it.

    And I think I was doing some step through debugging the wrong way or something. Not quite familar with .NET IDE so much!

    Thanks guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width