visual-studiodebuggingiterationbreakpointsconditional-breakpoint

How to iterate a loop a certain number of times when debugging in Visual Studio?


Visual Studio allows debugging line by line, or to jump straight to anywhere using a breakpoint.
But for example:

for (int i = 0; i < 100000; i++)
{
    //DO SOMETHING HERE
}

How can I jump immediately to 500th iteration of the loop?

The fastest I have found so far was to:

I'm using Visual Studio 2019.


Solution

  • You are looking for a conditional breakpoint that has been already covered few times on StackOverflow.

    Here is answer for your loop-specific problem:

    1. Click on the gear button...

    Breakpoint setup

    1. Setup your condition

    Write down your condition.

    Another possible solution

    for (int i = 0; i < 100000; i++)
    {
        #if DEBUG
            if( i == 500)
            {
                System.Diagnostics.Debugger.Break();
            }
        #endif
    }