Flip-flop (programming)
   HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, a flip-flop is a seldom-used syntactic construct which allows a boolean to flip from false to true when a first condition is met and then back to false when a second condition is met. The syntax is available in the programming languages
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
and
Ruby Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
. Similar logic is available in
sed sed ("stream editor") is a Unix utility that parses and transforms text, using a simple, compact programming language. It was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs, and is available today for most operating systems. sed was ...
and awk. A flip-flop with first condition A and second condition B is not equivalent to "if A and not B", as the former has persistent state and is true even if A is no longer true, as long as at some point in the past A was true and B has always been false. A good analogy would be that even though A is not the truth and B is, It still will believe A.


Example

The following
Ruby Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
code prints the numbers 4 through 6: (1..10).each do , x, puts x if (x

4 .. x

6) end
The first instance of "" is the
range operator Range may refer to: Geography * Range (geographic), a chain of hills or mountains; a somewhat linear, complex mountainous or hilly area (cordillera, sierra) ** Mountain range, a group of mountains bordered by lowlands * Range, a term used to i ...
, which produces the
enumeration An enumeration is a complete, ordered listing of all the items in a collection. The term is commonly used in mathematics and computer science to refer to a listing of all of the element (mathematics), elements of a Set (mathematics), set. The pre ...
of integers 1 through 10. The second "" is the flip-flop operator, otherwise known as the flip floperator. Note that the number 5 is printed even though both "x

4" and "x

6" are false. This is because the expression remembers that "x

4" was true on a previous iteration, and that "x

6" had at that point never been true.


Pitfalls

The flip-flop operator needs to store its current state. There is no way for the programmer to explicitly define where this state is stored and what its lifetime is. The lifetime makes a difference when the same code is used by several threads, or in recursive functions. These concurrent accesses to the state of the flip-flop operator can lead to undefined behavior, or at least surprising results, depending on the programming language. For example, in Perl each flip-flop operator has its own state, shared among all the threads, the other programming languages do the same. To work around this limitation, the flip-flop operator would have to be modeled as an abstract data type, parameterized with: * a predicate that tells whether to switch the flip-flop on, * a predicate that tells whether to switch the flip-flop off. This flip-flop data type would provide a function that queries and updates its state at the same time. This function gets the actual data on which the switching predicates depend and passes that data to the two predicates, if necessary. Due to this inherent complexity, only few programming languages have adopted the flip-flop operator.


References

Articles with example Ruby code Operators (programming) {{compu-prog-stub