how to bypass loop ?
-
I am debugging / modifying my working loop code and like to insert test code and "skip " the rest of large loop. What would be a cool way to accomplish that...? I have been "commenting out " temporary unwanted code , but that is pretty clumsy when large code blocks are commented out.
for (index = 0; index < list.size(); ++index)
{
... working code
{
...insert test block
}
... skip this working code
} loop end -
I am debugging / modifying my working loop code and like to insert test code and "skip " the rest of large loop. What would be a cool way to accomplish that...? I have been "commenting out " temporary unwanted code , but that is pretty clumsy when large code blocks are commented out.
for (index = 0; index < list.size(); ++index)
{
... working code
{
...insert test block
}
... skip this working code
} loop endIf I find I need to do something like this I do something like
#ifdef DEBUG
void foo( ... )
{
//
}
#endifIn debug mode, then I can call foo(arg, arg, arg, ... ) when I need to. Usually this would do something like print all the elements of a collection, for example. As far as I know, you can't skip over code. You can set a break point at the top of the loop, and
continue
to that point. If you really want to omit portions of the loop in your debug testing the best I could suggest would be to wrap that in an #ifdef DEBUG/#endif. Alternatively, you might#if DEBUG == 2
...
#endifOf course, you could use
#if DEBUG <= 2
, etc to get different debug levels of code when you compile. If you go that route, check with your IDE. It probably does not add a -DDEBUG to the compile command line in debug mode, so you'd have to do that yourself. If you want to have different debug levels you would pass -DDEBUG=2 (e.g.) (Linux - its probably different, but similar for windows)"A little song, a little dance, a little seltzer down your pants" Chuckles the clown
-
I am debugging / modifying my working loop code and like to insert test code and "skip " the rest of large loop. What would be a cool way to accomplish that...? I have been "commenting out " temporary unwanted code , but that is pretty clumsy when large code blocks are commented out.
for (index = 0; index < list.size(); ++index)
{
... working code
{
...insert test block
}
... skip this working code
} loop endDo you mean this (skip to next iteration)
for (int n=0; n<10; ++n)
{
cout << "working code A\n";
{
cout << "debug code, iteration = " << n << "\n";
continue;
}
cout << "working code B\n";
}Or, possibly, this (skip the loop)
for (int n=0; n<10; ++n)
{
cout << "working code A\n";
{cout << "debug code, iteration = " << n << "\\n"; n=10; // tainting loop variables is usually not recommanded, however this is debug code... continue; } cout << "working code B\\n";
}
?
"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
I am debugging / modifying my working loop code and like to insert test code and "skip " the rest of large loop. What would be a cool way to accomplish that...? I have been "commenting out " temporary unwanted code , but that is pretty clumsy when large code blocks are commented out.
for (index = 0; index < list.size(); ++index)
{
... working code
{
...insert test block
}
... skip this working code
} loop endIn C++ Macros - As noted you can remove the code entirely - You can also use a macro that resolves to a different value For code - Add a flag. - Add a class that manages flags Larger apps would use the second. Your code then tests the flag. There are various ways the flags can be set: command line, config file, database. There are various ways to manage the class itself. Could be static or use a builder pattern. Example of flag usage
if (myFlagClass.IsSet("TestFlag131")) ....
For systems (not just an app) can use special values in an API. The code of the target service looks for those special values and returns a fixed, or even variable, result. This is somewhat useful in verifying end to end enterprise functionality.