Tuesday 19 March 2013

Dry Run

The most important thing before going into programming is one thing: The dry running of programs..!! Every program, after you have created or you are seeing someone else's creation, to understand the logic behind the program,try to dry run it!

Now, you all must be wondering what dry run is...

DRY RUN is not being dry and running... but analyzing every step of the program, say for example, you reach a code statement such as:

..
..
int c=0,i;
for(i=0;i<5;i++)
{
printf("CP ");
}
..
..

This code prints "CP CP CP CP CP ". How did i get it without running in Dev C++ or Turbo C? Its all dry running..

To understand the dry running of this module(a part of programming code), we go step by step:
1. we see that initialization occurs of c as 0 i.e. c has an initial value of 0.

2. i has got no initial value at first. So we assume i has garbage value(default value always stored in any variable whose value can be got by printing it without any initialisation ... try it....).

3. when it comes to loop, i gets a value of 0 and a condition that i should be less than 5 and increment of i by 1(a++ increments only when a next gets operated and ++a increments then and there itself).

4.Now entering into the loop, we find "CP " being printed once and the exiting.

5. After exiting the first time, looping statement goes to the next value of increment which makes i =1 and increment is done for next time it gets the value.

6. Similar thing goes on till i=4 and on completion of the i=4 loop, when it comes to the looping statement again, i becomes 5 and condition kicks in.

7. The program halts and gets out of the loop.


This is how a dry run is carried out. every program has to be done dry-run for improvement in the logic.

3 comments: