English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am resuming work on a computer programming certificate in a few weeks after a 2 year break. I've been reviewing all my old C++ materials, and need some more work with nested loops. I understand how to use them and I understand the basics; I'm just slow at it because it's one of my weak points and would like more practice. Also, if someone has a clear, simple way of explaining nested for loops, I'd appreciate it. I understand that the outer loop is usually for the row and the inner loop is the column, but if you were taught a trick that helps you predict it's behavior without tracing through it step by step, I'd really appreciate that.

2006-08-03 15:03:37 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

Want something to analyze.. analyze this psuedo code.. :P
for (int i=0; i<1000; i++)
{
__double randomValue = rnd.getDouble(); // Call a randomizer
__for (int j=0; j<1234; j++) {
_______ randValue2 = rnd.getDouble();
_______double x = randomValue * randValue2;
_______if (x > .63)
_______{
_________break;
_______}
_______cout << "This is totally pointless";
___}
}

2006-08-03 21:04:27 · answer #1 · answered by Alex 2 · 1 2

It might help to take a step back to look at the larger picture first. What you need to understand is how nested scopes work, not just nested for loops.

Think about it as though your program structure was organized as a stack. Every time you push something onto the top of the stack, it can access things lower on the stack (subject to some rules, of course). When the scope ends, it pops itself off the top, so that its parent scope gets control back. A scope receives control of the program when it is on the top of the stack, and it is not removed until it's finished with whatever it's doing.

The nesting of scopes, then, is just adding something new to the top of the stack. The previous scope will be around until you remove what you just put there, and won't receive control until the current scope is finished. It doesn't matter if the previous scope is another for loop, an if statement, the overall function, or whatever... all of these things just include the scope that gets conceptually pushed.

So, if you have a pair of for loops in a nested order, you basically enter the first loop (push it onto the stack, make it active), which then sets up and enters the second loop (pushes it onto the stack, makes it active). The second loop does whatever it does, until its finished iterating and ends up with a false condition. This pops the second loop off the stack and the first one regains control. It increments (or whatever), checks its condition, and then continues on to enter the second loop again (meaning the second loop gets pushed back on, does its thing again, and finishes). The second loop can, in this case, access variables from scopes lower down in the stack, so in the body of the second loop, you can use variables from the first loop.

I do also want to point out that for loops are a language construct. They're independant of implementation details, like rows, or columns, or nodes in your data structure, or strings in a comma seperated list. They can do anything. It doesn't make a lot of sense to talk about what they usually do.

Quick disclaimer. The stack here should be taken as conceptual, not literal. Depending on how the compiler is written, there may well be a stack involved, but this is not to be confused with the call stack of your program.

Hope that makes sense. Good luck!

2006-08-03 22:31:04 · answer #2 · answered by Ryan 4 · 0 0

Looks like you have two questions, how to understand nested loops, and how to visualize 2-dimensional or multi-dimensional arrays. Let's say that you are a shopping service. You have a list (array) of customers. Each customer has it's own list of items that you need to purchase for them:
[
Joe [soup, apple, cheese]
Lisa [corn, flour, sugar, milk]
Steve[soda, yogurt]
]

If you wanted to create a single list of items, you would use one loop to step through each customer and a second loop to step through each customers items. I hopes this makes it a little easier to visualize what's going on.

2006-08-03 22:50:17 · answer #3 · answered by sterno73 3 · 0 0

fedest.com, questions and answers