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

Print 2-D Array in clockwise expanding spiral from center

I have an guaranteed to be a perfect square matrix. I want to start at the center of the matrix in this case it would be matrix[2][2], I know how to figure the center (int)(dimensions / 2). I need to output the contents of the array in this…
user3328187
  • 193
  • 2
  • 2
  • 6
9
votes
3 answers

javascript timer or intervals created in loop using closure

I'm using jQuery to setup a timer or interval loop on a few elements to check them every couple seconds. I've tried setting a timer and checking if I should restart it, or setting and interval and checking if I should stop it. Although simplified,…
phazei
  • 5,323
  • 5
  • 42
  • 46
9
votes
1 answer

Loop optimizations Oracle Java 7-8 Hotspot VM

I would like to know what are the loop optimizations performed by Oracle Java 7 (or 8) Hotspot VM?
El Marce
  • 3,144
  • 1
  • 26
  • 40
9
votes
4 answers

Image change every 30 seconds - loop

I would like to make an image change after 30 seconds. The javascript I'm using looks like this: var images = new Array(); images[0] = "image1.jpg"; images[1] = "image2.jpg"; images[2] = "image3.jpg"; setTimeout("changeImage()", 30000); var x =…
Latze
  • 1,843
  • 7
  • 22
  • 29
9
votes
1 answer

Using Cursor in a Loop of a stored procedure

So as to use cursors dynamically using MySQL is it possible to declare a cursor in a loop of a stored procedure? I've tried and got an error: increment: LOOP DECLARE cur1 CURSOR FOR SELECT person_id, publication_id FROM p_publication WHERE…
9
votes
7 answers

Split Integer into two separate Integers

Suppose I have int n=123456; int x,y=0; How do I split the integer "n" in two half. Note : The Total Number of digits in n will always be multiple of 2, e.g. 1234, 4567, 234567, 345621 etc... all have 2,4,6,8 digits. I want to divide them in…
Prateek
  • 189
  • 1
  • 3
  • 13
9
votes
2 answers

How can I loop through all RGB combinations in rainbow order in Java?

I'm trying to loop through all rgb colours in rainbow order. Currently I have this: int state = 1; int a = 255; int r = 255; int g = 0; int b = 0; if(g < 255 && state == 1){ g++; r--; if(g == 255) state = 2; } if(b < 255 &&…
user3166950
  • 315
  • 3
  • 6
  • 15
9
votes
3 answers

Vectorize iterative addition in NumPy arrays

For each element in a randomized array of 2D indices (with potential duplicates), I want to "+=1" to the corresponding grid in a 2D zero array. However, I don't know how to optimize the computation. Using the standard for loop, as shown here, def…
neither-nor
  • 1,245
  • 2
  • 17
  • 30
9
votes
6 answers

How can I loop scraping data for multiple pages in a website using python and beautifulsoup4

I am trying to scrape data from the PGA.com website to get a table of all of the golf courses in the United States. In my CSV table I want to include the Name of the golf course ,Address ,Ownership ,Website , Phone number. With this data I would…
Gonzalo68
  • 431
  • 4
  • 11
  • 22
9
votes
3 answers

Calculating total width of all list items?

I have a standard UL as follows: I am trying to find a quick & efficient way to…
Fred
  • 1,021
  • 5
  • 13
  • 29
9
votes
4 answers

While loop won't exit Java

I'm entering a huge number and adding each line of numbers to the string "hold". Once there's nothing left to read it should exit the while loop and turn the string to a BigInteger. It will run through the loop till there is nothing left then…
9
votes
4 answers

Looping through lists, better method

As I’m coding and discovering new ways of doing things in Java, I’m always somewhat perplexed in the better method of looping through lists to output data. In the following example, I’m looping through lists and using a counter, so many times I’ve…
GeekyDaddy
  • 384
  • 2
  • 12
9
votes
4 answers

for (Object object : list) [java] and index element

Is there a way to get an element id of a list to get it later through list.get(index) when using for(Object obj: o) construction Only define a local var and manually incrementing it? Anything simpler?
EugeneP
  • 11,783
  • 32
  • 96
  • 142
9
votes
2 answers

Python: iterate through a list

I´ve a mind challenge riddle that i want to resolve using python. They give 4 numbers (25, 28, 38, 35) and they want us to place the numbers in ...+...-...=... One possible solution is 25+38-35=28. I´ve tried to, making a list from the numbers,…
Luis
  • 95
  • 4
9
votes
6 answers

Create variable names using a loop in Java?

first time poster, long time reader so be gentle with me :) See the following code which works to generate me timestamps for the beginning and end of every month in a financial year. int year = 2010; // Financial year runs from Sept-Aug so…
SeerUK
  • 497
  • 2
  • 5
  • 10