Sir i wanted to know why are we adding thus if(curr==c) condition every tym

#include
using namespace std;
void spiralprint(int a[][10], int M, int N);
int main()
{
int i,j;
int M,N;
cin>>M>>N;
int a[10][10];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
cin>>a[i][j];
}
}
spiralprint(a,M,N);
cout<<β€œEND”;
return 0;
}
void spiralprint(int a[][10], int M, int N)
{
int sc=0,sr=0,ec=N-1,er=M-1,i;
int curr=0,c=M*N;
while(sc<=ec&&sr<=er)
{
if(curr==c)
break;
for(i=sr;i<=er;i++)
{
cout<<a[i][sc]<<", β€œ;
curr++;
}
sc++;
if(curr==c)
break;
for(i=sc;i<=ec;i++)
{
cout<<a[er][i]<<”, β€œ;
curr++;
}
er–;
if(curr==c)
break;
for(i=er;i>=sr;i–)
{
cout<<a[i][ec]<<”, β€œ;
curr++;
}
ec–;
if(curr==c)
break;
for(i=ec;i>=sc;i–)
{
cout<<a[sr][i]<<”, ";
curr++;
}
sr++;
if(curr==c)
break;
}
}
sir i wanted to know why are we adding thus if(curr==c) condition every tym

Hello @sneha23 in this condition we are checking whether the number of elements are not greater then total number of elements in the array.
total number of elements are m*n.
and everytime we are incrementing the curr for printing every element of the array.
if at any point we find that total elements we have printed is equal to the elements in the array the it will break at that point only.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.