Jump to content




Recent Status Updates

  • Photo
      15 Nov
    duzamucha

    Hi, I am final year Interior Design Student from University of Huddersfield. I am currently working on my final major project which is going to be linked to coding. I was hoping that you could help me with my research. I have prepared a short survey, it would be a massive help if you could fill it in for me. It takes less than 2 minutes to complete, I promise. Here is the link: https://www.surveymonkey.com/s/73XLJKK Thank you so much in advance!

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

Nested For Loop [EXAMPLES!]

nested loop for loop loop

  • Please log in to reply
116 replies to this topic

#1 ALPHA

ALPHA

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 23 October 2008 - 02:32 PM

Hi,
Here is a collection of few examples for NESTED FOR LOOP.
Simple codes in how to use nested loops,
There is only the code, no explanation (not good at explaining anything:D)
So, just try to understand the program
Hope this will help as much people as possible
Let's Stop talking and just begin

OK
Here we go!

1.Get the following output :
a.
1
12
123
1234
12345
123456
1234567
12345678
123456789
 

//Numbers Ladder
//Nested Loops
//By ALPHA
//On 22 Oct 2008
//www.CodeCall.net
//----------------------
public class testFor
{
public static void main(String [] args)
{
for (int i=1; i<=9; i++)
{
System.out.println();
for (int j=1; j<=i; j++)
{
System.out.print(j);
}
}
System.out.println();
}
}


b.

*
**
***
****
*****
******
*******
********
*********
(just bring it to the LEFT side just like it is)

 

Star Pattern using nested for loop

 

 

//Nested Loops
//star ladder
//www.CodeCall.net
//By ALPHA
//22 Oct 2008
//--------------
public class starForTest
{
public static void main(String [] args)
{
for (int i=1; i<=9; i++)
{
System.out.println();
for (int j=9; j>=i; j--)
{
System.out.print(" ");
}
for (int k=1; k<=i; k++)
{
System.out.print("*");
}
}
System.out.println();
}
}


2.A program for sorting an array elements (ascending sort)
 

//Nested For Loop
//Sorting elements of an Array
//By ALPHA
//23 Oct 2008
//www.CodeCall.net
//------------------------
public class arrSort
{
public static void main(String args [])
{
int num[]=new int[] {20,50,10,70,40,80,30,90,60};
int t=0;

for (int i=0; i<num.length; i++)
{
for (int j=i+1; j<num.length; j++)
if (num[i]>num[j])
{
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
System.out.println("Sorting elements in Ascending Order :\n");
for (int i=0; i<num.length; i++)
System.out.println(num[i]);
}
}


3.Program to create a Two-Dimensional Array
 

//Two Dimensional Array
//Nested Loops
//By ALPHA
//On 23 Oct 2008
//www.CodeCall.net
//-------------------------------
public class twoDimArray
{
public static void main(String [] args)
{
String city[][]={ {"NewYork","Muscat","London"} , {"Cairo","Beijing","CapeTown"} };
/* Note : hope u already know why
* i used Double Qoutes( " ) when
* initializing the Array elements
*/
for (int i=0; i<city.length; i++)
{
for (int j=0; j<city[i].length; j++)
//Note:we could use 2 and 3 instead of "city.length" and "city[i].length
//'Cause we already know that we have 2 Rows and 3 Columns ^_^
{
System.out.print(city[i][j]+"\t");
}
System.out.println();
}
}
}

 

Java - Take For Loop Variables And Put Into 2 Separate Array


4.Program to create multiplication table
 

//Multiplication Table
//Using Nested Loops
//By ALPHA
//23 October 2008
//www[dot]CodeCall[dot]net
//-----------------------------------
/*Important Note : this program includes Parts that
* are ONLY for the shape of the OUTPUT
* it will NOT be an ERROR if you
* missed those Parts but, as said it's only for
* the shape of the output
*/
//------------------------------------
public class multiplicationTable
{
public static void main(String args[])
{
System.out.print(" ");

for (int i=1; i<=9; i++)
System.out.print(" "+i);

System.out.println();
System.out.println("-------------------------------------------------------");

for (int i=1; i<=9; i++)
{
System.out.print(i+"|");
for (int j=1; j<=9; j++)
{
if (i*j<10)
System.out.print(" "+i*j);
else
System.out.print(" "+i*j);
}
System.out.println();
}
}
}


That's all I have for nested loops
If anyone has any other ideas, PLEASE post it !

c u :)
ALPHA

 

Looking for more tutorials on loops?

Control Statements

Java:Tutorial - The Loop

Java - Take For Loop Variables And Put Into 2 Separate Array


Edited by Roger, 25 February 2013 - 05:06 PM.

