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
297
votes
9 answers

How can I loop through a C++ map of maps?

How can I loop through a std::map in C++? My map is defined as: std::map< std::string, std::map > For example, the above container holds data like this: m["name1"]["value1"] = "data1"; m["name1"]["value2"] =…
Jack
  • 3,769
  • 6
  • 24
  • 32
295
votes
34 answers

Are loops really faster in reverse?

I've heard this quite a few times. Are JavaScript loops really faster when counting backward? If so, why? I've seen a few test suite examples showing that reversed loops are quicker, but I can't find any explanation as to why! I'm assuming it's…
djdd87
  • 67,346
  • 27
  • 156
  • 195
293
votes
10 answers

For every character in string

How would I do a for loop on every character in string in C++?
Jack Wilsdon
  • 6,706
  • 11
  • 44
  • 87
286
votes
12 answers

`break` and `continue` in `forEach` in Kotlin

Kotlin has very nice iterating functions, like forEach or repeat, but I am not able to make the break and continue operators work with them (both local and non-local): repeat(5) { break } (1..5).forEach { continue@forEach } The goal is to…
voddan
  • 31,956
  • 8
  • 77
  • 87
285
votes
12 answers

How to loop through array in jQuery?

I am trying to loop through an array. I have the following code: var currnt_image_list= '21,32,234,223'; var substr = currnt_image_list.split(','); // array here Am trying to get all the data out of the array. Can some one lead me in the right…
Rickstar
  • 6,057
  • 21
  • 55
  • 74
284
votes
13 answers

Is there a way to iterate over a range of integers?

Go's range can iterate over maps and slices, but I was wondering if there is a way to iterate over a range of numbers, something like this: for i := range [1..10] { fmt.Println(i) } Or is there a way to represent range of integers in Go like…
Vishnu
  • 4,377
  • 7
  • 26
  • 40
283
votes
16 answers

How to write loop in a Makefile?

I want to execute the following commands: ./a.out 1 ./a.out 2 ./a.out 3 ./a.out 4 . . . and so on How to write this thing as a loop in a Makefile?
avd
  • 13,993
  • 32
  • 78
  • 99
257
votes
17 answers

How do I loop through a date range?

I'm not even sure how to do this without using some horrible for loop/counter type solution. Here's the problem: I'm given two dates, a start date and an end date and on a specified interval I need to take some action. For example: for every date…
Paul Mignard
  • 5,824
  • 6
  • 44
  • 60
253
votes
9 answers

How do I apply the for-each loop to every character in a String?

So I want to iterate for each character in a string. So I thought: for (char c : "xyz") but I get a compiler error: MyClass.java:20: foreach not applicable to expression type How can I do this?
Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
252
votes
9 answers

How do you run a command eg chmod, for each line of a file?

For example, right now I'm using the following to change a couple of files whose Unix paths I wrote to a file: cat file.txt | while read in; do chmod 755 "$in"; done Is there a more elegant, safer way?
hawk
  • 2,655
  • 2
  • 16
  • 11
246
votes
7 answers

Does return stop a loop?

If I have the following for loop for (var i = 0; i < SomeArrayOfObject.length; i++) { if (SomeArray[i].SomeValue === SomeCondition) { var SomeVar = SomeArray[i].SomeProperty; return SomeVar; } } Does…
frenchie
  • 51,731
  • 109
  • 304
  • 510
240
votes
10 answers

How do I step out of a loop with Ruby Pry?

I'm using Pry with my Rails application. I set binding.pry inside a loop in my model to try and debug a problem. For example: (1..100).each do |i| binding.pry puts i end When I type quit, it goes to the next iteration and stops again. Is there…
Ryan
  • 9,340
  • 5
  • 39
  • 42
239
votes
16 answers

JavaScript: Difference between .forEach() and .map()

I know that there were a lot of topics like this. And I know the basics: .forEach() operates on original array and .map() on the new one. In my case: function practice (i){ return i+1; }; var a = [ -1, 0, 1, 2, 3, 4, 5 ]; var b = [ 0 ]; var c =…
DzikiChrzan
  • 2,747
  • 2
  • 15
  • 22
234
votes
15 answers

Is it possible to make a `for` loop without an iterator variable? (How can I make make code loop a set number of times?)

Is it possible to do following without the i? for i in range(some_number): # do something If you just want to do something N amount of times and don't need the iterator.
James McMahon
  • 48,506
  • 64
  • 207
  • 283
227
votes
9 answers

"for" vs "each" in Ruby

I just had a quick question regarding loops in Ruby. Is there a difference between these two ways of iterating through a collection? # way 1 @collection.each do |item| # do whatever end # way 2 for item in @collection # do whatever end Just…
mportiz08
  • 10,206
  • 12
  • 40
  • 42