• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
reloadead

Breaking out of a double for loop

7 posts in this topic

Hey guys,

I am busy with a small audio engine, playing .wav files and although everything works fine, I'm having some second thoughts about a certain part where I need to break out of 2 loops at certain points.

The way I do it now is as following:

[CODE]
static bool earlyout = false;
for(iterate through sounds)
{
// gather some relevant data
for(iterate through buffer)
{
if(something)
{
earlyout = true;
break;
}
}
if(earlyout)
{
earlyout = false;
break;
}
}
[/CODE]

it does the job, but I think this looks very ugly. The other option I had in mind was to use a goto: but I don't like using goto.. Does anyone have an idea of how I can do this in a more proper/cleaner way? If there is any at all?

Much appreciated!
0

Share this post


Link to post
Share on other sites
Some options: refactor the inner loop into its own function and break out based on the return value, refactor so that there's nothing after the two for loops and just return from the function, modify the iteration variable(s), work the early out condition into the iteration condition, etc. With a concrete example a better suggestion might be possible. Also, why are you making the condition variable static?
1

Share this post


Link to post
Share on other sites
That's fine or do it like SiCrane said, or you can restructure your engine.

Why are you iterating through sounds? Are these sound effects? What if you put each one of sound effects to its own class, then have each one to be responsible of its own buffers?
0

Share this post


Link to post
Share on other sites
Thanks for the tips, I'm going over the code again to see if I can avoid the break altogether, I think it is possible, else the inner loop in a function seems to be a good option. I was using the static purely as I was testing some things out before I made some final adjustments and in here to simply make a point.

@alnite
I am iterating through the current active sounds as I need to mix them into a single buffer for playback. Basically I have an abstract sound class with some needed variables like 3D sound, looping, volume, etc. and specific sound classes (at the moment only .wav) which hold file format specifics. When I want to play a sound, I simply add it to a vector of currently active sounds that do their magic with the data and add it to the buffer (or in my case, 2) that I use for playback.
0

Share this post


Link to post
Share on other sites
For the solutions involving refactoring things into functions, that's great as long as you can give the function a good name. If a chunk of code doesn't satisfy that rule, and especially if you have to pass a bunch of local variables to the helper function, I would much rather have a `goto' to break out of the nested loops.

`break' is just a `goto' to just past the loop. `continue' is just a `goto' to the end of the body of the loop. Since these usages of `goto' are common and perfectly acceptable, the language provides neat little aliases for them, and that way you don't need to write an ugly label. The language didn't provide a neat little alias from breaking out of nested loops (or a switch within a loop), so you need to write the ugly label. Give the label a good name and the code is perfectly readable.

[code]

for(iterate through sounds)
{
// gather some relevant data
for(iterate through buffer)
{
if(something)
goto BREAK_OUT_OF_LOOP_OVER_SOUNDS;
}
}
BREAK_OUT_OF_LOOP_OVER_SOUNDS:;
[/code] Edited by alvaro
1

Share this post


Link to post
Share on other sites
When the outer loop is not of for-each style, you can do
[code]
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
{
if (condition(i, j))
{
i = n; // Clear the condition of the outer loop so we don't make any extra iterations after finishing the inner loop.
break; // Break inner loop.
}
}
[/code]

If the conditions of the outer loop are complicated, you can transform that to similar thing you do using a boolean variable (but there's really no need to make it static):

[code]
bool iterating = true;
for(int i = 0; complicatedCondition(i) && iterating; ++i)
for(int j = 0; j < m; ++j)
{
if (condition(i, j))
{
iterating = false; // Clear the condition of the outer loop so we don't make any extra iterations after finishing the inner loop.
break; // Break inner loop.
}
}
[/code]

But most often, as was mentioned above, I also favor turning the double loop into a single loop and a helper function which encloses the inner loop.
1

Share this post


Link to post
Share on other sites
I'd personally favour either refactoring or goto. The alternate methods seem neat ways to avoid this, but the major advantage of refactor or goto is that it's immediately obvious to the reader (who may be yourself in 6 months time) what your intention is. goto is bad karma for sure, but only when using it instead of other more appropriate control-flow statements, and there are cases where using goto is actually cleaner, more legible, and less prone to unwanted side-effects than a tricksy method of avoiding it. With that said, I would say refactor first and only use goto if that's absolutely unacceptable. Edited by mhagain
0

Share this post


Link to post
Share on other sites
Most of the time I encounter such a situation, breaking the inner loop into its own function is the best solution, but occasionally (rarely) I find that gotos make more sense (though mine are usually the result of larger-scale architectural problems). If it's clean and easy to read using the goto do so, but first check about breaking the inner loop into a function - it definitely feels like (and usually is) the cleanest solution here.

In my entire code-base, I have two occurances of the word 'goto'. One I'm happy with, and one I'm not - but it needs re-factoring on a slightly larger architectural scale and I have more pressing areas of code to work on. The number of 'loops within a loop' broken into separate functions are probably around 20 or so, but I'm just guessing since I can't easily measure that. Number of 'loops within a loop' that don't need to be re-factored into separate functions (but also don't use goto) are probably about 10.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0