Nested Loops

Nested loop means loop within a loop one is inner loop(inside the body) and other is outer loop(two loops).Inner loop is placed inside outer loop.


/*Histogram Printing Program*/

import javax.swing.*;

public class Histogram


     public static void main(String aao[])


          int nn[]={19,3,15,7,11,9,13,5,17,1};

          String outputt=" " ;


          outputt+="Element \t Value \t Histogram";


          for(int i=0;i<nn.length;i++)


                            outputt+= "\n" +i+ "\t" +nn[i]+ "\t";

                        for(int jj=1;jj<=nn[i];jj++)



          JTextArea outputtArea=new JTextArea(11,30);



         JOptionPane.showMessageDialog(null, outputtArea,

               "Histogram Printiing Program", JOptionPane.INFORMATION_MESSAGE);






In this application reads number from an array and graphs the information in the form of bar chart(or histogram)---each number is printed, then a bar consisting of that many asterisks(*)  is displayed beside the number. The nested for loop actually appends the bars to the String that will be displayed in JTextArea (JTextArea outputtArea) on a message dialog. Note the loop continuation condition of the inner for structure(jj<=nn[i]). Each time the inner for structure is reached, it counts from 1 to nn[i], thus using a value in array nn to determine the final value of the control variable jj and the number of asterisks(*) to display.


Nested For Loop

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @