Questions tagged [loops]

Loops are a type of control flow structure in programming in which a series of statements may be executed repeatedly until some condition is met.

A loop is a fundamental programming idea that is commonly used in writing programs.

Types

A loop can be categorized in two different ways,

1. Entry Controlled Loops

A loop which checks for the condition before the iteration is known as Entry Controlled loops - for example

  • while loop - iterates while a certain condition is true
  • until loop - iterates while a certain condition is false
  • for loop - iterates through numbers in a certain range. Note: not the same as C++ for loop
  • foreach loop - iterates through the elements of a collections.

2. Exit Controlled Loops

A loop which checks the condition after the iteration is knows as Exit Controlled loop - for example

  • do-while loop - iterates while a certain condition is true (the first iteration will run regardless of the condition)
  • do-until loop - iterates while a certain condition is false (the first iteration will run regardless of the condition)

Most languages provide only a subset of loop types described above. For example: in Python there are only foreach (keyword for) and while loops.

Break and continue

In some languages, there are two keywords that simplify the task of implementing a more advanced control flow: break and continue. The former allows you to jump to the operator immediately after the loop, the latter allows you to jump to the end of the current iteration.

Example: implementation of do-until loop in Python using the break keyword:

while True:
    // loop body
    if condition:
        break

Tag usage

The tag can be used for programming related problems in implementing loops feature of any programming language. Please avoid theoretical questions related to tag on stackoverflow.

See also:

Read more

95843 questions
88
votes
2 answers

How to break outer loops from inner structures that respond break (loops/switch)

How to I break an outer loop from within an nested structure that responds to the break statement in Swift? For example: while someCondition { if someOtherCondition { switch (someValue) { case 0: // do something …
nhgrif
  • 61,578
  • 25
  • 134
  • 173
87
votes
2 answers

Object does not support item assignment error

In my views.py I assign values before saving the form. I used to do it the following way: projectForm.lat = session_results['lat'] projectForm.lng = session_results['lng'] Now, since the list of variables got a bit long, I wanted to loop over…
neurix
  • 4,126
  • 6
  • 46
  • 71
87
votes
4 answers

Why does a for loop behave differently when migrating VB.NET code to C#?

I'm in the process of migrating a project from Visual Basic to C# and I've had to change how a for loop being used is declared. In VB.NET the for loop is declared below: Dim stringValue As String = "42" For i As Integer = 1 To 10 -…
slee423
  • 1,307
  • 2
  • 20
  • 34
87
votes
5 answers

MySQL: How to insert a record for each result in a SQL query?

Say I have a select SELECT DISTINCT id, customer_id, domain FROM config WHERE type = 'foo'; which returns some records. How can I do an insert for reach row in the result set like INSERT INTO config (id, customer_id, domain) VALUES (@id,…
acme
  • 14,654
  • 7
  • 75
  • 109
87
votes
6 answers

How to break out of nested loops in Go?

I have an outer and inner loop, each iterating over a range. I want to exit the outer loop when a condition is satisfied inside the inner loop. I have a solution which works using two 'break's, one inside the inner loop and one inside the outerloop,…
Jay
  • 1,980
  • 1
  • 13
  • 23
87
votes
18 answers

How can I loop through all subviews of a UIView, and their subviews and their subviews

How can I loop through all subviews of a UIView, and their subviews and their subviews?
123hal321
  • 2,080
  • 4
  • 24
  • 25
85
votes
6 answers

Creating a list of objects in Python

I'm trying to create a Python script that opens several databases and compares their contents. In the process of creating that script, I've run into a problem in creating a list whose contents are objects that I've created. I've simplified the…
None
85
votes
13 answers

Is it bad practice to use break to exit a loop in Java?

I was wondering if it is a "bad practice" to use a break statement to exit a loop instead of fulfilling the loop condition? I do not have enough insight in Java and the JVM to know how a loop is handled, so I was wondering if I was overlooking…
Don
  • 1,428
  • 3
  • 15
  • 31
84
votes
5 answers

Why is 'for(var item in list)' with arrays considered bad practice in JavaScript?

Given a simple zero based, numerically indexed array: var list = ['Foo', 'Bar', 'Baz']; Many times, I have noticed that when someone suggests looping through variables in an array like this: for(var item in list) { ... } ...there's almost…
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
84
votes
5 answers

How to iterate through table in Lua?

So, I have a table something along these lines: arr = { apples = { 'a', "red", 5 }, oranges = { 'o', "orange", 12 }, pears = { 'p', "green", 7 } } It doesn't seem like it's possible to access them based on their index, and the values…
Lemony Lime
  • 1,113
  • 3
  • 11
  • 12
84
votes
6 answers

Iterating over arbitrary dimension of numpy.array

Is there function to get an iterator over an arbitrary dimension of a numpy array? Iterating over the first dimension is easy... In [63]: c = numpy.arange(24).reshape(2,3,4) In [64]: for r in c : ....: print r ....: [[ 0 1 2 3] [ 4 …
AFoglia
  • 7,968
  • 3
  • 35
  • 51
83
votes
4 answers

skip current iteration

I have a php array $numbers = array(1,2,3,4,5,6,7,8,9) if I am looping over it using a foreach foreach($numbers as $number) and have an if statement if($number == 4) what would the line of code be after that that would skip anything after that line…
Hailwood
  • 89,623
  • 107
  • 270
  • 423
83
votes
14 answers

Is there an equivalent to the "for ... else" Python loop in C++?

Python has an interesting for statement which lets you specify an else clause. In a construct like this one: for i in foo: if bar(i): break else: baz() the else clause is executed after the for, but only if the for terminates normally (not…
Delgan
  • 18,571
  • 11
  • 90
  • 141
82
votes
6 answers

How to concat string + i?

for i=1:N f(i) = 'f'+i; end gives an error in MatLab. What's the correct syntax to initialize an array with N strings of the pattern fi? It seems like even this is not working: for i=1:4 f(i) = 'f'; end
simpatico
  • 10,709
  • 20
  • 81
  • 126
82
votes
12 answers

Endless loop in C/C++

There are several possibilities to do an endless loop, here are a few I would choose: for(;;) {} while(1) {} / while(true) {} do {} while(1) / do {} while(true) Is there a certain form which one should choose? And do modern compilers make a…
magu_
  • 4,766
  • 3
  • 45
  • 79