Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loop counter set to itself in a nested for loop, what does that mean?

Tags:

c++

For a project I am working on, I need to go through source code from a C++ program.

At several locations I am seeing something that I don't understand and I can't find anything about on the internet.

In several nested for loops, the counter var for the outer loop is set to itself. Is that to exit the loop, to skip one, anybody have any idea?

So it's the "n = n;" part :)

for (int n = 0; n < 12; n++) {
    for (int m = 0; m < 99; m++) {
        for (int p = 0; p < 10000; p++) {
            if (p == 2300) {
                n = n;
            }
            // code here
        }
    }
}
like image 568
A. van Weije Avatar asked Jan 28 '23 16:01

A. van Weije


1 Answers

The n = n; line of code does not do anything and the whole if (p == 2300) { n = n; } part will probably be optimized away in a release build.

I suspect the reason the original author did this was so that they could put a breakpoint on the n = n; line and catch it in the debugger when p is 2300. They probably submitted the change by mistake.

Modern IDEs will typically have a way of setting conditional breakpoints to break on such conditions but they can be slow and sometimes it is faster to recompile with a condition like this in.

like image 159
Chris Drew Avatar answered Feb 06 '23 15:02

Chris Drew