%===========================================================
%=====     The "Edge-detector" Finite-state Machine    =====
%===== The "EdgeDetectorFiniteStateMachine()" Function =====
%===========================================================
function [] = EdgeDetectorFiniteStateMachine()

clear
clc

f = 'f';
g = 'g';
h = 'h';

InitialState = 'f';

CurrentState = InitialState;

Input = 0;

while (Input ~= 9)
    Input =  input('Enter a Binary 0 OR 1, 9 To Exit: ');
    switch (CurrentState)
        case f %The Initial State.
            switch (Input)
                case 0
                    display('0');
                    NextState = g;
                case 1
                    display('0');
                    NextState = h;
            end
        case g %When The Last Input Was 0.
            switch (Input)
                case 0
                    display('0');
                    NextState = g;
                case 1
                    display('1');
                    NextState = h; %0 -----> 1 Edge.
            end
        case h %When The Last Input Was 1.
            switch (Input)
                case 0
                    display('1');
                    NextState = g; %1 -----> 0 Edge.
                case 1
                    display('0');
                    NextState = h;
            end
    end
    CurrentState = NextState;
end