Wednesday, April 15, 2009

What are the benefits of "loops" in programming?

Saves on space, you don%26#039;t have to keep typing out the same code again,





can save time as well.








these are called recursive algorithms

What are the benefits of %26quot;loops%26quot; in programming?
They allow you to repeat code. Here are some video tutorials with examples that explain them more:





http://xoax.net/comp/cpp/console/Lesson8...


http://xoax.net/comp/cpp/console/Lesson1...
Reply:You can execute some task for N number of times, until the condition met to false





Available loops in Programming:





1) For


2) While


3) do while





for more details please visit these site





http://www.w3schools.com/php/php_looping...








with regards,


A Suresh Kumar


http://suresh-mobileweb.blogspot.com
Reply:First check out :


http://en.wikipedia.org/wiki/Control_flo...





Personally i like to use %26quot;for each loop%26quot;, well in this way, if you have a collection, you can loop thou all existing elements in the collection, and check out whether whom of them fit to conditions that i have.





edit:


Like if you have bills collection of some sort in person class in Java, you have data structure like:


private final ArrayList%26lt;Bill%26gt; bills= new ArrayList%26lt;Bill%26gt;();





and when you like need to test some condition inside person about his bills, you can use for each like:


..for (Bill x : this.bills) {


......if (x.getName().equals(somename)) {


..........//something


......}


..}





Now you even need loops to pass thou all the data, if you have the data in database. Similar Sql would look like:


Select something


From Persons


Where Persons.id=somenr And Persons.name=somename;
Reply:Loops are used to run the same code more than once without having to write the whole code out again. You can avoid using loops and repetition with recursion but this only makes sense in certain circumstances.
Reply:Think of it this way: we only pretend computers do these amazing things. They do not know the difference between graphics and databases and games and executable code. They don%26#039;t think.





That means we have to tell them to do absolutely everything we want them to do and most of the tasks we use them for involve repetition. In the old days of coding for the Apple II and the Commodore Vic the amount of extra memory that for loops took up was expensive enough so they %26quot;unrolled%26quot; these loops. And I remember a %26quot;professional%26quot; program that got it wrong. Recursion and loops take up more memory, but they are also easier to maintain and to keep track of.





Loops are great.
Reply:Here%26#039;s a quick example:





for (X=1 to 20)


PRINT X


NEXT X





(This will print the numbers 1 to 20 in a loop until the condition becomes false, that is: it stops when the count reaches 21.)





Here%26#039;s the slow way:


X = 1


PRINT X


X = 2


PRINT X


X = 3


PRINT X


X = 4


PRINT X





(Get the idea?)


;-)



small business web

No comments:

Post a Comment