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

1

1

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:

  • set a breakpoint at the begin of the loop, and
  • then press F5 multiple times.

I'm using Visual Studio 2019.

Laddy answered 24/11, 2019 at 8:13 Comment(1)
Possible duplicate of How to set conditional breakpoints in Visual Studio?Sheldonshelduck
S
1

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
}
Sheldonshelduck answered 24/11, 2019 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.