Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java Nested Loop

nested loop loop

  • Please log in to reply
5 replies to this topic

#1 killerpl

killerpl

    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Programming Language:C
  • Learning:C

Posted 26 May 2012 - 07:32 PM

pls somebody hlp me!
how do make output as:

123456
12345
1234
123
12
1
i can do this by myself, but below:

123456
12345
1234
123
12
1
how to do that?
  • 0

#2 tavichh

tavichh

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 399 posts
  • Location:Delaware
  • Programming Language:C#
  • Learning:C++, PHP, JavaScript

Posted 26 May 2012 - 07:57 PM

I do not see any difference?
  • 0
Send a like my way if I helped you or if you found my post useful or even just enjoyed reading my post.
Thank you :)

#3 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 27 May 2012 - 12:25 AM

pls somebody hlp me!
how do make output as:

123456
12345
1234
123
12
1
i can do this by myself, but below:

123456
12345
1234
123
12
1
how to do that?


Can you please post a snippet of code of your progress? We want everyone here to provide some of their effort so we can try to help them
fix the problem. You can do the first but you can't do the second output? Is that what you mean? :worry:


I do not see any difference?


There is difference on his outputs.. try to quote his post and you will see that the first output doesn't have indention and the second one has :)
maybe a bug in the forum?
  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#4 wim DC

wim DC

    Roar

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

Posted 27 May 2012 - 03:05 AM

There is difference on his outputs.. try to quote his post and you will see that the first output doesn't have indention and the second one has :)
maybe a bug in the forum?

This is no bug. Trailing spaces are always trimmed unless in CODE tags.
  • 1

#5 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 27 May 2012 - 11:21 AM

pls somebody hlp me!
how do make output as:

123456
12345
1234
123
12
1
i can do this by myself, but below:

123456
  12345
	1234
	  123
		12
		  1
I used code tags to preserve the leading spaces,but the formatting still is not right.

Look at the number of leading spaces on each line. Line 0 has none, line 1 has one etc
  • 0

#6 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 29 May 2012 - 06:15 AM

The answer is this: You're going to have to use 2 separate inner loops for the 2nd one (with leading spaces). The outer loop controls the decrementing limit as before, but the first inner loop prints a number of spaces equivalent to the difference between the overall limit and the current limit. Then, the 2nd inner loop prints the increasing numbers up to the current limit, exactly as your first example without the leading spaces.

This is an example of what this nested structure looks like:

for (outer loop conditions) {

    for (first inner loop conditions) {
        // print a space
    }

    for (second inner loop conditions) {
        // print a number
    }

    // print a newline

}

  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download