Monday, April 20, 2009

How to repeat C++ program without using loops?

this is a requirement for my C++ starter class.


It says: Your program should repeat forever, asking for new object pairs. Since we have not learned about loops or conditional statements yet, you may not use them (no for, while, do, if statements). I will tell you how to get the program to repeat in class, so you can save this part for last. (It%26#039;s just one line to add to your program. )





unfortunately, I did not go to class.





Anyone can help?

How to repeat C++ program without using loops?
use recursion to replace loop
Reply:hey, if you would ask for my opinion, as a programmer, DO NOT use goto... it might crash down your program.. You only use goto, when necessary and with FULL CARE... The tendency when you carelessly use this statement is, you might leak the memoy being in used especially the variables being declared... For example:





main()


{


BACK: { }


int var = 0;





// some statements here...





if (i == 0){ goto BACK; }


}





Well let me explain goto first... It is a statement to bring a certain part of a program as defined.. So in the above code, when i becomes 0, it will goto the topmost part of the function, specifically in the line where the word %26quot;BACK: { }%26quot; is labeled... But the big problem here is that, since int var is declared after the BACK label, the tendency is, the address that has been used by int var wont be properly destroyed... You have to consider, as programmer, that variables, when created, must be destroyed after it has been used.. In most compilers, though there is not really any proper statements used to destroy a variable, these variables are automatically destroyed, ONLY, if a certain end point of a scope where the variable was declared is met. As you can see above, the scope where the variable was declared was between the BACK label upto the closing bracket of the main function... Since right before the closing bracket of the main function, there appears a statement which tells the program to go back to the defined label when the value of i is 0. With this, it causes the variable to be unsuccesfully destroyed, though it was declared since the end of the scope where the variable was declared wasn%26#039;t even met at all.. Anyway, this is already the major considerations you have to take note upon creating huge applications. Anyway, I still advice not to use this statement, unless you are already way too advanced..





I would rather suggest that you use recursion. This means, a function calls it%26#039;s own function name.. For example..





main()


{


// some statements...





printf(%26quot;Hello world\n%26quot;);


getch();





return main();


}





This infinitely loops back to top without even having problems about memory leaks. Plus, it%26#039;s just one liner.. PM me in my YM if you have other questions about Recursion.. Right now, I might be able to share some ideas to you on how recursion works and how to achieve the best recursive function calls...


OR, post a comment on my blog, http://ronaldborla.blogsome.com/ (^^,)
Reply:I think you should use a GO TO statement without any condition, for example:


10 Read X


Print X+2


Go to 10
Reply:so you have to travel back, use %26#039;goto%26#039;



annual credit report

No comments:

Post a Comment