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 votes
2 answers
138 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 ...
user avatar
1 vote
2 answers
97 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 ...
user avatar
  • 119
4 votes
2 answers
104 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(...
user avatar
  • 1,501
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 ...
user avatar
  • 111
-5 votes
1 answer
387 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(); } } ...
user avatar
1 vote
1 answer
311 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 ...
user avatar
21 votes
9 answers
7k 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 ...
user avatar
  • 439
0 votes
5 answers
249 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 ...
user avatar
  • 27
3 votes
1 answer
179 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 ...
user avatar
-4 votes
4 answers
290 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 ...
user avatar
-4 votes
1 answer
97 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 ...
user avatar
-6 votes
1 answer
194 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: ...
user avatar
4 votes
2 answers
398 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()) { ...
user avatar
  • 246
-2 votes
1 answer
225 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; ...
user avatar
1 vote
4 answers
394 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 ...
user avatar
  • 121
1 vote
0 answers
40 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[,] ...
user avatar
  • 151
1 vote
1 answer
175 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 ...
user avatar
  • 2,412
-1 votes
2 answers
261 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; } ...
user avatar
  • 5,572
-1 votes
1 answer
680 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 ?
user avatar
0 votes
2 answers
69 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 ...
user avatar
4 votes
5 answers
605 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){ ...
user avatar
  • 5,572
-1 votes
1 answer
238 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) ...
user avatar
2 votes
2 answers
3k 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: ...
user avatar
-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 ...
user avatar
  • 349
2 votes
2 answers
159 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 ...
user avatar
  • 5,572
3 votes
2 answers
124 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&...
user avatar
  • 5,572
17 votes
6 answers
7k views

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

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 ...
user avatar
0 votes
3 answers
116 views

Start and exit actions in while loops

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) { // ...
user avatar
  • 105
10 votes
7 answers
9k views

Best practice to "continue" from inside a nested loop?

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 ...
user avatar
  • 3,319
48 votes
16 answers
15k views

Inside a for-loop, should I move the break condition into the condition field if possible? [closed]

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(...
user avatar
  • 5,572
-1 votes
2 answers
585 views

How do i create a logic for dynamic product list page like below using php and bootstrap?

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. ...
user avatar
3 votes
2 answers
462 views

While and do-while loop in the white box method of loop testing

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 ...
user avatar
-5 votes
2 answers
214 views

Term for refactoring looping logic for an event loop

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 ...
user avatar
1 vote
2 answers
1k views

How to return boolean result from comparison loops to maintain better readability?

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 ...
user avatar
  • 231
2 votes
1 answer
4k views

Does i in for loops means iteration or index? [duplicate]

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 ...
user avatar
1 vote
1 answer
2k views

What is the benefits of a foreach-loop vs a body-less for-loop

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 ...
user avatar
5 votes
6 answers
878 views

How to apply SRP in a loop where two things need to be done?

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 ...
user avatar
6 votes
7 answers
9k views

While loop without evaluating data twice

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 ...
user avatar
  • 644
0 votes
2 answers
304 views

Permutations for time in JSON

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 ...
user avatar
13 votes
2 answers
16k views

What exactly happens when a thread awaits a task inside a while loop?

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() { ...
user avatar
  • 309
7 votes
1 answer
300 views

Automatic Realtime Cycle Detection of function calls

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 ...
user avatar
2 votes
2 answers
3k views

Efficiency considerations: nested loop vs recursion

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,...
user avatar
  • 23
0 votes
1 answer
133 views

how to reach all nodes in a Tree Structure where end of the tree is unknown

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 ...
user avatar
  • 103
9 votes
9 answers
11k views

Clear way to skip the first element in an index based for loop

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+...
user avatar
  • 435
3 votes
3 answers
374 views

Is loop unrolling one of the examples of "targeted" compilation and faster instruction set?

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
982 views

How to update a player's money count every second

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 ...
user avatar
  • 2,113
8 votes
2 answers
8k views

Is it good practice to use array.pop() assignment in a while loop condition?

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()) {...
user avatar
  • 93
-1 votes
3 answers
443 views

Time complexity of an algorithm [closed]

What is the complexity of the following loop? for(i=1;i<n;i=2^i) sum+=i;
user avatar
1 vote
2 answers
132 views

Should I be Using a Thread?

I made a console application which makes the mouse click in the middle of the screen every two minutes. It has a while True loop in it which is meant to keep going for relatively long periods of time (...
user avatar
42 votes
11 answers
26k views

Is a while loop intrinsically a recursion?

I wondered whether a while loop is intrinsically a recursion? I think it is because a while loop can be seen as a function that calls itself at the end. If it is not recursion, then what is the ...
user avatar
  • 545