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
380
votes
8 answers

How do I skip an iteration of a `foreach` loop?

In Perl I can skip a foreach (or any loop) iteration with a next; command. Is there a way to skip over an iteration and jump to the next loop in C#? foreach (int number in numbers) { if (number < 0) { // What goes here to skip…
Brian
  • 8,147
  • 8
  • 34
  • 29
379
votes
19 answers

Loop backwards using indices

I am trying to loop from 100 to 0. How do I do this in Python? for i in range (100,0) doesn't work. For discussion of why range works the way it does, see Why are slice and range upper-bound exclusive?.
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
376
votes
2 answers

In Ruby, how do I skip a loop in a .each loop, similar to 'continue'

In Ruby, how do I skip a loop in a .each loop, similar to continue in other languages?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
374
votes
13 answers

Is recursion ever faster than looping?

I know that recursion is sometimes a lot cleaner than looping, and I'm not asking anything about when I should use recursion over iteration, I know there are lots of questions about that already. What I'm asking is, is recursion ever faster than a…
Carson Myers
  • 37,678
  • 39
  • 126
  • 176
373
votes
13 answers

Is there a difference between "pass" and "continue" in a for loop in Python?

Is there any significant difference between the two Python keywords continue and pass like in the examples for element in some_list: if not element: pass and for element in some_list: if not element: continue I should be…
Aufwind
  • 25,310
  • 38
  • 109
  • 154
352
votes
16 answers

Batch script loop

I need to execute a command 100-200 times, and so far my research indicates that I would either have to copy/paste 100 copies of this command, OR use a for loop, but the for loop expects a list of items, hence I would need 200 files to operate on,…
Tom J Nowell
  • 9,588
  • 17
  • 63
  • 91
345
votes
26 answers

Difference between declaring variables before or in loop?

I have always wondered if, in general, declaring a throw-away variable before a loop, as opposed to repeatedly inside the loop, makes any (performance) difference? A (quite pointless) example in Java: a) declaration before loop: double…
Rabarberski
  • 23,854
  • 21
  • 74
  • 96
331
votes
16 answers

Is there a way to access an iteration-counter in Java's for-each loop?

Is there a way in Java's for-each loop for(String s : stringArray) { doSomethingWith(s); } to find out how often the loop has already been processed? Aside from using the old and well-known for(int i=0; i < boundary; i++) - loop, is the…
Kosi2801
  • 22,222
  • 13
  • 38
  • 45
323
votes
8 answers

How to Iterate over a Set/HashSet without an Iterator?

How can I iterate over a Set/HashSet without the following? Iterator iter = set.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); }
user1621988
  • 4,215
  • 8
  • 27
  • 31
321
votes
5 answers

How do I break out of a loop in Perl?

I'm trying to use a break statement in a for loop, but since I'm also using strict subs in my Perl code, I'm getting an error saying: Bareword "break" not allowed while "strict subs" in use at ./final.pl line 154. Is there a workaround for…
Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
319
votes
25 answers

What's the fastest way to loop through an array in JavaScript?

I learned from books that you should write for loop like this: for(var i=0, len=arr.length; i < len; i++){ // blah blah } so the arr.length will not be calculated each time. Others say that the compiler will do some optimization to this, so…
wong2
  • 34,358
  • 48
  • 134
  • 179
318
votes
10 answers

Syntax of for-loop in SQL Server

What is the syntax of a for loop in TSQL?
Macho
  • 3,307
  • 3
  • 16
  • 6
305
votes
7 answers

How do I loop through a list by twos?

I want to loop through a Python list and process 2 list items at a time. Something like this in another language: for(int i = 0; i < list.length(); i+=2) { // do something with list[i] and list[i + 1] } What's the best way to accomplish this?
froadie
  • 79,995
  • 75
  • 166
  • 235
303
votes
6 answers

How to do something to each file in a directory with a batch script

How do you iterate over each file in a directory with a .bat or .cmd file? For simplicity please provide an answer that just echoes the filename or file path.
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
301
votes
21 answers

Is there a way to loop through a table variable in TSQL without using a cursor?

Let's say I have the following simple table variable: declare @databases table ( DatabaseID int, Name varchar(15), Server varchar(15) ) -- insert a bunch rows into @databases Is declaring and using a cursor my only…
Ray
  • 187,153
  • 97
  • 222
  • 204