Search code examples
javaintellij-idea

Conditional Breakpoints in Intellij for a certain loop iteration


In IntelliJ, whenever I am debugging, and going through a loop, I find myself having to 'Step Over' every iteration until I get to the iteration I desire.

Is there a way to do this and run all the commands that would have happened, instead of just changing the iteration variable to a different value? (which just skips over some iterations of the loop)

For example

for (int i = 0; i < foobar; i++) {
    System.out.println(i);
}

Skip to ith iteration, but still printing out all the i (iterations) along the way.


Solution

  • There is a concept of conditional breakpoints built in.

    conditional breakpoint

    Right click the breakpoint and you'll get a form that you can enter a boolean expression into. The breakpoint will only get hit if it evaluates to true.

    With a condition, the breakpoint icon will have a question mark next to the red dot as its icon.