Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [loops]

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

-1
votes
4answers
266 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
85 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
110 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
3k 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
78 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) { // ...
7
votes
7answers
1k 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
12k 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
223 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. ...
4
votes
2answers
126 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 ...
-4
votes
2answers
173 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
481 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
797 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
458 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 ...
5
votes
7answers
2k 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 ...
-1
votes
2answers
228 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 ...
8
votes
2answers
6k 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() { ...
8
votes
1answer
253 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 ...
7
votes
1answer
1k views

What is the opposite of a “priming read”?

This is a simple terminology question about a primitive loop design pattern. Some looping constructs require a priming read before entering the loop... Whereas other looping constructs require one ...
2
votes
2answers
940 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
103 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
3k 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+...
4
votes
3answers
301 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
182 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 ...
4
votes
2answers
1k 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
140 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
13k 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 ...
2
votes
3answers
1k views

Does IEnumerable require to use foreach in 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 ...
0
votes
0answers
104 views

Converting a 1D character array to a 2D array in random order.

I define my character array (9 items) as the following: char arr[] = {'a', 'c', 'b', 'z', 'k', 'l', 'j', 'o', 'd'} From this array, I would like to create a 3x3 array, in which the characters are in ...
4
votes
2answers
188 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
220 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
2k 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 ...
59
votes
17answers
18k 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
810 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
109 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 ...
5
votes
2answers
6k 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
846 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
4k 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
124 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
231 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 ...
1
vote
2answers
134 views

Load to list in one loop and then process list in another or do it all at once

I have a dataset full of rows that I must initialize into myclass and then process. I am currently looping through each row in the dataset, initializing a new instance of myclass, then adding that ...
-3
votes
3answers
920 views

Best way to handle variables used in a for loop? [duplicate]

From previous experience, I had always thought that, if you are going to use variables inside of a for loop, it was much better to declare them outside of the loop vs. inside the loop itself. I ...
22
votes
8answers
4k views

At what point is it taboo to have loops within loops?

Just curious. The most I have ever had was a for loop within a for loop, because after reading this from Linus Torvalds: Tabs are 8 characters, and thus indentations are also 8 characters. There ...
22
votes
2answers
13k views

Filtering foreach loops with a where condition vs continue guard clauses

I've seen some programmers use this: foreach (var item in items) { if (item.Field != null) continue; if (item.State != ItemStates.Deleted) continue; // code } instead ...
122
votes
11answers
20k views

Is there anything that can be done with recursion that can't be done with loops?

There are times where using recursion is better than using a loop, and times where using a loop is better than using recursion. Choosing the "right" one can save resources and/or result in fewer lines ...
-1
votes
1answer
184 views

eradicating the array = loop mindset [closed]

I have noticed a common issue in code reviews, that takes this form: // "arr" is an array for (i = 0; i < arr.length; ++i) { if (i == 3) { // do something with arr[i] } if (i ==...
6
votes
2answers
2k views

Is using for loop syntax for a “with(variable)” block an anti-pattern?

I fooled around with for-loops, remembered the with keyword from delphi and came up with following pattern (defined as a live template in IntelliJ IDEA): for ($TYPE$ $VAR$ = $VALUE$; $VAR$ != null; $...
2
votes
2answers
141 views

Did labelled loops pre-date for loops?

Was the first implementation of looping control flow effectively the goto (or something like a labelled JMP in assembler) or was there another syntactic construction? for (für) was introduced in ...