Questions tagged [loops]
A **loop** is a sequence of statements which is specified once but which may be carried out several times in succession.
154
questions
1
vote
1
answer
192
views
Should I separate special cases from for loop deliberately, or let a for loop handles special cases naturally?
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 ...
3
votes
4
answers
359
views
Reducing cyclomatic complexity of a state machine
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....
-3
votes
1
answer
85
views
Declarative name for function with multi-purpose-loop from broken-up monolithic function [closed]
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 & ...
2
votes
2
answers
140
views
Using the word "loop" as a subject in a sentence [closed]
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 ...
-1
votes
2
answers
4k
views
Loop pattern for batch data processing
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 ...
1
vote
2
answers
205
views
Make infinite loop handleable via delay?
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 ...
4
votes
2
answers
135
views
Separation of data retrieval and processing in loops?
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(...
0
votes
1
answer
85
views
Explicit expression of a counter
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 ...
-5
votes
1
answer
1k
views
How do I manage multiple nested for-loops without using multiple variables?
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();
}
}
...
1
vote
2
answers
1k
views
Calling recursive method in a loop - Backtracking
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 ...
22
votes
9
answers
8k
views
Declaring that a function never returns
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 ...
0
votes
5
answers
498
views
How to deail with a hypothetical situation in which a pub/sub cycle gets into an unending recursive loop?
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 ...
3
votes
1
answer
365
views
What is the expected performance of While loops using `array.pop()` assignment vs other methods
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 ...
-4
votes
4
answers
637
views
What are use cases of using optional for loop statements?
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
...
-4
votes
1
answer
139
views
Is there any formula or trick to count how many times a statement inside a nested loop gets executed?
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 ...
-6
votes
1
answer
383
views
While loop definition in python
My question is about a use of while loops that seems very abstract to me. I understand a while loop like the first one (one which has a clearly defined statement):
num = 1
while num<1:
...
4
votes
2
answers
491
views
How to avoid duplication in a for loop when "initialization step" is identical to "update step"?
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()) {
...
-2
votes
1
answer
303
views
Is there a way to recreate the code behind the for loop?
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;
...
1
vote
4
answers
1k
views
Should I always use iterators when working with strings?
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 ...
1
vote
0
answers
42
views
is it better to have tracking fields that are maintained separately for arrays? [duplicate]
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[,] ...
1
vote
1
answer
220
views
How much an iterator should do
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 ...
-1
votes
2
answers
299
views
Is there any consequences to write for(float i=0, myFloatVar=0; i < n; i++)?
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;
}
...
-1
votes
1
answer
1k
views
What happens when loops are compiled or interpreted?
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 ?
0
votes
2
answers
74
views
Main Loops and listeners in live coding systems
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 ...
4
votes
5
answers
734
views
Is copy and paste the head of for-loop (e.g.:for(let i=0;i<something.length;i++)) violating DRY principle?
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){
...
-1
votes
1
answer
408
views
How to read user input and at the same time execute periodic commands
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)
...
2
votes
2
answers
4k
views
Can nested loop have linear time complexity
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:
...
-1
votes
4
answers
2k
views
Alternatives to Long Loops [closed]
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 ...
2
votes
2
answers
175
views
Should special case be inside or outside the for loop here?
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 ...
3
votes
2
answers
125
views
Should I move tasks which is just for a specific element only out of for loop?
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&...