Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Questions tagged [loops]

A **loop** is a sequence of statements which is specified once but which may be carried out several times in succession.

Filter by
Sorted by
Tagged with
1 vote
1 answer
216 views

Suppose I need to find the last selected index of options according to last selected contents, eg: options : A,B,C,D,E ,last selected option: C, so the last selected index is 2. There is a special ...
wcminipgasker2023's user avatar
3 votes
4 answers
479 views

I have a function (written in Python) that processes a loosely structured log file. The log file would have begin-end markers for different sections, with each section describing different things (e.g....
Happy Green Kid Naps's user avatar
-3 votes
1 answer
91 views

I'm currently in the process of reworking a monolithic function. My first step is finding blocks of code that can be extracted into their own "sub"-methods for ease of readability & ...
Incubbus's user avatar
2 votes
2 answers
162 views

I would like to explain the following code to my colleagues: for options in [{Option.NO_CUTOFFS}, {}, {Option.HEURISTIC}]: ... The following sentence is kind of awkward: This loop loops on the ...
AlwaysLearning's user avatar
-1 votes
2 answers
5k views

When processing data sets in batches I usually can think of the following three implementations. Which one do you consider better than the other and why? Notes: The implementation is in C# but the ...
Botond Botos's user avatar
1 vote
2 answers
258 views

I have a Node.js micro-service architecture-based back-end. Services communicate with each other via PubSub. E.g. upon a request to service A, service A messages either service B or service C via ...
cis's user avatar
  • 255
4 votes
2 answers
160 views

Often I need to get some data and process it in some way. For example getting a list of customers from an API and assemble some summary data on them. As an example, getting : api_result = api.request(...
User's user avatar
  • 1,581
0 votes
1 answer
99 views

Consider the following pseudo-code: cont = 0 for g = 1,...,m for h = g,...,m cont = cont + 1 end for end for I'm searching for the explicit map that returns cont in ...
Gost91's user avatar
  • 111
-5 votes
1 answer
1k views

If I have code that looks like this: int i; void functionA (){ for (i=0; i<10; i++){ functionB(); } } void functionB (){ for (i=0; i<20; i++){ doSomething(); } } ...
Noah Smith's user avatar
1 vote
2 answers
1k views

I'm confused about a matter that I've been unable to figure out. I'm doing some leetcode problems. In backtracking problems, sometimes we use loop within our recursive method to call the recursion but ...
Umer Farooq's user avatar
24 votes
9 answers
9k views

If I have a function that never returns (it contains an infinite loop) how do I declare/communicate that it will never return to the user of the function? The user does not see the source code, only ...
Fred's user avatar
  • 509
0 votes
5 answers
601 views

Let me explain what I mean. Imagine A subs to event b. In such case A pubs event a. B subs to event a. In this case B subs b. This is a full-blown circle. How does a pub/sub cycle deal with such ...
Aurlito's user avatar
  • 27
3 votes
1 answer
432 views

Recently I was asked to refactor some code that leverages JavaScript's array.reduce() method because other developers felt the code hard to read. While doing this I decided to play around with some ...
Kenneth Moore's user avatar
-4 votes
4 answers
775 views

I don't think any of them are good practice. In addition to that they make the code longer. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for Optional initialization ...
thadeuszlay's user avatar
-4 votes
1 answer
166 views

I am modifying a software which is for trash collection. While reading the code, I asked myself is there any formula or trick to quickly calculate the number of time the statement gets executed inside ...
AKdeBerg's user avatar
4 votes
2 answers
553 views

I often find a situation where I need to write duplicate codes in a for loop, where the "init step" to identical to the "update step": // duplicate `next()` for (let x = next(); p(x); x = next()) { ...
Chiawen's user avatar
  • 266
-2 votes
1 answer
326 views

My question is, how would I go about making a function that serves the same purpose of the for loop, without using any loop method. function fl (initial, condition, iterator, code){ i = initial; ...
skistaddy's user avatar
  • 254
2 votes
4 answers
2k views

Here is the known old way to iterate over the string: for (int i = 0; i < str.length(); i++) { char c = str[i]; } However recently I have also seen in multiple places the usage of ...
h23's user avatar
  • 131
1 vote
0 answers
43 views

I wasn't sure exactly how to word this question, but basically, I have a struct stNeuralLayers (for a neural network I'm playing around with) with fields that are matrices (such as a double[,] ...
Tara's user avatar
  • 151
1 vote
1 answer
241 views

I am working on creating iterators, for strings, lists, trees, graphs, and potentially other things. First a side note. I have a string data type in my engine. The string is implemented as a bunch of ...
Lance Pollard's user avatar
-2 votes
2 answers
343 views

For example, sometimes I need a float variable to be used inside a for loop, e.g.: float sum=0; for(int i = 0; i < students.length; i++) { sum += Math.random(); students[i].result = sum; } ...
ocomfd's user avatar
  • 5,760
-1 votes
1 answer
1k views

I have came across a concept in recursion which says that when loops are compiled or interpreted then they get converted to recursive functions. If it is true how it takes place ?
Ashwani Sharma's user avatar
0 votes
2 answers
88 views

My question is about the construction of main loops running in the background listening to commands and signals and how are they constructed to be efficient. For instance in live music synthesis ...
user avatar
4 votes
5 answers
788 views

For example, in my project, I often found some head of for-loop appears many times, eg: for(let i=0;i<SharedData.students.length;i++){ SharedData.students[i].something=..... } if(isReset){ ...
ocomfd's user avatar
  • 5,760
-1 votes
1 answer
483 views

How would I go about prompting user-input, while at the same time running timers, to periodically execute automatic functions. Pseudo-code to illustrate: while true { if input() OR timer(10) ...
Ciarán J. Hagen's user avatar
2 votes
2 answers
4k views

I was going through the traditional quick sort algorithm. I had a look on the partition algorithm in a couple of places and the implementation difference was very subtle. Here are the 2 approaches: ...
arnie7's user avatar
  • 33
-1 votes
4 answers
2k views

I am part of a software team that is writing a console application (no UI) in C#. My part in the team is to write code to call RESTful APIs from a third party site, process the returned data and save ...
Xami Yen's user avatar
  • 379
2 votes
2 answers
202 views

For example, suppose I have 2 arrays: let arr1=[5,2,1]; let arr2=["abcde","ab","a"]; my work is simple : to check if length of strings in arr2 are larger than corresponding element with same index in ...
ocomfd's user avatar
  • 5,760
3 votes
2 answers
141 views

For example, I have a for loop, which element 0 has additional function to run compared with other elements, my question is, should the additional function be: 1.place inside for loop for(int i=0;i&...
ocomfd's user avatar
  • 5,760
17 votes
6 answers
9k views

This is the most popular way (it seems to me) of checking if a value is in an array: for (int x : array) { if (x == value) return true; } return false; However, in a book I’ve read ...
Danila Piatov's user avatar
0 votes
3 answers
607 views

Sometimes I find it useful to have loops that do an action at the beginning and/or the end of a while loop. The best way I can think of for representing this in C++ would be: if(condition) { // ...
Graham's user avatar
  • 105
13 votes
7 answers
13k views

Here is a simplified sample. Basically, it does checks on a string from a string list. If the check passes, it will remove that string (filterStringOut(i);), and it is no longer ...
Anon's user avatar
  • 3,649
48 votes
16 answers
17k views

Sometimes I need for loops which needs a break like this: for(int i=0;i<array.length;i++){ //some other code if(condition){ break; } } I feel uncomfortable with writing if(...
ocomfd's user avatar
  • 5,760
-1 votes
2 answers
805 views

The big image is catalog or ad blocks that comes from catalog table. And small images are the actual products block that comes from product table. There may be more products and more catalog. attached ...
monster's user avatar
3 votes
2 answers
561 views

I'm trying to understand how this method works and everywhere I check it I find there's something faulty or maybe it's that I'm not understanding something from the method. Here there's an ...
user2638180's user avatar
-5 votes
2 answers
237 views

Constrained by a main loop outside of my control (eg: UI event loop, game loop, etc...), I have a slow (relative to the main loop's expectations) algorithm involving a loop. Thus, I have to ...
Peter K's user avatar
1 vote
2 answers
2k views

Let's consider I have an std::string instance filled with textual data and an std::set<std::string> instance with keywords. I would like to know whether the text stored inside the std::string ...
Akira's user avatar
  • 257
2 votes
1 answer
6k views

Does i in for loops means iteration or index? How could I know the original meaning? Some programmers say it's iteration, some say it's index. It seems to me more of an index we start from --- we go ...
Arcticooling's user avatar
1 vote
1 answer
3k views

In C# you can use a for-loop without declaring the body, what is the benefit of using this in production-code versus using a foreach loop? Example My Examples below use reflection to get the ...
Brownish Monster's user avatar
5 votes
6 answers
1k views

Is there a way to apply the Single Responsibility Principle to a function where two things need to occur in a loop in order to not need to iterate twice? For example, suppose I have a function like ...
charlieshades's user avatar
6 votes
7 answers
10k views

I often come across the following pattern. while(GetValue(i) != null) { DoSomethingWith(GetValue(i++)); } Here GetValue is executed twice. It would be much nicer to use a pattern where we can ...
Roy T.'s user avatar
  • 654
0 votes
2 answers
333 views

Let's say that I have a JSON file like at example below. How would I go about finding all possible values of item combination time sums that exist between let's say 00:03:04 to 00:25:55 without ...
user2676326's user avatar
14 votes
2 answers
18k views

After dealing with C#'s async/await pattern for a while now, I suddenly came to realization that I don't really know how to explain what happens in the following code: async void MyThread() { ...
aoven's user avatar
  • 319
7 votes
1 answer
396 views

I am running into a wall mentally when trying to think of a way to solve this problem. At my work we process customer data through some complex reasoning logic. Sometimes this logic will cause ...
Hangman4358's user avatar
2 votes
2 answers
3k views

I would consider myself an intermediate Python programmer. One of my recent challenges was creating a list of all possible solutions to a given Countdown problem. Without getting into too much detail,...
IliaK's user avatar
  • 23
0 votes
1 answer
152 views

There is a XML link, which provides the children of any parentID given. http://www.browsenodes.com/xml.php?action=BrowseNodeInfo&node=1036592 Then you can run the URL again with a children ID ...
brainHax's user avatar
  • 103
9 votes
9 answers
14k views

I have a for loop where I must skip the first element in a zero-based array. Which of these shows my intentions more clearly? for($i=1 ; $i < count(array) ; $i++){ array[$i]; } or for($i=0+1 ...
TZubiri's user avatar
  • 443
3 votes
3 answers
410 views

I'm taking the Computer Architecture course in my undergraduate study. I see that in loop unrolling, one of the constraints is the number of available registers. Since the number of registers ...
user avatar
2 votes
2 answers
1k views

I'm in the process of analysis for a browser-based game I'm making, and I have question about programming the economy system. I'll use a simplified system to ask my question. Each user as GameState ...
Gil Sand's user avatar
  • 2,193
9 votes
2 answers
11k views

Just saw a code snippet that used an array pop assignment in the while condition; was wondering if this is acceptable/good practice? var arr = [0,1,2,3,4,5]; var current; while (current = arr.pop()) {...
flewk's user avatar
  • 103