A core feature of Visual Studio that allows developers to inspect, analyze, and troubleshoot code during execution.
In C++ code running under the Visual Studio debugger, execution cannot be arbitrarily “branched” to an arbitrary source line from the debugger UI. The debugger supports:
- Setting breakpoints on the line where execution should pause, then continuing with F5 so the program runs normally until that point.
- Using step commands (F10, F11) to move through code one statement at a time.
To transfer control inside the program logic itself, the C++ language provides the goto statement:
void func()
{
// ... some code ...
goto target; // unconditional branch
// code skipped by goto
target:
// execution continues here
}
Rules for goto in C++:
- The label must be in the same function as the
goto. -
gotocannot jump forward over the initialization of a variable that would be in scope at the destination. - It is generally recommended to use
break,continue, andreturninstead ofgotowhen possible.
So, to “branch” while debugging:
- Use breakpoints and stepping in the debugger to control where execution stops and how it advances.
- Use
gotolabels in the C++ code itself if an unconditional jump within a function is required by the program logic.
References: