Nested for loops

Discussion in 'Java' started by David, Feb 17, 2008.

  1. David

    David Guest

    Hi all, I'm new to java and google groups alike. I needed some help
    on creating the following out put.

    1
    22
    333
    4444
    55555
    666666
    7777777
    88888888
    999999999


    I've seen it done in class but can't seem to remember. There were
    only two for loops in the code. Can someone help me with the code?
     
    David, Feb 17, 2008
    #1
    1. Advertising

  2. David

    Christian Guest

    David schrieb:
    > Hi all, I'm new to java and google groups alike. I needed some help
    > on creating the following out put.
    >
    > 1
    > 22
    > 333
    > 4444
    > 55555
    > 666666
    > 7777777
    > 88888888
    > 999999999
    >
    >
    > I've seen it done in class but can't seem to remember. There were
    > only two for loops in the code. Can someone help me with the code?
    >


    Homework is there to be done by yourself. It is to teach you how to code
    java. If you don't do it yourself your course will have zero effect.

    Christian
     
    Christian, Feb 17, 2008
    #2
    1. Advertising

  3. David

    David Guest

    On Feb 16, 8:25 pm, Christian <> wrote:
    > David schrieb:
    >
    >
    >
    >
    >
    > > Hi all, I'm new to java and google groups alike.  I needed some help
    > > on creating the following out put.

    >
    > > 1
    > > 22
    > > 333
    > > 4444
    > > 55555
    > > 666666
    > > 7777777
    > > 88888888
    > > 999999999

    >
    > > I've seen it done in class but can't seem to remember.  There were
    > > only two for loops in the code.  Can someone help me with the code?

    >
    > Homework is there to be done by yourself. It is to teach you how to code
    > java. If you don't do it yourself your course will have zero effect.
    >
    > Christian- Hide quoted text -
    >
    > - Show quoted text


    Good advice Christian, however this is not homework. it's just a new
    concept that was introduced yesterday Friday and I didn't have a very
    good grasp on it. I wanted to use this weekend to practice the heck
    out of it but I couldn't start practicing because the basic structure
    eluded me.

    I could wait until Monday to ask the teacher but I'd rather be
    familier with it now and concentrate on Monday's teaching on Monday.

    How long have you been programming? Are you a serious programmer or
    just for fun?
     
    David, Feb 17, 2008
    #3
  4. David

    Peter Duniho Guest

    On Sat, 16 Feb 2008 17:16:55 -0800, David <> wrote:

    > Hi all, I'm new to java and google groups alike. I needed some help
    > on creating the following out put.
    >
    > 1
    > 22
    > 333
    > 4444
    > 55555
    > 666666
    > 7777777
    > 88888888
    > 999999999
    >
    >
    > I've seen it done in class but can't seem to remember. There were
    > only two for loops in the code. Can someone help me with the code?


    It's good you're not asking people to do your homework.

    That said, it may be more helpful to you, learning-wise, to be given a
    general description of what you're looking for, and let you work the
    details out on your own. So...

    In the output you're looking at, note that there are two things that are
    changing: the digit being displayed, and the length of the string being
    displayed.

    Note also that you even recall there were two loops. So, perhaps one loop
    changes the digit, while the other loop controls the length of the string.

    Finally, note that the length appears to be related directly to the
    digit. In other words, the loop controlling the length of the string may
    (i.e. will :) ) use the digit controlled by the one loop as its own
    input. In this way, the loop controlling the digit also winds up
    indirectly controlling the length of the string.

    Here's a simple loop that changes the value of an integer from 1 to 9:

    for (int i = 1; i <= 9; i++) { /* do something */ }

    Here's a simple loop that writes a fixed number of strings ("1" to "10")
    to the standard output:

    for (int j = 1; j <= 10; j++) { System.out.printf("%d", j); }

    Finally, note that while I have used constants in the loop expressions,
    anywhere there's a hard-coded literal integer, you could instead put an
    integer variable.

    All of the above are the building blocks you need to answer your
    question. You should now try on your own to assemble them in a useful way.

    Finally, while it's not quite as bad to post questions like this one as
    compared to asking for help with your homework, I would say that a
    newsgroup not specifically intended for learning how to program is
    probably not the best place for advice related to basic programming
    techniques. Ideally, this is something better handled by contacting your
    teacher and/or classmates for help, but if not, I suspect there are forums
    out there that are specifically intended for helping people new to
    programming.

    I can't speak for the others reading this newsgroup, but while I don't
    object to the occasional "I don't know how to program, please help"
    question, it's not really something that should come up a lot in a
    newsgroup like this one. That sort of thing can really reduce the
    signal-to-noise ratio for the newsgroup if it gets to be commonplace.

    Pete
     
    Peter Duniho, Feb 17, 2008
    #4
  5. David

    David Guest

    On Feb 16, 8:53 pm, "Peter Duniho" <>
    wrote:
    > On Sat, 16 Feb 2008 17:16:55 -0800, David <> wrote:
    > > Hi all, I'm new to java and google groups alike.  I needed some help
    > > on creating the following out put.

    >
    > > 1
    > > 22
    > > 333
    > > 4444
    > > 55555
    > > 666666
    > > 7777777
    > > 88888888
    > > 999999999

    >
    > > I've seen it done in class but can't seem to remember.  There were
    > > only two for loops in the code.  Can someone help me with the code?

    >
    > It's good you're not asking people to do your homework.
    >
    > That said, it may be more helpful to you, learning-wise, to be given a  
    > general description of what you're looking for, and let you work the  
    > details out on your own.  So...
    >
    > In the output you're looking at, note that there are two things that are  
    > changing: the digit being displayed, and the length of the string being  
    > displayed.
    >
    > Note also that you even recall there were two loops.  So, perhaps one loop  
    > changes the digit, while the other loop controls the length of the string.
    >
    > Finally, note that the length appears to be related directly to the  
    > digit.  In other words, the loop controlling the length of the string may  
    > (i.e. will :) ) use the digit controlled by the one loop as its own  
    > input.  In this way, the loop controlling the digit also winds up  
    > indirectly controlling the length of the string.
    >
    > Here's a simple loop that changes the value of an integer from 1 to 9:
    >
    >      for (int i = 1; i <= 9; i++) { /* do something */ }
    >
    > Here's a simple loop that writes a fixed number of strings ("1" to "10")  
    > to the standard output:
    >
    >      for (int j = 1; j <= 10; j++) { System.out.printf("%d", j); }
    >
    > Finally, note that while I have used constants in the loop expressions,  
    > anywhere there's a hard-coded literal integer, you could instead put an  
    > integer variable.
    >
    > All of the above are the building blocks you need to answer your  
    > question.  You should now try on your own to assemble them in a useful way.
    >
    > Finally, while it's not quite as bad to post questions like this one as  
    > compared to asking for help with your homework, I would say that a  
    > newsgroup not specifically intended for learning how to program is  
    > probably not the best place for advice related to basic programming  
    > techniques.  Ideally, this is something better handled by contacting your  
    > teacher and/or classmates for help, but if not, I suspect there are forums  
    > out there that are specifically intended for helping people new to  
    > programming.
    >
    > I can't speak for the others reading this newsgroup, but while I don't  
    > object to the occasional "I don't know how to program, please help"  
    > question, it's not really something that should come up a lot in a  
    > newsgroup like this one.  That sort of thing can really reduce the  
    > signal-to-noise ratio for the newsgroup if it gets to be commonplace.
    >
    > Pete- Hide quoted text -
    >
    > - Show quoted text -


    I like your style Pete, thank you very much.
     
    David, Feb 17, 2008
    #5
  6. David

    David Guest

    On Feb 16, 8:53 pm, "Peter Duniho" <>
    wrote:
    > On Sat, 16 Feb 2008 17:16:55 -0800, David <> wrote:
    > > Hi all, I'm new to java and google groups alike.  I needed some help
    > > on creating the following out put.

    >
    > > 1
    > > 22
    > > 333
    > > 4444
    > > 55555
    > > 666666
    > > 7777777
    > > 88888888
    > > 999999999

    >
    > > I've seen it done in class but can't seem to remember.  There were
    > > only two for loops in the code.  Can someone help me with the code?

    >
    > It's good you're not asking people to do your homework.
    >
    > That said, it may be more helpful to you, learning-wise, to be given a  
    > general description of what you're looking for, and let you work the  
    > details out on your own.  So...
    >
    > In the output you're looking at, note that there are two things that are  
    > changing: the digit being displayed, and the length of the string being  
    > displayed.
    >
    > Note also that you even recall there were two loops.  So, perhaps one loop  
    > changes the digit, while the other loop controls the length of the string.
    >
    > Finally, note that the length appears to be related directly to the  
    > digit.  In other words, the loop controlling the length of the string may  
    > (i.e. will :) ) use the digit controlled by the one loop as its own  
    > input.  In this way, the loop controlling the digit also winds up  
    > indirectly controlling the length of the string.
    >
    > Here's a simple loop that changes the value of an integer from 1 to 9:
    >
    >      for (int i = 1; i <= 9; i++) { /* do something */ }
    >
    > Here's a simple loop that writes a fixed number of strings ("1" to "10")  
    > to the standard output:
    >
    >      for (int j = 1; j <= 10; j++) { System.out.printf("%d", j); }
    >
    > Finally, note that while I have used constants in the loop expressions,  
    > anywhere there's a hard-coded literal integer, you could instead put an  
    > integer variable.
    >
    > All of the above are the building blocks you need to answer your  
    > question.  You should now try on your own to assemble them in a useful way.
    >
    > Finally, while it's not quite as bad to post questions like this one as  
    > compared to asking for help with your homework, I would say that a  
    > newsgroup not specifically intended for learning how to program is  
    > probably not the best place for advice related to basic programming  
    > techniques.  Ideally, this is something better handled by contacting your  
    > teacher and/or classmates for help, but if not, I suspect there are forums  
    > out there that are specifically intended for helping people new to  
    > programming.
    >
    > I can't speak for the others reading this newsgroup, but while I don't  
    > object to the occasional "I don't know how to program, please help"  
    > question, it's not really something that should come up a lot in a  
    > newsgroup like this one.  That sort of thing can really reduce the  
    > signal-to-noise ratio for the newsgroup if it gets to be commonplace.
    >
    > Pete- Hide quoted text -
    >
    > - Show quoted text -


    public class ForLoop {

    public static void main (String[] args) {
    for (int row = 1; row <= 9; row = row + 1){
    for (col = 1; col <= row; col = col + 1) {
    System.out.print(row);
    }
    System.out.println();
    }
    }
    }

    deciphering code is much easier than creating it.
     
    David, Feb 17, 2008
    #6
  7. On 2008-02-17 05:23 +0100, David allegedly wrote:
    > On Feb 16, 8:53 pm, "Peter Duniho" <>
    > wrote:
    > public class ForLoop {
    >
    > public static void main (String[] args) {
    > for (int row = 1; row <= 9; row = row + 1){
    > for (col = 1; col <= row; col = col + 1) {
    > System.out.print(row);
    > }
    > System.out.println();
    > }
    > }
    > }
    >


    row += 1
    or simply
    row++
    or
    ++row
    are a tad prettier than
    row = row + 1

    If you haven't been introduced to some of the operators above, have a
    look at:
    <http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html>

    > deciphering code is much easier than creating it.


    I'm afraid you'll find that, at a professional level, the exact opposite
    is the case.

    DF.
     
    Daniele Futtorovic, Feb 17, 2008
    #7
  8. David

    Christian Guest

    David schrieb:
    >
    > Good advice Christian, however this is not homework. it's just a new
    > concept that was introduced yesterday Friday and I didn't have a very
    > good grasp on it. I wanted to use this weekend to practice the heck
    > out of it but I couldn't start practicing because the basic structure
    > eluded me.
    >
    > I could wait until Monday to ask the teacher but I'd rather be
    > familier with it now and concentrate on Monday's teaching on Monday.
    >
    > How long have you been programming? Are you a serious programmer or
    > just for fun?


    I am programmming Java now for about 2 and a half year. Started just
    before I began studying Computer Science.
    Though most of my programming is for the pleasure I am meanwhile getting
    paid to do research on p2p systems, where my software is done in Java.

    Still as its just a few hours per week I do program I wouldn't call me
    that serious. After 2 years I still feel like a welp, when Patricia or
    alike people here talk about their experience.

    Christian
     
    Christian, Feb 17, 2008
    #8
  9. David

    Roedy Green Guest

    On Sat, 16 Feb 2008 17:38:14 -0800 (PST), David
    <> wrote, quoted or indirectly quoted someone
    who said :

    > it's just a new
    >concept that was introduced yesterday Friday and I didn't have a very
    >good grasp on it. I wanted to use this weekend to practice the heck
    >out of it but I couldn't start practicing because the basic structure
    >eluded me.


    the same principle applies. You are doing this to learn. If the
    solution is handed to you on a plate, you are depriving yourself of
    the learning experience. The goal is to teach you how to solve
    problems, not to find the answers.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Feb 17, 2008
    #9
  10. David

    David Guest

    On Feb 17, 12:33 am, Daniele Futtorovic
    <> wrote:
    > On 2008-02-17 05:23 +0100, David allegedly wrote:
    >
    > > On Feb 16, 8:53 pm, "Peter Duniho" <>
    > > wrote:
    > > public class ForLoop {

    >
    > >       public static void main (String[] args) {
    > >             for (int row = 1; row <= 9; row = row + 1){
    > >                 for (col = 1; col <= row; col = col + 1) {
    > >                     System.out.print(row);
    > >                 }
    > >                 System.out.println();
    > >             }
    > >       }
    > > }

    >
    >      row += 1
    > or simply
    >      row++
    > or
    >      ++row
    > are a tad prettier than
    >      row = row + 1
    >
    > If you haven't been introduced to some of the operators above, have a
    > look at:
    > <http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op1.html>
    >
    > > deciphering code is much easier than creating it.

    >
    > I'm afraid you'll find that, at a professional level, the exact opposite
    > is the case.
    >
    > DF.


    Great, thank you all for the responses. I have enough to keep me busy
    for now. Thanks again, I appreciate it.
     
    David, Feb 17, 2008
    #10
  11. David

    cool20

    Joined:
    Nov 18, 2011
    Messages:
    2
    please help me
    7
    66
    555
    4444
    33333
    222222
    11111111
     
    cool20, Nov 18, 2011
    #11
  12. David

    cool20

    Joined:
    Nov 18, 2011
    Messages:
    2
    help me please, i cant figure out this problem.

    7
    66
    555
    4444
    33333
    222222
    1111111

    :adore:
     
    cool20, Nov 18, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Bruce

    dynamic number of nested loops

    Allan Bruce, Jul 1, 2004, in forum: Java
    Replies:
    5
    Views:
    9,918
    Tor Iver Wilhelmsen
    Jul 3, 2004
  2. Porthos
    Replies:
    3
    Views:
    498
    Joris Gillis
    Feb 7, 2005
  3. SplaTTer
    Replies:
    2
    Views:
    399
    SplaTTer
    Jul 2, 2003
  4. viza

    break or continue out of nested loops

    viza, Jul 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    1,058
    sunil
    Jul 17, 2003
  5. Me
    Replies:
    2
    Views:
    264
Loading...

Share This Page