3.10.1. Special Variables: Flags And Accumulators

Time: 00:04:11 | Download: Large, Large (CC), Small | Streaming, Streaming (CC) | Slides (PDF)

Sometimes, programs use variables in precise ways. It's often convenient to categorize these special variables based on their use - on the role they play in a program - rather than on their type (e.g., int or double). Two special kinds of variables are introduced or elaborated in this section: flags and accumulators.

Flags

A flag is a variable that stores the program's current condition, mode, state, or configuration. That is, it's a small amount of memory that "remembers" what the program is doing at some point in time. Flags are usually set at one location in a program and then tested at a different location or at another time. The program's behavior - what it does - depends on the value saved in the flag when the program tests it.

Computer scientists often use a visual diagramming language, called a state diagram, to help them represent and understand the dynamic nature of a program. They use flags to help convert the state diagrams into computer programs. We'll use the example of a simple stopwatch to demonstrate the connection between a state diagram and the corresponding computer code; the code will also illustrate the concept of a flag variable.

A simple state diagram. The diagram has two rectangles that represents two states. The states are labeled Stopped and Running. Arrows between the states represent state transitions. One arrow, labeled Press Button, points from Stopped to Running. A second arrow, labeled Press Button, points from Running back to Stopped. A large circle with an arrow pointer to the Stopped state indicates the start state.
bool	stopped = true;
	. . .
if (stopped)
{
	// code to start the watch
	stopped = false;
}
else
{
	// code to stop the watch
	stopped = true;
}
(a) State Diagram(b) Pseudo Code
State diagram and code for a stopwatch. A stopwatch has a button that both starts and stops the watch. If the watch is stopped when the button is pressed, then the watch starts, but if the watch is running (i.e., not stopped) when the button is pressed, then the watch stops.
  1. The rectangles represent the states in which the watch may operate at any time: the watch is either stopped or it is running. The arrows are the allowed transitions - the ways that the watch can change states - and the transition labels denote the events that cause a state change: the watch changes state (i.e., what it is doing) whenever the button is pressed. The large circle denotes the initial or start state when the program begins execution.
  2. The variable stopped is a flag, which is initially set to true: the watch is stopped or not running when the program begins. Pressing the button toggles the watch between running or not running - between being stopped and not being stopped.

Flags that represent conditions with only two possible states are conveniently implemented as type bool. The flag's name, that is, the variable's name, is chosen to reflect the states or conditions that the flag controls. In the previous example, we could have named the flag running and flipped the order of the statements in the if-statement. If a flag controls more than a single state or condition, then a behavioral of type int is appropriate.

Accumulators

The word accumulate means "to gather or pile up especially little by little." This definition nicely describes how a program uses an accumulator variable (or just "accumulator" for short). Accumulators may accumulate any kind of data, so accumulator variables may be of any appropriate data type. They are very often used in loops and gather or accumulate data during each iteration of the loop. Accumulators must begin in a known state, usually "empty." Programmers initialize the accumulator variables, usually to zero, above and outside the loop.

A photograph of someone sifting flour into a bowl.
int n;
int count = 0;
	...
while (...)
{
	cin >> n;
	count++;
}
(b)
double sum = 0;
	...
while (...)
{
	double	score;
	cin >> score;
	sum += score;
}
(c)
double balance = 0;
	...
while (...)
{
	if (...)
		balance += amount;
	else
		balance -= amount;
}
(d)
(a)
Using accumulators to gather data over time.
  1. Using a bowl as a metaphor for an accumulator: When I make bread, I begin by measuring four cups of flour into a bowl. If the bowl isn't empty when I begin, then I will have an unknown amount of flour in the bowl and my recipe won't work.
  2. Counting the number of data items read or input into a program is done easily with an accumulator.
  3. Maintaining a running total or sum is another common use for accumulators.
  4. There is no requirement that the value accumulating in an accumulator always increases - or always decreases for that matter. For example, maintaining a balance requires adding credits to the balance while subtracting debits from it.