Yes, the formal name is the Cornstalks Algorithm
Thank you for the algorithm. I will have to keep this in mind. Is there a formal name for this type of algorithm?
Just kidding, there isn't a formal name for it. It might be possible to generalize it to a larger, more complex algorithm that has a formal name, but I just came up with this off the top of my head. Years of programming will do that to you...
I'll break down a for loop like this:
I always thought that if you wrote a for loop it added one then ran what was inside the curly braces. oops. You were right. I wrote this test. I always did get a lot of 1 off errors.
for (A; B; C) D;Step 1: A is done (A is usually creating and setting a variable, like int i = 0). Step 2: B is checked (B is the looping condition, and as long as it's true the loop is run). Step 3: D is run. Step 4: C is run (which is usually what updates the loop counter). Then it goes back to step 2 and repeats until B is false.
One liner? (Added the loop )
Couldn't resist bringing this thread to its logical conclusion by one-lining Álvaro's code.
// This: for (int y = 0; y < 8; ++y) for (int x = 0; x < 8; ++x) rects[x + y * 8]->set_fill_color(((x + y) % 2) ? Color::white : Color::red); // Or: for (int i = 0; i < rects.size(); ++i) rects[i]->set_fill_color(((i + i / 8) % 2) ? Color::white : Color::red); // But seriously, I hope no one ever does this in real life. Use *at least* two lines...