Questions tagged [loops]
A **loop** is a sequence of statements which is specified once but which may be carried out several times in succession.
154
questions
130
votes
11
answers
38k
views
Is there anything that can be done with recursion that can't be done with loops?
There are times where using recursion is better than using a loop, and times where using a loop is better than using recursion. Choosing the "right" one can save resources and/or result in fewer lines ...
110
votes
8
answers
26k
views
Why is polling accepted in web programming?
I am currently working on a Ruby on Rails project which shows a list of images.
A must-have for this project is that it shows new posts in realtime without the need of refreshing the web page. After ...
84
votes
3
answers
43k
views
How do I move away from the “for-loop” school of thought?
This is a rather conceptual question, but I was hoping I could get some good advice on this. A lot of the programming I do is with (NumPy) arrays; I often have to match items in two or more arrays ...
68
votes
17
answers
23k
views
How to write correct loops?
Most of time while writing loops I usually write wrong boundary conditions(eg: wrong outcome) or my assumptions about loop terminations are wrong(eg: infinitely running loop). Although I got my ...
57
votes
6
answers
18k
views
Who created the idea(s) of the first loop constructs?
while (1) {
if (1+1==2) {
print "Yes, you paid attention in Preschool!";
} else {
print "Wait... I thought 1+1=2";
}
}
As a developer, we all have to use ...
51
votes
7
answers
15k
views
Why are semicolons and commas interchanged in for loops?
In many languages (a wide list, from C to JavaScript):
commas , separate arguments (e.g. func(a, b, c)), while
semicolons ; separate sequential instructions (e.g. instruction1; instruction2; ...
48
votes
16
answers
16k
views
Inside a for-loop, should I move the break condition into the condition field if possible? [closed]
Sometimes I need for loops which needs a break like this:
for(int i=0;i<array.length;i++){
//some other code
if(condition){
break;
}
}
I feel uncomfortable with writing
if(...
46
votes
11
answers
31k
views
Is a while loop intrinsically a recursion?
I wondered whether a while loop is intrinsically a recursion?
I think it is because a while loop can be seen as a function that calls itself at the end. If it is not recursion, then what is the ...
39
votes
2
answers
84k
views
General way to convert a loop (while/for) to recursion or from a recursion to a loop?
This problem is mainly focusing on the algorithm, maybe something abstract and more academic.
The example is offering a thought, I wanna a generic way, so example is only used as to make us more ...
37
votes
6
answers
135k
views
Why are nested loops considered bad practice?
My lecturer mentioned today that it was possible to "label" loops in Java so that you could refer to them when dealing with nested loops. So I looked up the feature as I didn't know about it and many ...
27
votes
2
answers
47k
views
Filtering foreach loops with a where condition vs continue guard clauses
I've seen some programmers use this:
foreach (var item in items)
{
if (item.Field != null)
continue;
if (item.State != ItemStates.Deleted)
continue;
// code
}
instead ...
23
votes
8
answers
5k
views
At what point is it taboo to have loops within loops?
Just curious. The most I have ever had was a for loop within a for loop, because after reading this from Linus Torvalds:
Tabs are 8 characters, and thus indentations are also 8 characters.
There ...
22
votes
9
answers
8k
views
Declaring that a function never returns
If I have a function that never returns (it contains an infinite loop) how do I declare/communicate that it will never return to the user of the function?
The user does not see the source code, only ...
21
votes
1
answer
6k
views
What is priming the pump? Sometimes called a priming read
I was taught this expression and pattern way back in the day. Sure, the name comes from old pumps that needed to be filled with water before they could pump water, but who cares? We're talking about ...
17
votes
6
answers
8k
views
Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition
This is the most popular way (it seems to me) of checking if a value is in an array:
for (int x : array)
{
if (x == value)
return true;
}
return false;
However, in a book I’ve ...