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
161
votes
10 answers

Numpy how to iterate over columns of array?

Suppose I have and m x n array. I want to pass each column of this array to a function to perform some operation on the entire column. How do I iterate over the columns of the array? For example, I have a 4 x 3 array like 1 99 2 2 14 5 3 12 7 4…
User
  • 62,498
  • 72
  • 186
  • 247
159
votes
20 answers

How to break out of a loop from inside a switch?

I'm writing some code that looks like this: while(true) { switch(msg->state) { case MSGTYPE: // ... break; // ... more stuff ... case DONE: break; // **HERE, I want to break out of the loop itself** } } Is there…
jrharshath
  • 25,975
  • 33
  • 97
  • 127
158
votes
1 answer

Why does using from __future__ import print_function breaks Python2-style print?

I am new at programming with python, and I am trying to print out with a separator and end but it is still giving me a syntax error. I am using python 2.7. Here is my code: from __future__ import print_function import sys, os, time for x in…
UHMIS
  • 1,701
  • 2
  • 11
  • 5
158
votes
9 answers

Iterating through a JSON object

I am trying to iterate through a JSON object to import data, i.e. title and link. I can't seem to get to the content that is past the :. JSON: [ { "title": "Baby (Feat. Ludacris) - Justin Bieber", "description": "Baby (Feat.…
Mahdi Yusuf
  • 19,931
  • 26
  • 72
  • 101
158
votes
4 answers

Python loop counter in a for loop

In my example code below, is the counter = 0 really required, or is there a better, more Python, way to get access to a loop counter? I saw a few PEPs related to loop counters, but they were either deferred or rejected (PEP 212 and PEP 281). This is…
Andre Miller
  • 15,255
  • 6
  • 55
  • 53
157
votes
17 answers

How to early break reduce() method?

How can I break the iteration of reduce() method? for: for (var i = Things.length - 1; i >= 0; i--) { if(Things[i] <= 0){ break; } }; reduce() Things.reduce(function(memo, current){ if(current <= 0){ //break ??? //return; <-- this…
Julio Marins
  • 10,039
  • 8
  • 48
  • 54
155
votes
6 answers

How do you remove an array element in a foreach loop?

I want to loop through an array with foreach to check if a value exists. If the value does exist, I want to delete the element which contains it. I have the following code: foreach($display_related_tags as $tag_name) { if($tag_name ==…
ajsie
  • 77,632
  • 106
  • 276
  • 381
154
votes
3 answers

How to loop through a HashMap in JSP?

How can I loop through a HashMap in JSP? <% HashMap countries = MainUtils.getCountries(l); %>
blub
  • 1,541
  • 2
  • 10
  • 3
154
votes
8 answers

Iterator Loop vs index loop

Possible Duplicate: Why use iterators instead of array indices? I'm reviewing my knowledge on C++ and I've stumbled upon iterators. One thing I want to know is what makes them so special and I want to know why this: using namespace…
CodingMadeEasy
  • 2,257
  • 4
  • 19
  • 31
151
votes
27 answers

How to split a long array into smaller arrays, with JavaScript

I have an array of e-mails (it can be just 1 email, or 100 emails), and I need to send the array with an ajax request (that I know how to do), but I can only send an array that has 10 or less e-mails in it. So if there is an original array of 20…
Bill
  • 5,478
  • 17
  • 62
  • 95
151
votes
4 answers

Check if list contains any of another list

I have a list of parameters like this: public class parameter { public string name {get; set;} public string paramtype {get; set;} public string source {get; set;} } IEnumerable parameters; And a array of strings i want to…
gdp
  • 8,032
  • 10
  • 42
  • 63
147
votes
4 answers

Is the "*apply" family really not vectorized?

So we are used to say to every R new user that "apply isn't vectorized, check out the Patrick Burns R Inferno Circle 4" which says (I quote): A common reflex is to use a function in the apply family. This is not vectorization, it is loop-hiding.…
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
146
votes
12 answers

Iterating through a list to render multiple widgets in Flutter?

I have a list of strings defined like this: var list = ["one", "two", "three", "four"]; I want to render the values on the screen side by side using text widgets. I have attempted to use the following code to attempt this: for (var name in list)…
rakeshdas
  • 2,363
  • 5
  • 20
  • 28
145
votes
6 answers

Iterate through adjacent pairs of items in a Python list

Is it possible to iterate a list in the following way in Python (treat this code as pseudocode)? a = [5, 7, 11, 4, 5] for v, w in a: print [v, w] And it should produce [5, 7] [7, 11] [11, 4] [4, 5]
marvin_yorke
  • 3,469
  • 4
  • 25
  • 35
144
votes
8 answers

Check for null in foreach loop

Is there a nicer way of doing the following: I need a check for null to happen on file.Headers before proceeding with the loop if (file.Headers != null) { foreach (var h in file.Headers) { //set lots of properties & some other stuff …
Eminem
  • 7,206
  • 15
  • 53
  • 95