Unable to understand control flow in KMP algorithm
As explained in the comments, indentation marks code blocks. It is a standard way to denote them. while of line 6, repeats line 7, but has nothing to do with the if on line 8.
Indeed it only matters whether q and k are not zero.