Counters and Flags: An Essential in the Programmer’s Utility Belt

Eitan Spitz
4 min readNov 13, 2020

--

I’ll set the scene: You’re writing a simple loop and you want it to repeat a certain amount of times. How would you approach it? Your first instinct would be to just tell the loop how many time you want it to repeat.

Okay, that was simple enough. Now — what if you want this loop to run a different number of times depending on the array you give it?

Easy — the next evolution would be to count the array length, then feed that number to the times loop:

Now let’s say you want to use the contents of the array itself in the loop. This brings us to the basis of enumerables, which are some very powerful tools if used correctly.

The first and most utilitarian of the enumerables is the each method. This method loops over the given array allowing the elements in the array to be used and manipulated. There are many other enumerables, some of the most common of them being sum, map, find, and select.

Sum is pretty simple — it iterates through the array and, assuming all the elements can be added together, adds all of them into one sum.

Map needs to be given a block of code and preforms it on every element of the array to produce a new array with the results.

Find and select are pretty similar — they each take a block of code to identify an element in the array, however the differ in what they return:

Find returns only the first element that fits the code that was given.

Select returns all of the elements that return true.

Alright, now that we’ve got our basic enumerables down, let’s kick it up a notch.

Say you want to iterate over an array of integers, and want to add together the first half of the elements into one sum and the second half into another?

Perhaps you would think to split the array in half and run two separate loops. While that is definitely a solution, it is one of many. As you start to learn with code, there are a million ways to get the same result — but no all solutions are equally efficient. Let’s try to do it in only one loop using some new friends, the flag and counter.

Techterms.com defines the flag as follows — “In computer science, a flag is a value that acts as a signal for a function or process. The value of the flag is used to determine the next step of a program. Flags are often binary flags, which contain a boolean value (true or false). However, not all flags are binary, meaning they can store a range of values. You can think of a binary flag as a small red flag that is laying flat when it is false, but pops up when it is true. A raised flag says to a program, ‘Stop — do something different.’”

Our second tool is the counter. A tool that is used more commonly, the counter is a variable that is set to 0 and iterates throughout the loop.

To show how these two tools interact together quite well, let’s return to our two sums loop.

First, we set both the flag and counter outside the loop to ensure they both are reset to their starting values before each use of the code.

Then, we need to set up the logic inside the loop.

We start with writing in the iteration of the counter at the end of every iteration of the loop.

Then we set up the “if” statement after it to check if the counter is equal to half the length of the array (meaning that we’ve reached the halfway point in the array and want to switch to the second sum) and the code it executes is to “flip” the flag to it’s second position.

Now we can refactor the code in the loop to check the flag before it adds the two numbers, and if the flag has been “flipped” then it will switch over to the second sum.

How about increasing the complexity by just a little more — how can we add each third of the array inside a single loop?

The final piece to this puzzle is the case statement.

The case statement is essentially a fancy ‘if elsif else’ statement. We set a variable for the case statement to check and use the when keyword to specify what it should to in each case. (See, sometimes programmers name things really well!)

Now to incorporate it into our code.

Using two counters this time, we can have one track how many times we’ve gone through the loop (and thus how far into the array we’ve gone) and in turn adding in logic to iterate the second counter every time we hit a third.

Hopefully now you’ll be able to use flags and counters to make your code more dynamic. Good luck!

--

--

Eitan Spitz

Software Engineer student at the Flatiron school, looking to take on the programming world