• Post Reply Bookmark Topic Watch Topic
  • New Topic

Solving without Loops  RSS feed

 
Saad Zahoor
Ranch Hand
Posts: 70
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question : i want to make this program run over and over without using loops like we do in c++  
                             start :
                             goto start ;
   how to do such thing in java !!

 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saad,
Of course you can,make use of recursion.after line 11 insert the statement *main(args);*.but IMO,loop is a better approach than this one as it will give you the StackOverFlow error when the stack size becomes full(which is very sure as it is a infinite recursion).

Hope it helps!

Kind Regards,
Praveen.
 
Saad Zahoor
Ranch Hand
Posts: 70
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks thats help alot .. but can you explain what kind of error will i get and why ??
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i cannot explain much about it but may be i can visualize you  this infinite recursion,you can interpret such a call as the image below.
methods were also given some memory to run and if the memory becomes full.jvm will throw an error like so calledStackOverFlow error.



Praveen
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your case it is less likely to get an error though,practically, as you are prompting a user for the number so in order to get an error the user needs to enter the number several(several(several(several(several......of times.....)))))

Try this code...

and what i meant is to avoid this kind of coding...
 
Saad Zahoor
Ranch Hand
Posts: 70
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks now i understand !!
 
Campbell Ritchie
Sheriff
Posts: 53622
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saad Zahoor wrote:. . . without using loops like we do in c++  
                             start :
                             goto start ;
. . .
But that is a loop. That is how loops are implemented in assembly code, or even in older versions of Basic. It is a pretty dreadful implementation, but it still represents a loop.
 
Henry Wong
author
Sheriff
Posts: 22678
113
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, this is the first time, in a very very long time, since I've seen someone advocated the use of a goto statement over a loop (over anything, really). Good, bad, or indifferent, there is a likely reason why the goto statement never made it into the Java language -- and that likely reason is an overwhelming consensus against it.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12422
42
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you say "I want this code to run over and over", you are saying you want a loop, end of story.

Now, maybe you don't want to use the built-in loop constructs, like "for", "do", or "do-while", but you'd still have a loop.

A better qeustion is WHY don't you want to use them?  That's like saying "I want to travel from St. Louis to Denver, but I don't want to use anything that travels on a road, a train-track, or through the air."
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!