Skip to main content

Questions tagged [loops]

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

6 votes
6 answers
1k views

What does the English word "for" exactly mean in "for" loops?

English is not my first language, but since the keywords in programming languages are English words, I usually find it easy to read source code as English sentences: if (x > 10) f(); => "If ...
23 votes
8 answers
5k 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 ...
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 ...
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 ...
7 votes
5 answers
18k views

Is doing an assignment inside a condition considered a code smell?

Many times I have to write a loop that requires initialization of a loop condition, and an update every time the loop executes. Here's one example: List<String> currentStrings = ...
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....
16 votes
5 answers
41k views

Does it make a difference if I declare variables inside or outside a loop in Java?

Does it make a difference if I declare variables inside or outside a loop in Java? Is this for(int i = 0; i < 1000; i++) { int temp = doSomething(); someMethod(temp); } equal to this (with ...
-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 ...
68 votes
17 answers
23k 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 ...
-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 ...
0 votes
2 answers
1k views

Loading chunks around center

I am making a voxel game like Minecraft. I am able to load chunks using multithreading with the code... for (int x = 0; x < WorldSettings.WORLD_WIDTH_IN_CHUNKS; x++) { for (int y = 0; y < ...
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(...
15 votes
7 answers
3k 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 <=....

15 30 50 per page
1
2 3 4 5
11