Questions tagged [loops]

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

-2
votes
1answer
130 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
4answers
209 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
0answers
33 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
1answer
124 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
2answers
244 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
1answer
124 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
2answers
54 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
5answers
485 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
1answer
32 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
2answers
428 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
4answers
587 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
1answer
95 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 ...
2
votes
2answers
120 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&...
17
votes
6answers
4k 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 ...
0
votes
3answers
88 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) { // ...
8
votes
7answers
3k 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 ...
48
votes
16answers
13k 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(...
-1
votes
2answers
301 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. ...
3
votes
2answers
213 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 ...
-5
votes
2answers
182 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 ...
1
vote
2answers
809 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 ...
2
votes
1answer
1k 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 ...
1
vote
1answer
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 ...
2
votes
6answers
557 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 ...
6
votes
7answers
5k 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 ...
0
votes
2answers
248 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 ...
9
votes
2answers
10k 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() { ...
7
votes
1answer
255 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 ...
2
votes
2answers
1k 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,...
0
votes
1answer
108 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 ...
9
votes
9answers
4k 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+...
3
votes
3answers
341 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 ...
2
votes
2answers
309 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 ...
6
votes
2answers
2k 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()) {...
-1
votes
3answers
170 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;
1
vote
2answers
124 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 (...
37
votes
11answers
15k 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 ...
3
votes
3answers
2k views

Is implementing IEnumerable required to use foreach on collections?

I have the following class that doesn't implement IEnumerable but is working perfectly with foreach. And also, arrays are working without implementing IEnumerable. So why does it keep saying that ...
4
votes
2answers
203 views

Common loop variable names for indexes in 4D and above

I am curious if there are common loop index variables (of single character or not) for dealing with 4 dimensions and above? I was helping another student working through CS50x who was just learning ...
-2
votes
1answer
231 views

For loop and recursion for a new shell in C [closed]

I code a new shell in C, that could be done in several ways: Flex/bison, finite state machine, abstract syntax tree or just a tokenizer in C. Now I've written a for-loop that changes the condition of ...
15
votes
1answer
3k views

What is priming the pump? Sometimes called a priming read

I was taught this expression and pattern way back in the day. Sure, the name comes from old pumps that needed to be filled with water before they could pump water, but who cares? We're talking about ...
64
votes
17answers
20k views

How to write correct loops?

Most of time while writing loops I usually write wrong boundary conditions(eg: wrong outcome) or my assumptions about loop terminations are wrong(eg: infinitely running loop). Although I got my ...
-3
votes
1answer
1k views

Using System.out.println() with loops and arrays [closed]

Is there anyway I can create a loop for System.out.println("...") and reading in the values? For example I wish to do this: System.out.println("Input the Executive's name: "); String eName = ...
1
vote
1answer
143 views

Efficient way to skip ticks/frames in a loop?

So: main does a loop, and every iteration it increments a tick by one For example, say it were in python because that's easy to write: def main(): tick = 0 while True: tick += 1 ...
6
votes
2answers
7k views

Communication between microservices - distinguishing internal calls safely

I am rearchitecturing and rewriting my monolithic BaaS solution into microservices regarding to scalability and single responsibility rules. Due to the internal dependencies, services are placed on ...
-10
votes
1answer
1k views

Java: Why's this For-Loop thousands of times slower?

So I have a simple method I'm calling millions of times a second, and I noticed that it was taking 16% of all processing time!! The offensive routine is as follows: public void placeHolder(){ ...
15
votes
7answers
2k views

Should <= and >= be avoided when using integers, such as in a For loop? [closed]

I have explained to my students that equal-to testing is not reliable for float variables, but is fine for integers. The textbook I am using said that it is easier to read > and < than >= and <=....
0
votes
4answers
5k views

Nested For Loops JavaScript

I have an app which gets data from the database (MongoDB) in JSON format. The returned data contains nested arrays and I want to access the values in the nested arrays. The returned JSON format looks ...
2
votes
1answer
126 views

printing in methods vs printing in the main/driver class

I'm fairly new to programming and am currently creating a blackjack game in Java. I've progressed quite well, although could use some pointers with regards to the best way to implement a method. At ...
0
votes
2answers
233 views

Do some built-in functions loop behind the scenes?

I mostly code in C# & VB, but I think this question is pretty universal. I try to limit loops to increase performance. For instance, string functions that split the string into an array, or do a ...