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

81874 questions
5023
votes
40 answers

For-each over an array in JavaScript

How can I loop through all the entries in an array using JavaScript? I thought it was something like this: forEach(instance in theArray) Where theArray is my array, but this seems to be incorrect.
Dante1986
  • 52,129
  • 12
  • 34
  • 53
4124
votes
22 answers

Accessing the index in 'for' loops?

How do I access the index in a for loop like the following? ints = [8, 23, 45, 12, 78] for i in ints: print('item #{} = {}'.format(???, i)) I want to get this output: item #1 = 8 item #2 = 23 item #3 = 45 item #4 = 12 item #5 = 78 When I loop…
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
4012
votes
32 answers

How to enumerate an enum

How can you enumerate an enum in C#? E.g. the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { …
Ian Boyd
  • 220,884
  • 228
  • 805
  • 1,125
3532
votes
7 answers

Iterate through a HashMap

What's the best way to iterate over the items in a HashMap?
burntsugar
  • 54,120
  • 21
  • 51
  • 77
3451
votes
41 answers

Loop through an array in JavaScript

In Java you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can you do the same in JavaScript?
Mark Szymanski
  • 50,182
  • 22
  • 66
  • 87
3161
votes
41 answers

How do I loop through or enumerate a JavaScript object?

I have a JavaScript object like the following: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; Now I want to loop through all p elements (p1, p2, p3...) And get their keys and values. How can I do that? I can modify the…
Tanmoy
  • 39,410
  • 14
  • 42
  • 52
2984
votes
44 answers

JavaScript closure inside loops – simple practical example

var funcs = []; // let's create 3 functions for (var i = 0; i < 3; i++) { // and store them in funcs funcs[i] = function() { // each should log its value. console.log("My value: " + i); }; } for (var j = 0; j < 3; j++) { // and now…
nickf
  • 499,078
  • 194
  • 614
  • 709
2856
votes
30 answers

What is the best way to iterate over a dictionary?

I've seen a few different ways to iterate over a dictionary in C#. Is there a standard way?
Jake Stewart
  • 28,753
  • 3
  • 18
  • 14
2831
votes
3 answers

Why is printing "B" dramatically slower than printing "#"?

I generated two matrices of 1000 x 1000: First Matrix: O and #. Second Matrix: O and B. Using the following code, the first matrix took 8.52 seconds to complete: Random r = new Random(); for (int i = 0; i < 1000; i++) { for (int j = 0; j < 1000;…
Kuba Spatny
  • 24,958
  • 9
  • 33
  • 58
2182
votes
30 answers

Iterate through object properties

var obj = { name: "Simon", age: "20", clothing: { style: "simple", hipster: false } } for(var propt in obj){ console.log(propt + ': ' + obj[propt]); } How does the variable propt represent the…
Rafay
  • 22,175
  • 4
  • 18
  • 27
2126
votes
7 answers

How does PHP 'foreach' actually work?

Let me prefix this by saying that I know what foreach is, does and how to use it. This question concerns how it works under the bonnet, and I don't want any answers along the lines of "this is how you loop an array with foreach". For a long time I…
DaveRandom
  • 84,004
  • 11
  • 142
  • 168
1922
votes
27 answers

Why is using "for...in" for array iteration a bad idea?

I've been told not to use for...in with arrays in JavaScript. Why not?
lYriCAlsSH
  • 54,286
  • 10
  • 24
  • 20
1908
votes
36 answers

How do I break out of nested loops in Java?

I've got a nested loop construct like this: for (Type type : types) { for (Type t : types2) { if (some condition) { // Do something and break... break; // Breaks out of the inner loop } } } Now…
boutta
  • 22,571
  • 7
  • 33
  • 45
1678
votes
25 answers

How to loop through a plain JavaScript object with the objects as members?

How can I loop through all members in a JavaScript object including values that are objects. For example, how could I loop through this (accessing the "your_name" and "your_message" for each)? var validation_messages = { "key_1": { …
edt
  • 20,403
  • 27
  • 78
  • 114
1616
votes
15 answers

Looping through the content of a file in Bash

How do I iterate through each line of a text file with Bash? With this script: echo "Start!" for p in (peptides.txt) do echo "${p}" done I get this output on the screen: Start! ./runPep.sh: line 3: syntax error near unexpected token…
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
1
2 3
99 100