Hello @Prabhleen_sheenu,
No problem.
I will explain to you in brief.
In the case of while loop:
the loop will terminate when
- you will find the target element i.e. achieved using the break statement.
- or you will go out of the range i.e is achieved by the condition mentioned inside the while loop i<m && j>=0.
Also, the movement i.e. left and down will be decided by the else if and else statements inside the while loop.
Reason:
we are changing the value of index inside that.
In the case of for loop:
for(int i=0;i<n;i++){
for(int j=m-1;j>=0;jā){
the above mentioned nested loop will iterarte the matrix row by row and each row will traversed from right to left as the initial indeces are i=0 and j=m-1 and after each iteration we are decrementing j and incrementing i.
But, the else if and else conditions inside will also affect the movement.
This way the entire execution will become ambiguous and hence, not searching in step case fashion.
Hope, this would help.