Wednesday, December 15, 2010

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.

Related PostBlueJ Programs On Pattern

6 comments:

  1. *********
    ****_****
    ***___***
    **_____**
    *_______*
    *_______*
    **_____**
    ***___***
    ****_****
    *********
    (_) are spaces

    ReplyDelete
  2. Here is the program


    class Pattern
    {
    public void show()
    {
    int i,j,m=1,m1=1,c=1,c1=7;
    for(i=0;i< 10;i++)
    {
    if(i==0 || i==9)
    {
    for(j=0; j< 9; j++)
    System.out.print("*");
    }
    else if(i<10/2)
    {
    m=(10-c)/2;

    for(j=0;j<m;j++)
    System.out.print("*");
    for(j=0;j<c;j++)
    System.out.print(" ");
    for(j=0;j<m;j++)
    System.out.print("*");
    c=c+2;
    }
    else
    {
    for(j=0;j<m1;j++)
    System.out.print("*");
    for(j=0;j<c1;j++)
    System.out.print(" ");
    for(j=0;j<m1;j++)
    System.out.print("*");
    c1=c1-2;
    m1++;
    }
    System.out.println();

    }
    }
    public static void main(String args[])
    {
    new Pattern().show();
    }
    }

    ReplyDelete
  3. hey that should not be pattern it should be a

    ReplyDelete
    Replies
    1. Please complete your sentence. What you are trying to say is not clear.

      Delete
  4. Sir any last minute suggestions for the isc practical 2012?

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner