DRY RUN IN PROGRAM
Dry run means you have to put the value and run it in your mind first by the help of copy and pen . For any program initialize the value and and try it to do first in copy or in your mind . Think what happened after a one point or a step . Dry run is not a different type of run in computer system or in a program , we have to just run it on the mind . Dry run is not only for pattern question . We should dry run each and every question so we can understand it much better than before. Dry run in java is similar to dry run in C++ or in any other programming language.
On every question we should dry run before the execution so we can understand how the execution done of any program. we can dry run any of code written in programming language either in java or C++.
Let's take an example of pattern type question .
Write a program in java to print a pattern .
*
**
***
****
*****
For this pattern a java program is written below.
Dry run for the given question will be like this
Lets take the value of n =5 , the for loop will execute <=n means < =5 . In a simple way the first loop will execute 5 times , and second loop will execute i times. Because the value of i initialize with 1 and ends with less than or equal to 5.
First loop for i=1 it checks 1<=5 // Condition true .
It will go to the second loop .
Again it checks for j=1 ; 1<=1 // Condition true. And it prints * . The value of j will now increase by 1 . The value of j=1 .
Again it checks the condition 2<=1 // Condition false . It comes out from the second loop . And cursor comes in Second line due to System.out.println(); Now the value of i will increase by 1 . It becomes 2 .
For i=2 ;2<=5 // Condition true.
It will go to second loop .
Again the value of j will starts from j=1 ; 1<=2 // Condition true . print * . The value of j will increased by 1 . And checks the condition .2<=2 // Condition true. print * . The value of j again increment by 1 , means j becomes 3 . 3<=2 // Condition false . It comes out from the second loop and comes in next line.
So far we have two stars . **
For i=3 ;3<=5 // Condition true.
It will go to second loop . First check 1<=3 // Condition true . print *
2<=3 // Condition true . print *
3<=3 // Condition true . print * , The value of j will increase by one and becomes 4 . It checks the condition 4<=3 // Condition false , It comes out from the second loop and comes in next line . So far we have three *** . The value of i increases by 1 .
For i=4 ; 4<=5 // Condition true .
It will go to the second loop . First check 1<=4 //Condition true . print *
The value of j increased by 1 , 2<=4 // Condition true . print *
The value of j increased by 1 , 3<=4 // Condition true . print *
The value of j increased by 1 , 4<=4 // Condition true . print *
The value of j again increased by 1 and becomes 5 , 5<=4 // Condition false . Comes out from the second loop and comes in next line . now there are four ****
the value of i is increased by one and becomes 5 .
For i=5 ; 5<=5 // Condition true .
It will go to second loop , and initialize the value of j from 1 .
1<=5 // Condition true , print * j increased by 1.
2<=5 // Condition true , print * j increased by 1.
3<=5 // Condition true , print * j increased by 1.
4<=5 // Condition true , print * j increased by 1 .
5<=5 // Condition true , print * j increased by 1 .
6<=5 // Condition false and it comes back from second loop and comes to next line .
Now there are five ***** . The value of i increased by 1 , and it becomes 6 . It checks the condition for i<=5;
For i =6 ;6<=5 // Condition false . The execution complete .
We have output like :
Dry run in java or Dry run in C ++ or in any other programming language is not different from each other . We have to just implement the values which is given . Try to dry run a program : print number from 1 to 10.