  • 1

#2 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 23 October 2008 - 10:06 PM

Great stuff,however you should post this in the java tutorials


Edited by dargueta, 28 April 2013 - 08:39 PM.
Cleaned up post quote

  • 0

#3 ALPHA

ALPHA

    CC Newcomer

  • Just Joined
  • PipPip
  • 15 posts

Posted 24 October 2008 - 08:12 AM

sorry guys,
i thought it would be better to post it there
anyway, thanks for moving it to the right section ^^

and by the way, nice sig turk4n ^^
  • 0

#4 chabielita

chabielita

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 11 February 2010 - 03:25 AM

whoow.. nice codes.. you gave me an idea for our exam.. hihihi..:D

well since this area are for NESTED for LOOP examples.. just wanna share
my simple codes.. :rolleyes:



public class ForLoop1{
public static void main(String[]args){

int count=0;

	for(count=0;count<13;count++)
	{
	System.out.print("*");
	}

int count1=0;
	
	for(count1=0;count1<7;count1++)
	{
	
	System.out.println("\t\t");
	System.out.println("\t\t*");
	}
}
}



Output:

*************
*
*
*
*
*
*
*


hope this codes helps addens dis collection... :love:
  • 0

#5 Ranilplus

Ranilplus

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 07 May 2010 - 09:42 PM

how to
999999999
_88888888
__7777777
___666666
____55555
_____4444
______333
_______22
________1
  • 0

#6 Sinipull

Sinipull

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 384 posts

Posted 08 May 2010 - 01:08 AM


        for(int i = 9; i > 0; i--){

            for(int j = i; j < 9; j++){

                System.out.print("_");

            }

            for(int j = i; j > 0; j--){

                System.out.print(i);

            }

            System.out.println();

        }


  • 0

#7 asma

asma

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 19 May 2010 - 09:21 AM

how to do this??
1234
123
12
1
please help me....
  • 0

#8 GMVResources

GMVResources

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 71 posts

Posted 13 June 2010 - 07:23 PM

Nice loops you guys! =D
  • 0

#9 AlexIsOneCoolGuy

AlexIsOneCoolGuy

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 23 June 2010 - 04:09 PM

Nested loops for Objective-C.
#import <Foundation/Foundation.h>


int main (int argc, const char * argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	

	int i,j;
	
	// loop for top pyramid of the diamond
	
	for (i = 6; i > 0; i--) {		
		
		for (j = i; j > 0; j--) {	
			printf("-");
			
		}
		
		for (j = i; j < 6; j++) {	
			printf("*");
		}
		
		for (j = i; j <= 6; j++) {	
			printf("*");
		}
		
		for (j = i; j > 0; j--) {
			printf("-");
		}
		
		
		printf("\n");				
		
		
	}

// loop for bottom pyramid of the diamond
	
	for (i = 6; i >= 0; i--) {
		for (j = i; j < 6; j++) {
			printf("-");
		}
		
		for (j = i; j > 0; j--) {
			printf("*");
		}
		
		for (j = i; j >= 0; j--) {
			printf("*");
		}
		
		for (j = i; j < 6; j++) {
			printf("-");
		}
		
	
		
		printf("\n");
		
	}
	
	
	[pool drain];
	return 0;

}




/*
 Program output:
 ------*------
 -----***-----
 ----*****----
 ---*******---
 --*********--
 -***********-
 *************
 -***********-
 --*********--
 ---*******---
 ----*****----
 -----***-----
 ------*------
 
 */


Edited by AlexIsOneCoolGuy, 23 June 2010 - 04:13 PM.
previous comments made the code unreadable, so I deleted them.

  • 0

#10 gaurav_rana

gaurav_rana

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 02 August 2010 - 12:00 PM

hi ,
i am having difficulty in writing a program with output

1
22
333
4444
55555

please help me
  • 1

#11 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,088 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 02 August 2010 - 12:50 PM

One loop to count rows and the column loop does as many iterations as current row (size) is.
for (i = 1; i <= 5; i++) {
    for (j = 0; j < i; j++) {
        output i;
    }
    output new line;
}

  • 0

The roots of education are bitter, but the fruit is sweet.


#12 Roman Y

Roman Y

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 196 posts

Posted 03 August 2010 - 12:37 AM

how to do this??
1234
123
12
1
please help me....


Here you go. Not that different from the examples it's just that you start with higher "i" and decreasing it instead:
for (int i = 4; i > 0; i--)
		{
			for (int j = 1; j <= i; j++)
			{
				System.out.print(j);
			}
			System.out.println();
		}

  • 0





Also tagged with one or more of these keywords: nested loop, for loop, loop

Powered by binpress