Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Nested For Loop [EXAMPLES!]

nested loop for loop loop

  • Please log in to reply
121 replies to this topic

#25 BenzR

BenzR

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 10 October 2010 - 04:42 AM

@BenzR: Please create a new thread in the Java Help section to get more help on this. You're hijacking a thread!


where is the help section  :confused:

Edited by dargueta, 28 April 2013 - 08:41 PM.

  • 0

#26 fazilaayisha

fazilaayisha

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 12 October 2010 - 07:37 AM

please help me solve this using nested for loops
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
  • 0

#27 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 12 October 2010 - 08:51 AM

int number = 15;

for(int i=0 ; i<5 ; i++){

  for(int j=0 ; j<5-i ; j++){

    System.out.print(number--);

  }

}

  • 0

#28 Majestic

Majestic

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 19 October 2010 - 12:17 AM

i am a rookie from java...i find it is quite hard for me:crying:
i wanna know how to print out like this?

88888881
88888812
88888123
88881234
88812345
88123456
81234567
12345678

could someone tell me the solution?:crying:


{
		int n = 8; int i; int j; 
		for (i=1;i<=n;i++) 
		{ 
			for (j=n;j>i;j--) 
				{ 
				System.out.print(n); 
				}
		
			for( j=1; j<=i;j++) 
			 { 
				System.out.print(j); 
			 }
		
			System.out.println();
		}
	} 

  • 1

#29 fyhring4

fyhring4

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 24 October 2010 - 03:24 AM

Thx this is really awesome!
  • 0

#30 jun71178

jun71178

    CC Newcomer

  • Just Joined
  • PipPip
  • 17 posts

Posted 26 October 2010 - 06:39 AM

for you asma........ here is your request...

public class asma
{
    public static void main(String [] args)
    {
        for (int i=1; i<=4; i++)
        {
            
            for (int j=4; j>=i; j--)
            {
                System.out.print(i);
            }
        
      System.out.println(j);
    }
}

Edited by Alexander, 26 October 2010 - 09:49 AM.
Added [code] tags

