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
125
votes
2 answers

Is it safe to delete an object property while iterating over them?

When iterating over an object's properties, is it safe to delete them while in a for-in loop? For example: for (var key in obj) { if (!obj.hasOwnProperty(key)) continue; if (shouldDelete(obj[key])) { delete obj[key]; } } In…
Joe Shaw
  • 22,066
  • 16
  • 70
  • 92
125
votes
2 answers

Loop through a Map with JSTL

I'm looking to have JSTL loop through a Map and output the value of the key and it's value. For example I have a Map which can have any number of entries, i'd like to loop through this map using JSTL and output both…
Dean
  • 1,261
  • 2
  • 8
  • 4
125
votes
10 answers

How to loop through dates using Bash?

I have such bash script: array=( '2015-01-01', '2015-01-02' ) for i in "${array[@]}" do python /home/user/executeJobs.py {i} &> /home/user/${i}.log done Now I want to loop through a range of dates, e.g. 2015-01-01 until 2015-01-31. How to…
Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147
124
votes
14 answers

Cannot find control with unspecified name attribute on formArrays

I am trying to iterate over a formArray in my component but I get the following error Error: Cannot find control with unspecified name attribute Here is what the logic looks like on my class file export class AreasFormComponent implements OnInit…
Bazinga777
  • 5,140
  • 13
  • 53
  • 92
124
votes
4 answers

Looping through files in a folder Node.JS

I am trying to loop through and pick up files in a directory, but I have some trouble implementing it. How to pull in multiple files and then move them to another folder? var dirname = 'C:/FolderwithFiles'; console.log("Going to get file…
Prolasis
  • 1,323
  • 2
  • 11
  • 8
123
votes
3 answers

Breaking loop when "warnings()" appear in R

I am having an issue: I am running a loop to process multiple files. My matrices are enormous and therefore I often run out of memory if I am not careful. Is there a way to break out of a loop if any warnings are created? It just keeps…
mmann1123
  • 5,031
  • 7
  • 41
  • 49
122
votes
11 answers

PHP: Can I get the index in an array_map function?

I'm using a map in php like so: function func($v) { return $v * 2; } $values = array(4, 6, 3); $mapped = array_map(func, $values); var_dump($mapped); Is it possible to get the index of the value in the function? Also - if I'm writing code that…
Ollie Glass
  • 19,455
  • 21
  • 76
  • 107
121
votes
3 answers

Updating a value in a pandas dataframe in an iterrows loop

I am doing some geocoding work that I used selenium to screen scrape the x-y coordinate I need for address of a location, I imported an xls file to pandas dataframe and want to use an explicit loop to update the rows which do not have the x-y…
lokheart
  • 23,743
  • 39
  • 98
  • 169
121
votes
11 answers

Loop through childNodes

I'm trying to loop through childNodes like this: var children = element.childNodes; children.forEach(function(item){ console.log(item); }); However, it output Uncaught TypeError: undefined is not a function due to forEach function. I also try…
user3828771
  • 1,603
  • 3
  • 14
  • 14
120
votes
5 answers

How can I loop through all rows of a table? (MySQL)

I have a table A and there is one primary key ID. Now I want to go through all rows in A. I found something like 'for each record in A', but this seems to be not how you do it in MySQL. Thing is for each row I want to take a field and transform it,…
Raffael
  • 19,547
  • 15
  • 82
  • 160
119
votes
4 answers

For what value of i does while (i == i + 1) {} loop forever?

I ran cross this puzzler from an advanced programming course at a UK university exam. Consider the following loop, in which i is, so far, undeclared: while (i == i + 1) {} Find the definition of i, that precedes this loop, such that the while…
jake mckenzie
  • 1,246
  • 2
  • 8
  • 9
118
votes
7 answers

Creating an array from a text file in Bash

A script takes a URL, parses it for the required fields, and redirects its output to be saved in a file, file.txt. The output is saved on a new line each time a field has been found. file.txt A Cat A Dog A Mouse etc... I want to take file.txt…
TheDailyToast
  • 1,283
  • 2
  • 9
  • 6
118
votes
6 answers

Loop through list with both content and index

It is very common for me to loop through a python list to get both the contents and their indexes. What I usually do is the following: S = [1,30,20,30,2] # My list for s, i in zip(S, range(len(S))): # Do stuff with the content s and the index…
Oriol Nieto
  • 5,409
  • 6
  • 32
  • 38
117
votes
7 answers

VBA - how to conditionally skip a for loop iteration

I have a for loop over an array. What I want to do is test for a certain condition in the loop and skip to the next iteration if true: For i = LBound(Schedule, 1) To UBound(Schedule, 1) If (Schedule(i, 1) < ReferenceDate) Then …
Richard H
  • 38,037
  • 37
  • 111
  • 138
117
votes
8 answers

Decreasing for loops in Python impossible?

I could be wrong (just let me know and I'll delete the question) but it seems python won't respond to for n in range(6,0): print n I tried using xrange and it didn't work either. How can I implement that?
Gal
  • 23,122
  • 32
  • 97
  • 118