Exiting nested loops

I started off with a really simple hash function, where words are indexed according to their first letter, so a would be 0, b = 1 etc. Then I insert a piece of code into the load function, which looks at other letters in the word if something is already inserted at that index. So for instance, if we had `"dog"` and `"dopamine"`, both would hash to 3, but then `"dopamine"` would go into a node later on in the linked list after going through the comparison loop. The only problem that I have, according to `gdb`, is that I have no idea how to exit said loop. Here is the code:

``````// if a node with that index already exists, separate chaining
else
{
new_node->next = NULL;

// use separate chaining...I guess :P
// traverse the list
node*prev = NULL;
for(node* cur = hashtable[index]; cur != NULL; cur = cur->next)
{
// compare words inside the list, letter by letter
for(int i = 1; i <strlen(new_node->word); i++)
{
if(cur->word[i] < new_node->word[i])
continue;  // break
else
prev = cur;
}
}
}
``````

So in essence, how would I jump from the continue statement to the outside of both for loops? I tried using break, continue, and something called `goto` I found online. The `goto` method looked sensible but the program wouldn't compile when I used it, so I would massively appreciate help :)

Use a function where you have the innermost for loop. That's the cleanest way. I have used "goto", i have to confess, but i used it at a time at which i wasn't too sure how to use functions. By now you should either know how to use them, or learn how to, imo.

Although i'm not too sure why you would want to check for the alphabetical order in which the words are loaded, since the dictionary is already alphabetically sorted :)

• Oh dear. You're right, I totally forgot the words were sorted! slow clap for self Thank you for your response nonetheless! Sep 20 '15 at 8:54

I don't understand exactly what you are trying to do in your code, and even though `goto` would solve your problem I think it's really bad in your code, especially when you are beginning your journey into programming.

To break through both of your loops, you can use a `bool` variable (I'll call it `flag`) like so. The loops normally would print 25 lines (5 * 5) but I want it to break when both values are 2, so I add the condition I want and change `flag` to `false`:

``````bool flag = true;

for (int i = 0; (i < 5) && flag; i++)
{
for (int j = 0; (j < 5) && flag; j++)
{
printf("i == %d ", i);
printf("j == %d\n", j);
if (i == 2 && j == 2)
{
flag = false;
}
}
}
``````

output

``````i == 0 j == 0
i == 0 j == 1
i == 0 j == 2
i == 0 j == 3
i == 0 j == 4
i == 1 j == 0
i == 1 j == 1
i == 1 j == 2
i == 1 j == 3
i == 1 j == 4
i == 2 j == 0
i == 2 j == 1
i == 2 j == 2
``````

See that I added `flag` in the condition that stops both loops, so if I turn `flag` to `false`, both of them will exit the next time they execute. Hopefully you can extrapolate this and integrate it in your own code.

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!