Questions tagged [loops]

A **loop** is a sequence of statements which is specified once but which may be carried out several times in succession.

Taken from Wikipedia:

Most programming languages have constructions for repeating a loop a certain number of times. Note that if N is less than 1 in these examples then the language may specify that the body is skipped completely, or that the body is executed just once with N = 1. In most cases counting can go downwards instead of upwards and step sizes other than 1 can be used.

155 questions
129
votes
11 answers

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 of code. Are there any cases where a task can…
Pikamander2
  • 1,259
  • 2
  • 9
  • 9
110
votes
8 answers

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 searching for a while, I've stumbled upon some…
dennis
  • 1,169
  • 3
  • 9
  • 8
83
votes
3 answers

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 that are of different sizes and the first thing I go…
turnip
  • 1,657
  • 2
  • 15
  • 21
68
votes
17 answers

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 assumptions correct after some trial and error but I…
CodeYogi
  • 2,156
  • 1
  • 18
  • 34
56
votes
6 answers

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 loops very frequently. We know that. What I was…
Dynamic
  • 5,746
  • 9
  • 45
  • 73
51
votes
7 answers

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; instruction3). So why is this mapping reversed in the…
Piotr Migdal
  • 521
  • 1
  • 4
  • 8
48
votes
16 answers

Inside a for-loop, should I move the break condition into the condition field if possible?

Sometimes I need for loops which needs a break like this: for(int i=0;i
ocomfd
  • 5,652
  • 8
  • 29
  • 37
46
votes
11 answers

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 difference?
badbye
  • 585
  • 1
  • 4
  • 5
38
votes
2 answers

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 clearly about your thoughts. Generally speaking, a…
xqMogvKW
  • 533
  • 1
  • 5
  • 8
37
votes
6 answers

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 places where this feature was explained it was…
Force444
  • 643
  • 2
  • 7
  • 13
27
votes
2 answers

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 of where I'd normally use: foreach (var item in…
Paprik
  • 399
  • 1
  • 3
  • 8
23
votes
8 answers

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 are heretic movements that try to make indentations…
Anon
  • 3,565
  • 3
  • 27
  • 45
21
votes
9 answers

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 the function signature and the documentation. So…
Fred
  • 449
  • 1
  • 4
  • 12
21
votes
1 answer

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 code here. Some really good examples and an…
candied_orange
  • 102,279
  • 24
  • 197
  • 315
17
votes
6 answers

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 read many years ago by, probably, Wirth or Dijkstra,…
1
2 3
10 11