# Moving a continue condition into the for loop produced something NOT logically equivalent?

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:

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:

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).

• You need to step through your code in debugger and see what exactly is happening. Aug 26, 2016 at 6:14
• Wait, commenting out the continue draws only half a grid? Have you got your two snippets mixed up? Aug 26, 2016 at 7:58
• @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). Aug 26, 2016 at 9:34
• @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. Aug 26, 2016 at 9:37

## 2 Answers

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);
}
}

• 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. Aug 27, 2016 at 6:53

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.

• 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. Aug 26, 2016 at 9:47
• @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. Aug 26, 2016 at 9:49