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
219
votes
5 answers

Emulating a do-while loop in Bash

What is the best way to emulate a do-while loop in Bash? I could check for the condition before entering the while loop, and then continue re-checking the condition in the loop, but that's duplicated code. Is there a cleaner way? Pseudo code of my…
Alex
  • 10,470
  • 8
  • 40
  • 62
218
votes
16 answers

How do I count occurrence of unique values inside a list

So I'm trying to make this program that will ask the user for input and store the values in an array / list. Then when a blank line is entered it will tell the user how many of those values are unique. I'm building this for real life reasons and not…
Joel Aqu.
  • 2,289
  • 2
  • 13
  • 4
216
votes
5 answers

In Java, what are the advantages of streams over loops?

I was asked this at an interview and I'm not convinced I gave the best answer I could have. I mentioned that you can do a parallel search and that null values were handled by some means I couldn't remember. Now I realize I was thinking of Optionals.…
user447607
  • 5,149
  • 13
  • 33
  • 55
215
votes
18 answers

How to efficiently remove all null elements from a ArrayList or String Array?

I try with a loop like that // ArrayList tourists for (Tourist t : tourists) { if (t != null) { t.setId(idForm); } } But it isn't nice. Can anyone suggest me a better solution? Some useful benchmarks to make better…
Juan de Dios
  • 2,683
  • 2
  • 20
  • 24
211
votes
21 answers

Should try...catch go inside or outside a loop?

I have a loop that looks something like this: for (int i = 0; i < max; i++) { String myString = ...; float myNum = Float.parseFloat(myString); myFloats[i] = myNum; } This is the main content of a method whose sole purpose is to return…
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
210
votes
10 answers

Speed up the loop operation in R

I have a big performance problem in R. I wrote a function that iterates over a data.frame object. It simply adds a new column to a data.frame and accumulates something. (simple operation). The data.frame has roughly 850K rows. My PC is still working…
Kay
  • 2,109
  • 3
  • 13
  • 3
208
votes
13 answers

foreach vs someList.ForEach(){}

There are apparently many ways to iterate over a collection. Curious if there are any differences, or why you'd use one way over the other. First type: List someList = foreach(string s in someList) {
Bryce Fischer
  • 5,336
  • 9
  • 30
  • 36
208
votes
8 answers

How do I loop through a set of records in SQL Server?

How do I loop through a set of records from a select statement? Say I have a few records that I wish to loop through and do something with each record. Here's a primitive version of my select statement: select top 1000 * from dbo.table where…
Funky
  • 12,890
  • 35
  • 106
  • 161
208
votes
11 answers

How to loop through an associative array and get the key?

My associative array: $arr = array( 1 => "Value1", 2 => "Value2", 10 => "Value10" ); Using the following code, $v is filled with $arr's values foreach ($arr as $v){ echo $v; // Value1, Value2, Value10 } How do I get $arr's keys…
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
207
votes
11 answers

Null check in an enhanced for loop

What is the best way to guard against null in a for loop in Java? This seems ugly : if (someList != null) { for (Object object : someList) { // do whatever } } Or if (someList == null) { return; // Or throw ex } for (Object…
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
206
votes
9 answers

How do you create different variable names while in a loop?

For example purposes... for x in range(0,9): string'x' = "Hello" So I end up with string1, string2, string3... all equaling "Hello"
Takkun
  • 8,119
  • 13
  • 38
  • 41
197
votes
9 answers

Python: Continuing to next iteration in outer loop

I wanted to know if there are any built-in ways to continue to next iteration in outer loop in python. For example, consider the code: for ii in range(200): for jj in range(200, 400): ...block0... if something: …
Sahas
  • 10,637
  • 9
  • 41
  • 51
196
votes
11 answers

Why does Lua have no "continue" statement?

I have been dealing a lot with Lua in the past few months, and I really like most of the features but I'm still missing something among those: Why is there no continue? What workarounds are there for it?
Dant
  • 1,961
  • 2
  • 12
  • 3
193
votes
6 answers

How to create an infinite loop in Windows batch file?

This is basically what I want in a batch file. I want to be able to re-run "Do Stuff" whenever I press any key to go past the "Pause". while(true){ Do Stuff Pause } Looks like there are only for loops available and no while loops in…
sooprise
  • 22,657
  • 67
  • 188
  • 276
193
votes
4 answers

Is there a "do ... until" in Python?

Is there a do until x: ... in Python, or a nice way to implement such a looping construct?
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526