• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing a String object without using loop

 
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we print the String "Hello World!" 1000 times without using loop?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write System.out.println("Hello World!"); 1000 times in your code?
What problem are you trying to solve here. If you can provide some more information, we can get a clearer picture.
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let, String msg="Hello World!"; Can we print this message for 1000 times without using any loop? I have faced this question in an interview and replied exactly the same as you. But the interviewer asked me to search for it in google.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
recursion
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Fred, but a code snippet will be more helpful.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Kumar Jena wrote:Thanks Fred, but a code snippet will be more helpful.

As they say here in Teesside, "Thereygo".
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Kumar Jena wrote:Thanks Fred, but a code snippet will be more helpful.


We generally don't just give out answers. Too many folks would be tempted to use us to cheat on their homework.

do you know what recursion is? Have you ever written a recursive function? It's a function that calls itself, each time reducing to a simpler case.

Printing something 1000 times is easy. you just print it once, and then call a function that prints it 999 more times.
Printing something 999 times is easy. you just print it once, and then call a function that prints it 998 more times.
Printing something 998 times is easy. you just print it once, and then call a function that prints it 997 more times...
...
Printing something 2 times is easy. you just print it once, and then call a function that prints it 1 more time.
Printing something 1 time is easy. you just print it once, and then don't call any other function.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, the good old "xx Bottles of beer on the wall" song
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's cute, Fred!
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

Deepak Kumar Jena wrote:Thanks Fred, but a code snippet will be more helpful.


We generally don't just give out answers. Too many folks would be tempted to use us to cheat on their homework.

do you know what recursion is? Have you ever written a recursive function? It's a function that calls itself, each time reducing to a simpler case.

Printing something 1000 times is easy. you just print it once, and then call a function that prints it 999 more times.
Printing something 999 times is easy. you just print it once, and then call a function that prints it 998 more times.
Printing something 998 times is easy. you just print it once, and then call a function that prints it 997 more times...
...
Printing something 2 times is easy. you just print it once, and then call a function that prints it 1 more time.
Printing something 1 time is easy. you just print it once, and then don't call any other function.


Let's make it a bit harder, going in a similar direction.

Printing something 1000 times is easy. You print it 500 times twice.
Printing something 500 times is easy. You print it 250 times twice.
...
Printing something 125 times is a bit harder but still easy. You print it once, then two times 62 times. Or you print it 63 times and 62 times.
...
Printing something 1 time is easy. You print it and you're done.


Note that Fred's example is easier, and both are just as fast. After all, in the end they will both do the same thing: print something 1000 times. I just wanted to show that even with recursing there can be alternatives.
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys for the answers and suggestions. What I think is explaining the logic to work with and a small piece of code will help to the inexperienced programmers.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Kumar Jena wrote: . . . a small piece of code . . .

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what would help even more would be for you to try doing it yourself. One of two things will happen:

1) You'll get it to work (HORAY!!!)
2) You'll get stuck, and then you can post your code here and we can make suggestions from that.
 
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
recursion - a function that calls itself.

I wonder if programmers create an infinite recursion and what is use for? Just a thought that came up when I saw this posting.

Gary
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recursion. With that link, Gary's statement and Fred's post you should really be able to solve this yourself.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gary Ba wrote: . . . I wonder if programmers create an infinite recursion and what is use for? . . .

Of course people create infinite recursion. It is very useful. You can kick the computer, shout, "The bl**d* thing will never work," and then, most important of all . . .












































. . . go for a cup of coffee!
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gary Ba wrote:I wonder if programmers create an infinite recursion and what is use for?


yes they do it, but usually not on purpose. in programming, each call should be to a simpler case. eventually, you should get to some 'base' case that doesn't need to be simplified any more.

If you code it wrong (and probably everyone who has ever tried to write a recursive function has done this), you don't reduce to your base case. Since each call uses a little more memory (and doesn't free the memory already used), you can quickly run out. At that point, the JVM will throw and out-of-memory exception and your program crashes. This is generally considered a 'bad thing', and people try to avoid it.
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. I have done it.
 
Deepak Kumar Jena
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all for your valuable suggestions.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great job man.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can improve that no end. Apart from the indentation . . .

You don't need the return;. You simply put the entire body of the method inside an if (counter > 0) { . . . } block. You are better using counter - 1 than --counter . Since counter is a local variable, you are not accessing a field. Since that method does not access any fields, it could be made static.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code currently has one danger - if counter < 0 your program will print out the string until the counter is Integer.MIN_VALUE (- 2^31), then print with a counter of Integer.MAX_VALUE (2^31 - 1), then print out until counter == 0. The check Campbell suggested (if (counter > 0)) will solve this issue for you.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic