Would you please suggest some tips to understand such problems ? |
Look at each line of code and walk though it thinking "what is the computer going to do here?" It all comes down to a step-by-step sequence, and all you have to do to understand the overall process is understanding each step.
If you have difficulty doing this...
use a debugger. Debuggers have 3 key features:
1) You can set a breakpoint on any line of code. When the program gets to that line of code it will "snap", pausing program execution and bringing the debugger/IDE to the foreground.
2) It has a "watch" window, where you can type in the name of any variable, and it will show you that variables contents. Combine this with #1 and you can see not only where the program is, but also what all the variable are filled with at the time.
3) It has the ability to "step into"/"step over" lines of code. When the debugger is snapped... these will allow the program to move forward exactly one line of code.
Using Step Into / Step Over with a Watch window will allow you to walk though the program and see exactly what is happening, line by line. It'll show you which lines are executing, and the contents of all interesting variables at the time. You can see exactly how it's working.
How to use basic functionality of debuggers really should be taught from day 1 in programming courses. I really don't understand how so many of them don't even mention them.
Is there any tutorial exists on net which explains nested For Loop iteration better ? |
There's nothing special to nested for loops. If you understand how a for loop works... then a nested for loop is the same thing. It's just a loop that inside another loop.
In this case... the inner for loop on line 18 is looping once for each column
The outer for loop on line 14 is looping once for each row.
Put them together and you're looping once for each column, in each row. It isn't any more complex than that.