Spiral print printing extra element

The above code works fine for rows =columns but for cases like row=3 and column=4, it is printing an extra element.
I am operating a parent while loop which keeps a count of each element printed. Why then are 13 elements being printed when only 12 should be printed?

@bluejay,
I have corrected your code: https://ide.codingblocks.com/s/197078

I have added sr <= er && sc <= ec in every for loop to keep a check on elements.

Why is the count method not working in this case?

@bluejay,
While loop checks the conditions and then executes the code in it.

Here when the count exceeds the limit while loop is still in progress and it breaks only after the entire code is executed. Another way that should work is include count in all for loops as well.

Ok. I get it now. Thanks.