  • 0

#31 dandi

dandi

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 31 October 2010 - 05:09 PM

City Map Block Builder

* if user input less than 1, will be defined as zero (0)

* user input the matrices

* Image of block :
  +---+
 /   /|
+---+ |
|   | +
|   |/
+---+

* Sample Input:

row : 3
column : 4

map matrices :
1 2 1 5
1 4 2 1
2 1 1 1

Result :
..................+---+
................./   /|
................+---+ |
................|   | +
................|   |/|
........+---+...+---+ |
......./   /|...|   | +
......+---+ |...|   |/|
......|   | +...+---+ |
......|   |/|-+.|   | +
......+---+ |/|.|   |/|
......|   | +---+---+ |
......|   |/   /|   | +
..+---+---+---+ |   |/|
./   /|   |   | +---+ |
+---+ |   |   |/   /| +
|   | +---+---+---+ |/.
|   |/   /   /   /| +..
+---+---+---+---+ |/...
|   |   |   |   | +....
|   |   |   |   |/.....
+---+---+---+---+......

how to make this :confused:
  • 0

#32 siva

siva

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 28 November 2010 - 05:13 PM

how to make

1234
-234
--34
---4
  • 0

#33 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 29 November 2010 - 07:50 AM

Solution for every loop your teacher is bothering you with.

It always consists of 1 outer for-loop with 1 or more inner for-loops.
The outer for-loop ALWAYS is the amount of rows you need. The inner loop will use System.out.print(); to print values.

So, let's take this sample:
88888881 
88888812 
88888123
88881234
88812345
88123456
81234567
12345678
The first things you want to do is count the number of rows: 8
Then you write this:
for( int i=0 ; i<[B][SIZE="4"]8[/SIZE][/B] ; i++){
  System.out.println("");
}
the println() will just make an "enter" / a newline.
The next thing you want to do is take the output and split the "equal stuff", okay badly explained but i hope the example helps:
8888888
888888
88888
8888
888
88
8

AND
1
12
123
1234
12345
123456
1234567
12345678
There are 2 things there. We start with the eights:
Now ask yourself how often it must print a value. You must find the link between the outer loop and the amount of times it must be printed.
Counter of the outer loop: i
i   #times to be printed
0         7
1         6
2         5
3         4
4         3
5         2
6         1
7         0
I hope everyone sees that #times to be printed is 7-i. If you don't see this, I suggest you go get some extra math classes.
7-i is important. because that's the link between the inner loop and outer loop.
The default for-loop is: for(int x=0 ; x<condition ; x++){}. Where x is a letter that's not used yet.

The condition to use in the default for-loop is 7-i as we've just found.
So put 7-1 as the condition (and I take the letter 'j' instead of 'x'):
This results is for(int j=0 ; j<7-i; j++){}
Because the value that is printed is constant, you are now able to add the 2nd loop to the code:
for( int i=0 ; i<8 ; i++){
  for( int j=0 ; j<7-i ; j++ ) {
     System.out.print("8");
  }
  System.out.println("");
}
Note it's a .print() in there, and no .printLN() we don't want newlines after every 8.

Now we need
1
12
123
1234
12345
123456
1234567
12345678

Counter of the outer loop: i
i   #times to be printed
0           1
1           2
2           3
3           4
4           5
5           6
6           7
7           8
IF you don't see here that #times to be printed = i+1, you really really need to take those math classes.
So we take our default for-loop again: for(int x=0 ; x<condition ; x++){}
condition = i+1
So fill in the condition (and I took letter 'k' instead of 'x' now)
for(int k=0 ; k<i+1; k++){
  System.out.print("?");
}
Unlike the previous loop the value that's printed is not constant. We need to figure out what it depends on.

Counter of the inner loop: k
i	k   #value to be printed
0	0           1
0	1           2
0	2           3
0	3           4
0	4           5
0	5           6
0	6           7
0	7           8
1       0           1
1       1           2
...
You could complete the table there but i hope it's allready clear.
Here we see we must print 'k+1', again if you fail to see this -->you really really need to take those math classes.
Now we can modify our last loop:
for(int k=0 ; k<i+1; k++){
  System.out.print([B]k+1[/B]);
}

And put it with the rest of the code in 1 piece:
    for( int i=0 ; i<8 ; i++){
      for( int j=0 ; j<7-i ; j++ ) {
        System.out.print("8");
      }
      for(int k=0 ; k<i+1; k++){
        System.out.print(k+1); 
      }
      System.out.println("");
    }

OUTPUT
------
88888881
88888812
88888123
88881234
88812345
88123456
81234567
12345678


Now let's take another example
15 14 13 12 11
10 9 8 7 
6 5 4
3 2 
1
Amount of rows:5
for(int i=0 ; i<5 ; i++){
  System.out.println("");
}
First look at the amount of times a value has to be printed:
i   #times to be printed
0           5
1           4
2           3
3           2
4           1
--> "#times to be printed = 5-i" if you fail to see this ... you know..
for(int x=0 ; x<condition ; x++){}
"5-i" = condition
result:
for(int j=0 ; j<5-i ; j++){
  System.out.print("?");
}
Now the value that needs to be printed
Counter of the outer loop: i
Counter of the inner loop: k
i    k   #value to be printed
0    0           15                
0    1           14               
0    2           13            
0    3           12                         
0    4           11
---next loop---
1    0           10
1    1           9
1    2           8
1    3           7
---next loop---
2    0           6
2    1           5
2    2           4
---next loop---
3    0           3
3    1           2
---next loop---
4    0           1
Here the value that has to be printed has nothing to do with anything. It's just a number 15 that's lowered by 1 every loop.
So create a number 15 OUTSIDE all loops.
Print and lower this number every loop
int number = 15;
-----above all loops-----

-----inside loop-----
for(int j=0 ; j<5-i ; j++){
  System.out.print(number + " ");
  number = number -1;
}
(don't forget to print the space)
Final result:
int number=15;
    for(int i=0 ; i<5 ; i++){
      for(int j=0 ; j<5-i ; j++){
        System.out.print(number +" ");
        number = number -1;
      }
      System.out.println("");
    }

OUTPUT
------
15 14 13 12 11 
10 9 8 7 
6 5 4 
3 2 
1 

Are we done yet? ** NO! another short example:
1
22
333
4444
55555
Amount of rows: 5
for(int i=0 ; i<5 ; i++){
  System.out.println("");
}
How many times is it printed each row?
0    1
1    2
2    3
3    4
4    5
So this is i+1 = condition
fill in condition in for(int x=0 ; x<condition ; x++){}
for(int j=0 ; j<i+1 ; j++){
  System.out.print("?");
}
What values are printed each row?
0    1
1    2
2    3
3    4
4    5
so this is also i+1
adapt loop:
for(int j=0 ; j<i+1 ; j++){
  System.out.print(i+1);
}
Put into outer loop:
for(int i=0 ; i<5 ; i++){
  for(int j=0 ; j<i+1 ; j++){
    System.out.print(i+1);
  }
  System.out.println("");
}
TADAAA, done.

Recap
most of the times:
  • Amount of rows -> outer loop
  • How many times per loop? --> inner loop condition
  • What has to be printed --> in the System.out.print(<--->);


Now I don't wanna see requests here with no code. At least the outer loop is EASY.

Edited by wim DC, 29 November 2010 - 09:01 AM.

  • 2

#34 siva

siva

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 30 November 2010 - 12:54 PM

I need help on how to write a method that will return the reverse of an inputted word
public String reverse (String word)
{

}
  • 0

#35 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 01 December 2010 - 10:47 AM

I need help on how to write a method that will return the reverse of an inputted word
public String reverse (String word)
{

}

That's very simple because the StringBuilder class has a .reverse() method.
StringBuilder sb = new StringBuilder("MyWord");
String reverse = sb.reverse().toString();
System.out.println(reverse);


---output---
droWyM
Didn't really test this code but i suppose it's correct.
  • 1

#36 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 01 December 2010 - 12:26 PM

Nice compilation oxano, I was like "Woah", wonder if this should be a new version of this outdated tutorial :)
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.






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