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
1803
votes
29 answers

How to loop through a plain JavaScript object with the objects as members

How can I loop through all members in a JavaScript object, including values that are objects? For example, how could I loop through this (accessing the "your_name" and "your_message" for each)? var validation_messages = { "key_1": { …
edt
  • 22,010
  • 30
  • 83
  • 118
1036
votes
28 answers

Traverse a list in reverse order in Python

How do I traverse a list in reverse order in Python? So I can start from collection[len(collection)-1] and end in collection[0]. I also want to be able to access the loop index.
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
934
votes
9 answers

A for-loop to iterate over an enum in Java

I have an enum in Java for the cardinal and intermediate directions: public enum Direction { NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST } How can I write a for loop that iterates through each of…
Nick Meyer
  • 39,212
  • 14
  • 67
  • 75
867
votes
16 answers

C# loop - break vs. continue

In a C# (feel free to answer for other languages) loop, what's the difference between break and continue as a means to leave the structure of the loop, and go to the next iteration? Example: foreach (DataRow row in myTable.Rows) { if…
Seibar
  • 68,705
  • 38
  • 88
  • 99
849
votes
15 answers

Syntax for a single-line while loop in Bash

I am having trouble coming up with the right combination of semicolons and/or braces. I'd like to do this, but as a one-liner from the command line: while [ 1 ] do foo sleep 2 done
Brian Deacon
  • 21,384
  • 13
  • 39
  • 41
842
votes
28 answers

What is the difference between range and xrange functions in Python 2.X?

Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about for i in range(0, 20): for i in xrange(0, 20):
Teifion
  • 108,121
  • 75
  • 161
  • 195
837
votes
7 answers

How to break out of jQuery each loop?

How do I break out of a jQuery each loop? I have tried: return false; in the loop but this did not work. Any ideas? Update 9/5/2020 I put the return false; in the wrong place. When I put it inside the loop everything worked.
Luke101
  • 63,072
  • 85
  • 231
  • 359
707
votes
13 answers

Ways to iterate over a list in Java

Being somewhat new to the Java language I'm trying to familiarize myself with all the ways (or at least the non-pathological ones) that one might iterate through a list (or perhaps other collections) and the advantages or disadvantages of each.…
jacobq
  • 11,209
  • 4
  • 40
  • 71
682
votes
40 answers

How to iterate over a list in chunks

I have a Python script which takes as input a list of integers, which I need to work with four integers at a time. Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples. Currently, I'm…
Ben Blank
  • 54,908
  • 28
  • 127
  • 156
671
votes
11 answers

Calling remove in foreach loop in Java

In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance: List names = .... for (String name : names) { // Do something names.remove(name). } As an addendum, is it…
Michael Bobick
  • 8,897
  • 5
  • 22
  • 13
647
votes
14 answers

How to find the foreach index?

Is it possible to find the foreach index? in a for loop as follows: for ($i = 0; $i < 10; ++$i) { echo $i . ' '; } $i will give you the index. Do I have to use the for loop or is there some way to get the index in the foreach loop?
user18334
  • 6,563
  • 2
  • 19
  • 7
631
votes
18 answers

Looping through array and removing items, without breaking for loop

I have the following for loop, and when I use splice() to remove an item, I then get that 'seconds' is undefined. I could check if it's undefined, but I feel there's probably a more elegant way to do this. The desire is to simply delete an item…
dzm
  • 22,844
  • 47
  • 146
  • 226
630
votes
18 answers

What's the best way to break from nested loops in JavaScript?

What's the best way to break from nested loops in Javascript? //Write the links to the page. for (var x = 0; x < Args.length; x++) { for (var Heading in Navigation.Headings) { for (var Item in Navigation.Headings[Heading]) { …
Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199
625
votes
19 answers

How to iterate over a JavaScript object?

I have an object in JavaScript: { abc: '...', bca: '...', zzz: '...', xxx: '...', ccc: '...', // ... } I want to use a for loop to get its properties. And I want to iterate it in parts (not all object properties at…
nkuhta
  • 10,388
  • 12
  • 41
  • 54
612
votes
21 answers

PHP How to determine the first and last iteration in a foreach loop?

The question is simple. I have a foreach loop in my code: foreach($array as $element) { //code } In this loop, I want to react differently when we are in first or last iteration. How to do this?
mehdi
  • 9,262
  • 11
  • 35
  • 35