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
64
votes
17 answers

Sum two arrays in single iteration

I want to sum each value of an array of numbers with its corresponding value in a different array of numbers, and I want to do this without looping through each individual value. So: var array1 = [1,2,3,4]; var array2 = [5,6,7,8]; var sum =…
TheNovice
  • 1,277
  • 3
  • 16
  • 23
64
votes
2 answers

range in jinja2 inside a for loop

I have a nested list. I need to iterate through a list and keep it in for loop as shown below. {% for alpha in list %}
Chandan Gupta
  • 1,410
  • 2
  • 13
  • 29
64
votes
14 answers

What's the best way to loop through a set of elements in JavaScript?

In the past and with most my current projects I tend to use a for loop like this: var elements = document.getElementsByTagName('div'); for (var i=0; i
James
  • 109,676
  • 31
  • 162
  • 175
63
votes
1 answer

Why should I avoid loops when designing relationships for a database?

Someone told me that it was bad design to have loops in the datamodel. I have heard this before a couple of times but didn't pay much attention. For example you have entities User, Project, Activity. A project is owned by a User, so we have a…
63
votes
8 answers

C# Wait until condition is true

I am trying to write a code that executes when a condition is met. Currently, I am using while...loop, which I know is not very efficient. I am also looking at AutoResetEvent() but i don't know how to implement it such that it keeps checking until…
Keylee
  • 713
  • 1
  • 5
  • 11
63
votes
2 answers

Python For loop get index

I am writing a simple Python for loop to prnt the current character in a string. However, I could not get the index of the character. Here is what I have, does anyone know a good way to get the current index of the character in the loop? loopme =…
user1817081
  • 1,185
  • 3
  • 15
  • 21
63
votes
2 answers

Loop through files in a folder in matlab

I have a set of days of log files that I need to parse and look at in matlab. The log files look like this: LOG_20120509_120002_002.csv (year)(month)(day)_(hour)(minute)(second)_(log part number) The logs increment hourly, but sometimes the…
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
62
votes
8 answers

Incrementing a variable inside a Bash loop

I'm trying to write a small script that will count entries in a log file, and I'm incrementing a variable (USCOUNTER) which I'm trying to use after the loop is done. But at that moment USCOUNTER looks to be 0 instead of the actual value. Any idea…
maephisto
  • 4,952
  • 11
  • 53
  • 73
62
votes
4 answers

for or while loop to do something n times

In Python you have two fine ways to repeat some action more than once. One of them is while loop and the other - for loop. So let's have a look on two simple pieces of code: for i in range(n): do_sth() And the other: i = 0 while i < n: …
Sventimir
  • 1,996
  • 3
  • 14
  • 25
61
votes
10 answers

What is the best way to iterate over the lines of a Java String?

Currently I'm using something like : String[]lines = textContent.split(System.getProperty("line.separator")); for(String tmpLine : lines){ //do something } I'm not very glad of this method because it create an heavy array (let say textContent…
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
61
votes
10 answers

How to stop intense Javascript loop from freezing the browser

I'm using Javascript to parse an XML file with about 3,500 elements. I'm using a jQuery "each" function, but I could use any form of loop. The problem is that the browser freezes for a few seconds while the loop executes. What's the best way to…
Chris B
  • 15,524
  • 5
  • 33
  • 40
61
votes
7 answers

Doesn't JavaScript support closures with local variables?

I am very puzzled about this code: var closures = []; function create() { for (var i = 0; i < 5; i++) { closures[i] = function() { alert("i = " + i); }; } } function run() { for (var i = 0; i < 5; i++) { closures[i](); …
qollin
  • 1,271
  • 1
  • 14
  • 15
61
votes
5 answers

Iterate through dictionary values?

Hey everyone I'm trying to write a program in Python that acts as a quiz game. I made a dictionary at the beginning of the program that contains the values the user will be quizzed on. Its set up like so: PIX0 = {"QVGA":"320x240", "VGA":"640x480",…
Ben Williams
  • 745
  • 1
  • 5
  • 9
61
votes
4 answers

Looping through rows in a DataView

The DataView object doesn't have a Rows property like DataTable. How do I loop through the rows of a DataView?
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
60
votes
2 answers

Groovy, how to iterate a list with an index

With all the shorthand ways of doing things in Groovy, there's got to be an easier way to iterate a list while having access to an iteration index. for(i in 0 .. list.size()-1) { println list.get(i) } Is there no implicit index in a basic for…
raffian
  • 31,267
  • 26
  • 103
  • 174