Flip-flop (programming)
In computer programming, 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.[src] The syntax is available in the programming languages Perl and Ruby.[src] Similar logic is available in sed and awk.[src] 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.[src] A good analogy would be that even though A is not the truth and B is, It still will believe A.[src]
In computer programming, 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.[src] The syntax is available in the programming languages Perl and Ruby.[src] Similar logic is available in sed and awk.[src] 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.[src] A good analogy would be that even though A is not the truth and B is, It still will believe A.[src] ==Example== The following Ruby code prints the numbers 4 through 6: (1..10).each do |x| puts x if (x == 4 ..[src] x == 6) end The first instance of " " is the range operator, which produces the enumeration of integers 1 through 10.[src] The second " " is the flip-flop operator, otherwise known as the flip floperator.[src] Note that the number 5 is printed even though both "x == 4" and "x== 6" are false.[src] 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.[src] ==Pitfalls== The flip-flop operator needs to store its current state.[src] There is no way for the programmer to explicitly define where this state is stored and what its lifetime is.[src] The lifetime makes a difference when the same code is used by several threads, or in recursive functions.[src] 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.[src] For example, in Perl each flip-flop operator has its own state, shared among all the threads, the other programming languages do the same.[src] 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.[src] This flip-flop data type would provide a function that queries and updates its state at the same time.[src] This function gets the actual data on which the switching predicates depend and passes that data to the two predicates, if necessary.[src] Due to this inherent complexity, only few programming languages have adopted the flip-flop operator.[src] ==References==[src]
25 sentences · 25 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Flip-flop (programming)." encyclopegia.org, 2025, https://encyclopegia.org/wiki/flip-flop-programming. Accessed 28 November 2026.
Chicago
"Flip-flop (programming)." encyclopegia.org. Accessed November 28, 2026. https://encyclopegia.org/wiki/flip-flop-programming.
APA
encyclopegia.org. (2025). Flip-flop (programming). Retrieved November 28, 2026, from https://encyclopegia.org/wiki/flip-flop-programming
Accessed November 28, 2026 · CC BY-SA 4.0 · citation page · JSON