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
8
votes
3 answers

use existing variable in for loop

Is it possible to leave out the variable assignment from a for loop and do something like this…? otherVar = 3; for ( otherVar > 0; otherVar-- ) { stuff }
Essential
  • 301
  • 4
  • 13
8
votes
4 answers

Objective-C NSString for loop with characterAtIndex

I'm trying to loop through a NSString, character by character, but I'm getting a EXC_BAD_ACCESS error. Do you have an idea how to do this right? I've been googling for hours now but can't figure it out. Here is my code (.m): self.textLength =…
Linus
  • 4,092
  • 7
  • 44
  • 68
8
votes
2 answers

How does this for loop work? for ( ; i < length; i++ )

How does this for loop work? It doesn't make sense to me. for ( ; i < length; i++ ) {
Jackie Chan
  • 2,507
  • 6
  • 33
  • 65
8
votes
3 answers

PHP: Best way to iterate two parallel arrays?

As seen in this other answer, there are several ways to iterate two same-sized arrays simultaneously; however, all of the methods have a rather significant pitfall. Here are some of the caveats with the methods suggested: You can't use FALSE…
FtDRbwLXw6
  • 25,206
  • 12
  • 61
  • 102
7
votes
1 answer

how to set counter of loop inside loop correctly inside jmeter?

I have jmeter flow like this: ThreadGroup --Sampler to get the number of items and store to vars("numItem",XYZ) --LoopController on $numItem -----Sampler to get number of subItem and store to vars("numSubitem", ABC) -----LoopController on…
Sean Nguyen
  • 10,990
  • 18
  • 66
  • 101
7
votes
2 answers

How to create update/draw loop appropriately with android opengl?

I wanted to write some simple game in android using opengl es but immediately ran into trouble with the main game loop. As I read here: http://developer.android.com/resources/tutorials/opengl/opengl-es10.html the app calls public void…
NPS
  • 5,261
  • 8
  • 46
  • 80
7
votes
5 answers

How to implement subtraction using only loop and increment

This is an interview question. We have only two constructs loop(a) means loop for a times. increment(a) increments a. Thus to implement a+b one could write loop(a) {inc(b)} return b; The question is how to implement a-b.
Neal
  • 2,859
  • 3
  • 25
  • 28
7
votes
1 answer

Iterating through each text element in a page?

I'm trying to write a script in jQuery that would iterate through each text element inside a page. Then I need to change the color of each letter one by one. For example, for a page such as this one:

Some text and

laurent
  • 79,308
  • 64
  • 256
  • 389
7
votes
8 answers

Manually increment an enumerator inside foreach loop

I have a nested while loop inside a foreach loop where I would like to advance the enumerator indefinitately while a certain condition is met. To do this I try casting the enumerator to IEnumerator< T > (which it must be if it is in a foreach loop)…
Despertar
  • 19,416
  • 7
  • 69
  • 74
7
votes
3 answers

Java BufferedReader check next lines of a loop before looping

I'm parsing a .cvs file. For each line of the cvs, I create an object with the parsed values, and put them into a set. Before putting the object in the map and looping to the next, I need to check if the next cvs's line is the same object as the…
André Perazzi
  • 999
  • 1
  • 10
  • 26
7
votes
8 answers

php looping through multiple arrays

okay so I have two arrays $array_one([a]=>2,[b]=>1,[c]=>1); $array_two([a]=>1,[b]=>2,[c]=>1); I want to be able to loop through both of these arrays simultaneously so I can make simple comparisons. I looked at using a foreach loop but I can only…
jkdba
  • 1,874
  • 3
  • 17
  • 29
7
votes
3 answers

JS ArrowDown addEventListener for several elements in loop (demo)

Have following listener for keyboard ArrowDown event(it's key code is 40): window.onload = function() { var itemsContainer = document.getElementById('cities-drop'); document.addEventListener('keyup',function(event){ if (event.keyCode == 40 &&…
sergionni
  • 12,462
  • 41
  • 121
  • 182
7
votes
5 answers

Optimizing a Loop vs Code Duplication

My dilemma concerns how to best handle long heavy loops which can accept parameters. Consider the following method: void HeavyLoop(byte* startingAddress, bool secondaryModification) { for (int i = 0; i < 10000000; i++) { byte* b =…
Rotem
  • 19,723
  • 6
  • 57
  • 101
7
votes
1 answer

Limiting the time that a function processes in an R for loop

I want to apply a function ("foo" for this explanation) to tranform a vector of data into another value. This function takes the data as an input, and needs submit forms to webpages. Sometimes, this goes quickly, and other times, it can a long…
exl
  • 1,493
  • 2
  • 15
  • 25
7
votes
4 answers

Java - How do I refer to the previous and next element during an iteration?

When I have a for loop, I use the i to refer to the elements of my array, objects, etc. Like: Current item: myArray[i] Next item: myArray[i+1] Previous item: myArray[i-1] But at the moment, I'm using a foreach loop ( for (Object elem : col) {…
Apache
  • 956
  • 5
  • 16
  • 38
1 2 3
99
100