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

How to break out of multiple loops at once in C#?

What if I have nested loops, and I want to break out of all of them at once? while (true) { // ... while (shouldCont) { // ... while (shouldGo) { // ... if (timeToStop) { break; //…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
76
votes
3 answers

iterate vector, remove certain items as I go

I have a std::vector m_vPaths; I will iterate this vector and call ::DeleteFile(strPath) as I go. If I successfully delete the file, I will remove it from the vector. My question is can I get around having to use two vectors? Is there different…
cchampion
  • 7,607
  • 11
  • 41
  • 51
75
votes
4 answers

do-while loop in R

I was wondering about how to write do-while-style loop? I found this post: you can use repeat{} and check conditions whereever using if() and exit the loop with the "break" control word. I am not sure what it exactly means. Can someone please…
Tim
  • 1
  • 141
  • 372
  • 590
75
votes
2 answers

How to iterate through property names of Javascript object?

I would like to get the property names from a Javascript object to build a table dynamically. Example: var obj = {'fname': 'joe', 'lname': 'smith', 'number': '34'}; for (var i = 0; i < obj.properties.length; i++) { alert(' name=' +…
eiu165
  • 6,101
  • 10
  • 41
  • 59
74
votes
6 answers

How does a for loop work, specifically for(;;)?

Looking through some old company code, I came across a for loop that looks like this: for (;;) { //Some stuff } I tried Google but couldn't find any answers. Did I fall asleep in a programming class or is this an unusual loop?
Frank
  • 1,110
  • 2
  • 16
  • 21
74
votes
7 answers

Using command line argument range in bash for loop prints brackets containing the arguments

It's probably a lame question. But I am getting 3 arguments from command line [ bash script ]. Then I am trying to use these in a for loop. for i in {$1..$2} do action1 done This doesn't seem to work though and if $1 is "0" and $2 is 2 it…
ru4mqart668op514
  • 743
  • 1
  • 5
  • 5
74
votes
8 answers

Single line for loop over iterator with an "if" filter?

I have a simple for loop followed by a simple if statement: for airport in airports: if airport.is_important: and I was wondering if I can write this as a single line somehow. Yes, I can do this: for airport in (airport for airport in airports…
Tal Weiss
  • 8,889
  • 8
  • 54
  • 62
73
votes
9 answers

Continue For loop

I have the following code: For x = LBound(arr) To UBound(arr) sname = arr(x) If InStr(sname, "Configuration item") Then '**(here I want to go to next x in loop and not complete the code below)** End If '// other…
DevilWAH
  • 2,553
  • 13
  • 41
  • 57
73
votes
7 answers

Modifying list while iterating

l = range(100) for i in l: print i, print l.pop(0), print l.pop(0) The above python code gives the output quite different from expected. I…
Xolve
  • 22,298
  • 21
  • 77
  • 125
73
votes
3 answers

Bash scripting, multiple conditions in while loop

I'm trying to get a simple while loop working in bash that uses two conditions, but after trying many different syntax from various forums, I can't stop throwing an error. Here is what I have: while [ $stats -gt 300 ] -o [ $stats -eq 0 ] I have…
jake9115
  • 3,964
  • 12
  • 49
  • 78
73
votes
10 answers

What is the difference between "++" and "+= 1 " operators?

In a loop in C++, I usually encounter situations to use ++ or +=1, but I can't tell their difference. For instance, if I have an integer int num = 0; and then in a loop I do: num ++; or num += 1; they both increase the value of num, but what is…
E_learner
  • 3,512
  • 14
  • 57
  • 88
73
votes
7 answers

Retry a Bash command with timeout

How to retry a bash command until its status is ok or until a timeout is reached? My best shot (I'm looking for something simpler): NEXT_WAIT_TIME=0 COMMAND_STATUS=1 until [ $COMMAND_STATUS -eq 0 || $NEXT_WAIT_TIME -eq 4 ]; do command …
Philippe Blayo
  • 10,610
  • 14
  • 48
  • 65
70
votes
3 answers

Run command every second in Bash?

I want to write some image downloader and assign it on bash. What I have and what I need: I have: Command, which works fine (something like wget http://mywebcam.com/image.jpg -O /var/cam/Image.jpg) Root rights Fast Internet line between my server…
Egor Sazanovich
  • 4,979
  • 5
  • 23
  • 37
70
votes
15 answers

Recursion vs loops

I'm facing a problem where both recursion and using a loop seem like natural solutions. Is there a convention or "preferred method" for cases like this? (Obviously it is not quite as simple as below) Recursion Item Search(string desired, Scope…
zildjohn01
  • 11,339
  • 6
  • 52
  • 58
70
votes
7 answers

Iterating over a 2 dimensional python list

I have created a 2 dimension array like: rows =3 columns= 2 mylist = [[0 for x in range(columns)] for x in range(rows)] for i in range(rows): for j in range(columns): mylist[i][j] = '%s,%s'%(i,j) print mylist Printing this list gives an…
bhaskarc
  • 9,269
  • 10
  • 65
  • 86