Вопрос

I am interested to see how a for loop / while loop would be implemented as an automaton. I am having difficulty imagining how that would work. Say the while-loop did this:

var i = 0
while (i < 10) i = i + 1

Not sure what happens if that were implemented as an automaton (what the states and transitions would be, and where the operation i = i + 1 would occur in the automaton). Wondering if one could show the main states and transitions of this if it were an automaton.

Это было полезно?

Решение

It looks like this:

   i++  i++  i++  i++  i++  i++  i++  i++  i++  i++
(0)->(1)->(2)->(3)->(4)->(5)->(6)->(7)->(8)->(9)->(10)

Each time you change i you're changing state. Loops require state to be different on each iteration so they won't look like loops when graphed as automata that show variable state. This isn't the typical usage of automata since there actually isn't an external input controlling the transitions. It's just the program repeatedly executing state mutating code.

Лицензировано под: CC-BY-SA с атрибуция
scroll top