Difference between revisions of "Nested loops"

From CompSciWiki
Jump to: navigation, search
Line 41: Line 41:
 
</pre>
 
</pre>
  
Another simple [[for]] loop that prints out the value of count, updates count and finishes by adding a new line once the loop is complete. This loop will run 3 times printing out:
+
Here is a simple [[for]] loop that prints out the value of count, updates count and finishes by adding a new line once the loop is complete. This loop will run 3 times printing out:
  
 
<pre>
 
<pre>
Line 49: Line 49:
  
 
===Outer Loop===
 
===Outer Loop===
We already know how a single [[for]] loop works so lets take the first section to start:
 
 
 
<pre>
 
<pre>
 
for(row = 0; row < 2; row++)
 
for(row = 0; row < 2; row++)
Line 58: Line 56:
 
</pre>
 
</pre>
  
This looks simple enough; whatever is inside the loop will be executed twice (in this example).
+
We already know that a [[for]] loop will perform the same task until its [[condition]] is met. In this example, the loop will execute the code inside it two times.
  
 
==Other Uses==
 
==Other Uses==

Revision as of 16:48, 19 March 2007

Introduction

A nested loop is simply just more than one loop inside another; this may sound complicated but after you start using nested loops it will seem like second nature. The best way to start learning about nested loops is to look at an example using matrices.

Matrix Example

If you have never encountered a matrix before you can think of it as rows and columns of numbers. Or you can think of a matrix as simply a table of rows and columns like so:

A simple matrix with two rows and three columns.

012
345

Now suppose you wanted to print that matrix out in a program by simply using loops. An easy solution would be to use double nested for loops such as:

int row, column;
int count = 0;

for(row = 0; row < 2; row++)
{
     for(column = 0; column < 3; column++)
     {
          System.out.print(count);
          count++;
     }
     System.out.println();
}

The best way to describe what is happening here is to break this code up into the two loops starting from the inside.

Inner Loop

for(column = 0; column < 3; column++)
{
     System.out.print(count);
     count++;
}
System.out.println();

Here is a simple for loop that prints out the value of count, updates count and finishes by adding a new line once the loop is complete. This loop will run 3 times printing out:

012

Outer Loop

for(row = 0; row < 2; row++)
{
 ...
}

We already know that a for loop will perform the same task until its condition is met. In this example, the loop will execute the code inside it two times.

Other Uses

Example Program

/**********************************
* NestedX:
* Prints out a fancy "ascii art"
* picture in the shape of an X where
* the user supplies the size of the X.
***********************************/

import javax.swing.*;

public class NestedX
{
	public static void main(String[] args)
	{
		int size = 0;
		int i, j;

		// Keep asking for input until it is greater or equal to 3.
		while(size < 3)
		{
			size = Integer.parseInt(JOptionPane.showInputDialog("Enter a number greater or equal to 3"));
		}

		for(i = 0; i < size; i++)
		{
			for (j = 0; j < size; j++)
			{
				if(i == j)
				{
					System.out.print("*");
				}
				else if(j == (size - (i + 1)))
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}
			}
			System.out.println();
		}

		System.out.println("\n~~~End of processing~~~");
		System.exit(0);

	}// end main
}// end NestedX


Back to Loops