Your logic should work out as , if x>Length(4) then 0 else 1, then if x<0 then 0 else previous conditional. Do the same with z then attach both axis checks to your last conditional.
A simple shape shader would be a better option for this function, but as a learning exercise you're not far off, and like everything there's several ways of doing the same thing.
Cheers
Jon