Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Nested for loops

Reply
Thread Tools

Nested for loops

 
 
David
Guest
Posts: n/a
 
      02-17-2008
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?

 
Reply With Quote
 
 
 
 
Christian
Guest
Posts: n/a
 
      02-17-2008
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
 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      02-17-2008
On Feb 16, 8:25*pm, Christian <fakem...@xyz.de> 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?
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      02-17-2008
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
 
Reply With Quote
 
David
Guest
Posts: n/a
 
      02-17-2008
On Feb 16, 8:53*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sat, 16 Feb 2008 17:16:55 -0800, David <nosmadadi...@gmail.com> 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.
 
Reply With Quote
 
David
Guest
Posts: n/a
 
      02-17-2008
On Feb 16, 8:53*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sat, 16 Feb 2008 17:16:55 -0800, David <nosmadadi...@gmail.com> 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.
 
Reply With Quote
 
Daniele Futtorovic
Guest
Posts: n/a
 
      02-17-2008
On 2008-02-17 05:23 +0100, David allegedly wrote:
> On Feb 16, 8:53 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> 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.
 
Reply With Quote
 
Christian
Guest
Posts: n/a
 
      02-17-2008
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      02-17-2008
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
 
Reply With Quote
 
David
Guest
Posts: n/a
 
      02-17-2008
On Feb 17, 12:33*am, Daniele Futtorovic
<da.futt.newsLOVELYS...@laposte.net> wrote:
> On 2008-02-17 05:23 +0100, David allegedly wrote:
>
> > On Feb 16, 8:53 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
nested loops and conditional statements Porthos XML 3 02-07-2005 08:47 PM
dynamic number of nested loops Allan Bruce Java 5 07-03-2004 09:12 PM
break or continue out of nested loops viza C Programming 5 07-17-2003 04:04 AM
compacting similar nested for loops SplaTTer C++ 2 07-02-2003 09:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57