Wednesday, February 23, 2011

Basic logics behind pattern printing in BlueJ - Chapter I

Basic logics behind pattern printing in BlueJ - Chapter I

It is observed that many students face problems regarding the logics behind pattern printing. I think everybody knows that to print pattern nested loops are required. So before we proceed for printing pattern, out first job will be to understand nested loop properly.

We know that combination of control statement and body of a loop is a single statement. Again loop body contains statement (s). So we can put loop(s) inside the body of another loop. This is nested loop.

The loop kept inside the body of another loop is called inner loop and the other is known as outer loop. It is the inner loop which does the printing job of any pattern and in which row the inner loop will work is determined by outer loop. In every pattern there are rows and columns. I think it is now clear which loop controls what. Inner loop controls the columns where the values are displayed.

The concept would more clear if we take the case of Excel sheet. In a sheet there are number of rows starting from 1,2,3…, but in every row the columns are fixed like ‘A’, ‘B’,…. And values are always written on columns. From the above, it can be said that rows changes but columns remains the same in every row.

In case of pattern, it is also a combination of rows and columns. Rows are changing where the columns are repeated. Let’s take an example to illustrate it better. A simple pattern is given below:

@@@@
@@@@
@@@@
@@@@

In the above patterns there are four rows and four columns in each row. The rows are controlled by outer loop, hence it should runs four times. As the prints are on columns and columns are controlled by inner loop, we can say that on each iteration of the outer loop, inner loop must run times. The loop will be like

for( int i=0; i< 4; i++)

{
for( int j=0; j< 4; j++)
{
System.out.print(“@”);
}
System.out.println();
}


The statement given below is the inner loop:
for( int j=0; j< 4; j++)
{
System.out.print(“@”);
}

When the outer loop ‘for( int i=0; i< 4; i++)’ runs for the first time, the inner loop catches the control and executes for four times printing the symbols in the fixed row ( determined by ‘i’) but in different columns. After the inner loop has executed for four times, the control goes out of its body and new line is feed. A full course iteration of the inner loop means one iteration of the outer loop. As the outer loop starts its second iteration again the inner loop catches the control and executes for another full course, means four times as defined. Ultimately the inner loop runs for total 16 times and the symbol is printed for 16 times. There are four rows in the pattern and the outer loop has executed for four times only feeding four rows.
The final coding for this program is

class Pat1
{
public void showPattern()
{
for( int i=0; i< 4; i++)

{
for( int j=0; j< 4; j++)
{
System.out.print(“@”);
}
System.out.println();
}
}
}


Please put your comments here so that I can understand whether we are moving in the right direction. Lots of patterns are to be discussed.

2 comments:

  1. Hello Java expert
    You are simply copying the content of another BlueJ tutorial site. Immediate remove your contents or give link to the original site otherwise I will simply report the matter to google.

    ReplyDelete
  2. code for printing such pattern:

    123
    456
    789

    ReplyDelete