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
69
votes
6 answers

How to break from nested loops in Ruby?

assume the following ruby code: bank.branches do |branch| branch.employees.each do |employee| NEXT BRANCH if employee.name = "John Doe" end end NEXT BRANCH is of course pseudocode. is there a way that i can break out of a parent loop, the…
crlsrns
  • 799
  • 1
  • 5
  • 5
69
votes
12 answers

Should I avoid using Java Label Statements?

Today I had a coworker suggest I refactor my code to use a label statement to control flow through 2 nested for loops I had created. I've never used them before because personally I think they decrease the readability of a program. I am willing to…
Aaron
  • 23,450
  • 10
  • 49
  • 48
69
votes
12 answers

JavaScript loop performance - Why is to decrement the iterator toward 0 faster than incrementing

In his book Even Faster Web Sites Steve Sounders writes that a simple way to improve the performance of a loop is to decrement the iterator toward 0 rather than incrementing toward the total length (actually the chapter was written by Nicholas C.…
Soundlink
  • 3,915
  • 2
  • 28
  • 36
69
votes
2 answers

How do I do a "break" or "continue" when in a functional loop within Kotlin?

In Kotlin, I cannot do a break or continue within a function loop and my lambda -- like I can from a normal for loop. For example, this does not work: (1..5).forEach { continue@forEach // not allowed, nor break@forEach } There are old…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
68
votes
6 answers

How to make a setInterval stop after some time or after a number of actions?

I've created a loop of "changing words" with jQuery by using the code in this answer: jQuery: Find word and change every few seconds How do I stop it after some time? Say after either 60 seconds or after it has gone through the loop? (function() { …
Alisso
  • 1,861
  • 1
  • 17
  • 32
68
votes
28 answers

Why use a for loop instead of a while loop?

Possible Duplicates: Iterate with for loop or while loop? Loops in C - for() or while() - which is BEST? When should one use a for loop instead of a while loop? I think the following loops are identical, except for their syntax. If so, then why…
Ziggy
  • 21,845
  • 28
  • 75
  • 104
68
votes
10 answers

Rails: An elegant way to display a message when there are no elements in database

I realized that I'm writing a lot of code similar to this one: <% unless @messages.blank? %> <% @messages.each do |message| %> <%# code or partial to display the message %> <% end %> <% else %> You have no messages. <% end %> Is there…
Jakub Troszok
  • 99,267
  • 11
  • 41
  • 53
67
votes
5 answers

Jquery, checking if a value exists in array or not

I believe this question will be fairly easy for the ones who played around with java script / jquery. var arr = new Array(); $.map(arr, function() { if (this.id == productID) { this.price = productPrice; }else { arr.push({id: productID,…
Revenant
  • 2,942
  • 7
  • 30
  • 52
67
votes
7 answers

Elegant way for do ... while in groovy

How to do code something like this in groovy? do { x.doIt() } while (!x.isFinished()) Because there is no do ... while syntax in groovy. No 'do ... while()' syntax as yet. Due to ambiguity, we've not yet added support for do .. while to…
MariuszS
  • 30,646
  • 12
  • 114
  • 155
67
votes
2 answers

How does OpenMP handle nested loops?

Does the following code just parallelize the first (outer) loops, or it parallelize the entire nested loops? #pragma omp parallel for for (int i=0;i
user0002128
  • 2,785
  • 2
  • 23
  • 40
67
votes
8 answers

Is using a 'goto' statement bad?

After doing some reseach on how to break through a secondary loop while (true) { // Main Loop for (int I = 0; I < 15; I++) { // Secondary loop // Do Something break; // Break main loop? } } most people recommended to call the…
Rasmus Søborg
  • 3,597
  • 4
  • 29
  • 46
67
votes
1 answer

Iterating over integer[] in PL/pgSQL

I am trying to loop through an integer array (integer[]) in a plpgsql function. Something like this: declare a integer[] = array[1,2,3]; i bigint; begin for i in a loop raise notice "% ",i; end loop; return true; end In my actual…
Dipro Sen
  • 4,350
  • 13
  • 37
  • 50
66
votes
8 answers

PHP: How do you determine every Nth iteration of a loop?

I wanted to echo an image every after 3 post via XML here is my code :
kwek-kwek
  • 1,343
  • 3
  • 19
  • 34
65
votes
7 answers

While loops using Await Async.

This Javascript function seems to use the while loop in an asynchronous way. Is it the correct way to use while loops with asynchronous conditions? var Boo; var Foo = await getBar(i) while(Foo) { Boo = await getBar3(i) if (Boo) { …
65
votes
4 answers

Empty loop is slower than a non-empty one in C

While trying to know how long a line of C code used to execute, I noticed this weird thing : int main (char argc, char * argv[]) { time_t begin, end; uint64_t i; double total_time, free_time; int A = 1; int B = 1; begin =…
Celerio
  • 803
  • 6
  • 11