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

Practice for breaking or returning in a loop

Background: I am making a chess game. It works almost completely, just missing the check for checkmate, but I am refining some code for readability, etc. Right now, I am recoding a method I had to check the path from any piece, to another location…
Matt C
  • 4,470
  • 5
  • 26
  • 44
9
votes
1 answer

C++ - using glfwGetTime() for a fixed time step

After doing some research and debugging in my C++ project with glfwGetTime(), I'm having trouble making a game loop for my project. As far as time goes I really only worked with nanoseconds in Java and on the GLFW website it states that the function…
tcoy
  • 179
  • 1
  • 3
  • 11
9
votes
1 answer

loop tiling. how to choose block size?

I am trying to learn the loop optimization. i found that loop tiling helps in making the array looping faster. i tried with two block of codes given below with and without loop blocking and measure the time taken for both. i did not find significant…
Sagar
  • 1,115
  • 2
  • 13
  • 22
9
votes
1 answer

Accessing next element in range based for loop before the next loop

How do I access the next element in range based for loop before the next loop? I know with an iterative approach you do something like arr[++i], but how do I achieve the same result in a range-based for loop? for (auto& cmd : arr) { steps =…
theintellects
  • 1,320
  • 2
  • 16
  • 28
9
votes
3 answers

Is there a way to show where LLVM is auto vectorising?

Context: I have several loops in an Objective-C library I am writing which deal with processing large text arrays. I can see that right now it is running in a single threaded manner. I understand that LLVM is now capable of auto-vectorising loops,…
Swizzlr
  • 447
  • 4
  • 15
9
votes
3 answers

Breakpoint in loop after large number of iterations in Eclipse

Suppose I have the following code. While debugging, I want Eclipse to stop when it has done 1 million iterations. How to do this? I cannot manually do 1 million times. for(int i = 0; i < 10000000; i++) { //some code }
codepk
  • 605
  • 2
  • 10
  • 23
9
votes
1 answer

Using += for strings in a loop, is it bad practice?

I saw this method of string building used in another post which has since been removed. One of the comments described the practice as "career limiting" Why is this so?
andrew
  • 9,313
  • 7
  • 30
  • 61
9
votes
3 answers

How to represent an interrupt in a UML sequence diagram?

I want to design a simple embedded system. The special thing about this is that I want to design its architecture using UML. Among other diagrams I'm using, I have a Sequence Diagram as the one shown in the below image. What I'm trying to depict…
m4l490n
  • 1,592
  • 2
  • 25
  • 46
9
votes
1 answer

Detect for...of Loop Support in JavaScript

Is this possible? Example: var parts = [1,2,3,4,5]; for (part of parts) { console.debug(part); } I want to detect if doing this is possible.
Gabriel Nahmias
  • 920
  • 3
  • 15
  • 20
9
votes
20 answers

C# Count Vowels

I am learning to program C# and I am trying to count the vowels. I am getting the program to loop through the sentence, but instead of returning vowel count, it is just returning the length of the string. Any help would be greatly appreciated. …
Phorden
  • 994
  • 4
  • 19
  • 43
9
votes
6 answers

continue ALLWAYS Illegal in switch in JS but break works fine

switch ("B") { case "A": break; case "B": continue; case "C": break; default: break; } simple correct code in C++, but when made in javascript in stable chrome it just throws an error "Illegal continue statement", looks like…
Owyn
  • 663
  • 2
  • 8
  • 17
9
votes
3 answers

Read list of files on unix and run command

I am pretty new at shell scripting and I have been struggling all day to figure out how to perform a "for" command. Essentially, what I am trying to do is the following: I have a list.txt file with a bunch of names: name1 name2 name3 for every name…
user2647734
  • 127
  • 1
  • 1
  • 5
9
votes
8 answers

Get indices of n maximums in java array

I have an array of size 1000. How can I find the indices (indexes) of the five maximum elements? An example with setup code and my attempt are displayed below: Random rand = new Random(); int[] myArray = new int[1000]; int[] maxIndices = new…
Brian
  • 7,098
  • 15
  • 56
  • 73
9
votes
3 answers

Grouping Selectors inside of a loop using Sass

The Issue I'm currently in a pickle. I need to group selectors inside of a Sass loop. I've tried many different ways to go about doing this such as: body { $store: null; @for $i from 1 through 10 { $store: append($store,…
djthoms
  • 3,026
  • 2
  • 31
  • 56
9
votes
7 answers

Trying to count words in a string

I'm trying to analyze the contents of a string. If it has a punctuation mixed in the word I want to replace them with spaces. For example, If Johnny.Appleseed!is:a*good&farmer is entered as an input then it should say there are 6 words, but my code…
Harry Harry
  • 179
  • 2
  • 2
  • 8
1 2 3
99
100