Sunday, March 13, 2011

Use Of Break Statement In BlueJ Programs

In my earlier post I have given the definition of break and few programs on break statement. On many occations I have found that students are not using break properly in their programs. One thing must be clear to all that break is an important statement and it should be used whenever required. When a program is designed, there are many factors which are to be kept in mind like time complexity, space complexity and logical correctness of the program.Now we will see different uses of break in programs and how it helps to reduce time complexity of a program and at the same time the program becomes logically correct.

Use of break in prime number checking program 



I have seen many programs on prime number checking like this


import java.io.*;
class Prime
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int i,n,c=0;
 public void showPrime() throws Exception
 {
  System.out.println("Enter the number:");
  n=Integer.parseInt(br.readLine());
  for(i=1;i< =n/2;i++)
  {
   if(n%i==0)
   c++;
   }
   if(c==2)
   System.out.println("Number is Prime.");
   else
  System.out.println("Number is not Prime.");
 System.out.println("The loop has executed for "+ i+ " times to check prime or not.");
  }
  public static void main(String args[]) throws Exception
  {
   Prime ob=new Prime();
   ob.showPrime();
 }
 }

This above program will display correct result on execution but is it logically correct? There is a statement like ‘System.out.println("The loop has executed for "+ i+ " times to check prime or not.");’ in our above program. If your entered value is 200, it would display that ‘Number is not prime’ and ‘The loop has executed for 100 times to check prime or not’ and if it is 19 then the display will be ‘Number is prime’ and ‘The loop has executed for 9 times to check prime or not’. The second case is ok, but whats about the first case? For 200, the conclusion that the number is not prime can be reached much earlier, no need to execute the loop for 100 times.

Look at the other program on prime number checking using break statement.

import java.io.*;
class Prime
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int i,n;
 public void showPrime() throws Exception
 {
  System.out.println("Enter the number:");
  n=Integer.parseInt(br.readLine());
  for(i=2;i< n/2;i++)
  {
   if(n%i==0)
   break;
   }
   if(i==n)
   System.out.println("Number is Prime.");
   else
  System.out.println("Number is not Prime.");
  System.out.println("The loop has executed for "+ (i-1)+ " times to check prime or not.");
  }
  public static void main(String args[]) throws Exception
  {
   Prime ob=new Prime();
   ob.showPrime();
 }
 }

Here the loop starts its execution from  2 and end its execution on the half value of the number as the above program. Inside the loop whenever the number is found to be completely divided by any number, the loop is terminated using break statement because if a number is completely divided by any number between 2 and half value of the number, there is no need to continue the loop. For example if the number is 200, the first iteration of the loop is enough to display the result.

Use of break in unique value searching program

I have seen programs like this when searing any unique value using linear search program.

import java.io.*;
class Search
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int i,val,arr[],n,m;
 boolean bool;
 public void show() throws Exception
 {
  System.out.println("How many values to store:");
  n=Integer.parseInt(br.readLine());
  arr=new int[n];
  for(i=0;i< n;i++)
  {
   System.out.println("Enter value:");
   arr[i]=Integer.parseInt(br.readLine());
   }
  
  System.out.println("Enter the value to search:");
  m=Integer.parseInt(br.readLine());
  for(i=0;i< n;i++)
  {
   if(m==arr[i])
   bool=true;
   }
   if(bool==true)
   System.out.println("Value found and it took "+i + " iterations to display the result.");
  else
  System.out.println("Value not found.");
  }
  public static void main(String args[]) throws Exception
  {
   Search ob=new Search();
   ob.show();
 }
 }

In this above program, the loop executes the full course even if the value is found.

Using break statement the program will be like

import java.io.*;
class Search
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int i,val,arr[],n,m;
 public void show() throws Exception
 {
  System.out.println("How many values to store:");
  n=Integer.parseInt(br.readLine());
  arr=new int[n];
  for(i=0;i< n;i++)
  {
   System.out.println("Enter value:");
   arr[i]=Integer.parseInt(br.readLine());
   }
  
  System.out.println("Enter the value to search:");
  m=Integer.parseInt(br.readLine());
  for(i=0;i< n;i++)
  {
   if(m==arr[i])
    break;
   }
   if(i!=n)
   System.out.println("Value found and it took "+i + " iterations to display the result.");
  else
  System.out.println("Value not found.");
  }
  public static void main(String args[]) throws Exception
  {
   Search ob=new Search();
   ob.show();
 }
 }

The loop is terminated here in the above program whenever the value is found and thus unnecessary execution of the loop can be avoided.

Use of break in bubble sort program

If we compare bubble sort with selection sort technique, the first and most important point is that in bubble sort we can terminate the process of sorting whenever the values are arranged which is not possible in selection sort technique. This termination of loop execution is accomplished using break statement in the outer loop of bubble sort. If we do not use break statement in bubble sort, it would be same as selection sort. Selection sort technique continues So we can say that bubble sort without break statement is a crime from programming point of view.

Note the program below


import java.io.*;
class BubbleSort
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int i,j,temp,arr[],n;
 public void show() throws Exception
 {
  System.out.println("How many values to store:");
  n=Integer.parseInt(br.readLine());
  arr=new int[n];
  for(i=0;i< n;i++)
  {
   System.out.println("Enter value:");
   arr[i]=Integer.parseInt(br.readLine());
   }
   for(i=0;i< n;i++)
   {
    for(j=0;j< n-i-1;j++)
    {
     if(arr[j]>arr[j+1])
     {
      temp=arr[j];
      arr[j]=arr[j+1];
      arr[j+1]=temp;
     }
     }
     }
  System.out.println("Sorted values:");
 for(i=0;i< n;i++)
  System.out.print(arr[i]+" ");
  }
  public static void main(String args[]) throws Exception
  {
   BubbleSort ob=new BubbleSort();
   ob.show();
 }
 }


This above program continues the loop even after the elements are sorted.

Note the program using break statement in bubble sort

import java.io.*;
class BubbleSort
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 int i,j,flag,temp,arr[],n;
 public void show() throws Exception
 {
  System.out.println("How many values to store:");
  n=Integer.parseInt(br.readLine());
  arr=new int[n];
  for(i=0;i< n;i++)
  {
   System.out.println("Enter value:");
   arr[i]=Integer.parseInt(br.readLine());
   }
   for(i=0;i< n;i++)
   {
   flag=0;
    for(j=0;j< n-i-1;j++)
    {
     if(arr[j] >arr[j+1])
     {
     flag=1;
      temp=arr[j];
      arr[j]=arr[j+1];
      arr[j+1]=temp;
     }
     }
     if(flag==0)
     break;
     }
     System.out.println("It took " + i + " iterations of outer loop to sort the elements:");
  System.out.println("Sorted values:");
 for(i=0;i< n;i++)
  System.out.print(arr[i]+" ");
  }
  public static void main(String args[]) throws Exception
  {
   BubbleSort ob=new BubbleSort();
   ob.show();
 }
 }

The above program shows that as bubble sort compares consecutive values, if there is no exchange of values in any full course iteration of the inner loop, the outer loop is terminated using break as there is no need to continue the loop.

Related PostBlueJ Programs on Number

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner