2
\$\begingroup\$

I'm setting up a hex grid. The column/row system I use puts 0, 0 as the top left corner, and increasing columns (x) moves to the right but also vertically upwards. So columns run straight up and down, but rows run to the right but slanted upwards a bit.

This code...

for (j = 0; j < bd*2-1; j += 1) // bd is "board dimension", in my case 9
{
  for (i = 0; i < bd*2-1 and i < j+bd; i += 1)
  {
    if (i <= j-bd)
      continue;

    scr_ini_hex(i, j, bxo + i*hs, byo + j*hh - i*vs); // bxo and byo are arbitrary board x/y origins where the first cell is drawn, hs, hh, and vs are the gaps between cells in horizontal and vertical directions.
  }
}

produced this:

enter image description here

which is exactly what I want, a large hexagon with 9 smaller hexagons on each side.

So I thought I would be sophisticated and move that continue condition into the for loop. I wrote this...

for (j = 0; j < bd*2-1; j += 1)
{
  for (i = 0; i < bd*2-1 and i < j+bd and i > j-bd; i += 1) // added "i > j-bd", basically migrated the condition into this inner loop
  {
    //if (i <= j-bd)
      //continue;

    scr_ini_hex(i, j, bxo + i*hs, byo + j*hh - i*vs);
  }
}

and it produced this:

enter image description here

What went wrong here? I thought they were logically equivalent (except negating the condition, because the continue code was like a do until, but the for condition is like a do while).

\$\endgroup\$
  • 2
    \$\begingroup\$ You need to step through your code in debugger and see what exactly is happening. \$\endgroup\$ – Kromster says support Monica Aug 26 '16 at 6:14
  • \$\begingroup\$ Wait, commenting out the continue draws only half a grid? Have you got your two snippets mixed up? \$\endgroup\$ – lozzajp Aug 26 '16 at 7:58
  • \$\begingroup\$ @lozzajp It's not only commenting out the continue condition. I moved it to the inner for loop (but negated it because the for loop is like a do while whereas the inline thing was like a do until). \$\endgroup\$ – DrZ214 Aug 26 '16 at 9:34
  • \$\begingroup\$ @DrZ214 Ah I see what you mean, you "moved" the continue to the for loop condition. The answer below is what you want to see, as it says the inner for loop is exiting and returning control to the next iteration of i in the outer loop. \$\endgroup\$ – lozzajp Aug 26 '16 at 9:37
2
\$\begingroup\$

The difference is that continue only skips forward one step and the condition ends the whole loop.

To me it seems like the issue is, that you try to fit both the starting point and the end point into the loop-condition. When you should set the different starting point in the loop-initialization:

for (j = 0; j < bd*2-1; j += 1)
{
    for(i = max(0,j-bd);i < bd*2-1 and i < j+bd; i++)
    {
        scr_ini_hex(i, j, bxo + i*hs, byo + j*hh - i*vs);
    }
}
\$\endgroup\$
  • \$\begingroup\$ That worked almost perfectly, i had to make it max(0, j-bd+1) But thanks! Yes, I should have realized the separate start and end conditions. I personally dont like for loops and try to use do whiles because the for clusters things up that would be more readible spread out. \$\endgroup\$ – DrZ214 Aug 27 '16 at 6:53
4
\$\begingroup\$

The condition part of the for-loop tells the loop when to stop looping. This is different from the continue statement that just skips the rest of the current step and moves on to the next.

Consider the following loop;

for(i = 0; i < 10; i += 1) {
    if (i % 2 == 0)
        continue;  // Skip even indices

    // Do stuff
}

If you were to extract the condition for the continue and place it as part of the for-loop you'd get

for(i = 0; i < 10 && !(i % 2 == 0); i += 1) {

    // Do stuff only on odd ones
}

But that means that the loop will exit as soon as it sees an even number, this is not what the continue statement does.

\$\endgroup\$
  • \$\begingroup\$ Right, continue will skip over things, whereas the for condition(s) will break everything. This is why I negated it in the migration and expected something logically equivalent. I must be missing something. Is there any way for me to move my condition into the for loop with the right transformation? I will experiment with ands and ors with grouping, because i have 3 expressions in the for conditions right now. \$\endgroup\$ – DrZ214 Aug 26 '16 at 9:47
  • 1
    \$\begingroup\$ @DrZ214 I don't think it is possible, also I would probably opt for the continue statement for readability. You are saying I don't want to run the rest of this statement but I do want to continue with the for loop. If it is in the for loop conditional it is saying the condition is not met so stop the for loop all together. \$\endgroup\$ – lozzajp Aug 26 '16 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.