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

learn more… | top users | synonyms

-4
votes
1answer
40 views

New to Java, Question Concerning Loops [on hold]

I am new to Java. I was wondering how to complete a user-defined number of loops. Here's the code I have right now: package diceroller; public class DiceRoller { public static void main(String[] ...
3
votes
2answers
402 views

Using 'reduce' vs 'for' loop for returning boolean [closed]

I was wondering what people think about using a reduce function vs loop for returning true if a condition exists. Example: var a = [1, 5, 7, 4, 2, 5, 3]; var greaterThan5 = a.reduce(function(prev, ...
0
votes
0answers
68 views

Using a PHP loop or MySQL table for arrays values

I have the following results page from the previous form: <?php //Get the form results (which has been converted to an associative array) from the $_POST super global $musicgenres = ...
1
vote
2answers
77 views

Factored out loop control in Python

I have a for loop in python like this: for item in items: onwards = make_flow_decision(item) if onwards == 'break': break elif onwards == 'continue': continue elif ...
3
votes
2answers
75 views

Handling exceptions in a loop without breaking the loop (try to process all members)

Scenario: I have a loop that iterates over an Array of COM objects and does some work using them. My fear, working with COM objects, is that some exception will creep up (possibly on another ...
0
votes
2answers
331 views

Loops to create nested loops

Is there a way to use a loop to create more nested loops? E.g. Doing this for (int i = 0; i < iterations; i++) { //Do stuff for (int ii = 0; ii < ...
0
votes
1answer
87 views

Recursion, iteration, and …? [closed]

Here are three common code structures that apply a function multiple times: foo(x) { if basecase(x) return k else return foo(g(x)) } uses recursion. for i in 0..10 { n *= bar(i) } uses ...
0
votes
0answers
68 views

Does loop unrolling on a JIT platfrom (.NET or JVM) provide any benefit

I know in C, you can use loop unrolling (unwinding) to help reduce branching in your code but at the expense of program size. That seems to work when you compile to a target machine, however, does it ...
2
votes
1answer
91 views

Break big method into 2 methods, first containing a “for” loop and second a “break”

A method grew too big for its own good, and I need to break it up into two separate methods. def big_method(dct): # Initial code # ... for i in dct: # More code # ... ...
2
votes
2answers
203 views

How to iterate between these elements

I have an array of elements: int[] elem = new int[] {A, B, C}; I need to calculate the sum of ALL the combinations of those elements, where only some of the elements can be optionally selected. I ...
-2
votes
1answer
88 views

How to name variables without plural in a for-each loop? [closed]

How do you name loop variables when the list item is named after something without a plural? For instance (in python): [x for x in sheep]. x is not a great name, but sheep have/has no plural that ...
8
votes
7answers
691 views

provability of while loop vs for loop

I have this teacher, he's quite smart (sometimes, haha) he said good programmers try to use while loops instead of for loops. the reason he gave for this is because while loops can be proven, as in, ...
0
votes
1answer
105 views

Sorting Sentences by New Words in Each

A very useful learning tool I stumbled across for Chinese was a massive list of sentences that, barring the first 10 or 15, only differed by the ones before by one or two words, or at least as few as ...
1
vote
1answer
210 views

adding the digits of a number

I am trying to write a program that asks the user for a decimal number and then calculates the sum of its digits. for example if the number is 123.25 then the sum will be 1+2+3+2+5=13. I decided to ...
2
votes
3answers
218 views

The recommended Way to exit a Loop

Occasionally - but recurringly - I face the following loop pattern problem: CodeSnippet1 DO WHILE LoopCondition //LoopCondition depends on some pre-calculation from CodeSnippet1 CodeSnippet2 ...
2
votes
1answer
39 views

Comparing path strings against list of rules

I am trying to find a way to enhance a filtering algorithm. I am developing a backup software that enables users to specify custom filters to exclude unwanted files/directories. Currently when trying ...
1
vote
1answer
127 views

Most efficient way to calculate number of iterations to run

Let's say I have a batch process that executes 4 times per loop, and needs to execute a total of 9 items. Example: Iteration 1 executes 4 items of 9, leaving 5 left Iteration 2 executes 4 times of ...
2
votes
0answers
65 views

Loop Unfolding and Named Significant Bits

I've been writing a Parser Compiler for the last seven or so years, and I recently got to the point (yet again, never satisfied) of structuring the portion dealing with the portions of the language ...
-3
votes
3answers
344 views

Real life scenario of why we would use loops [closed]

I am going to school to become a programmer, and I am doing some homework and I have a question. It wants me to give an example of why we would use loops in the real world. And I was thinking about ...
2
votes
1answer
101 views

Proper way to refactor multiple if based conditions [duplicate]

I took over a large legacy code base. It has a code like this: if ($route == 'login' || $route == 'logout' || $route == 'forgot-password') { return; } if ($loggedInUser == false && ...
0
votes
4answers
232 views

How to prove that this while loop calculates n^2

I'm studying for my exam of logic later this week and I have to prove that this while loop: i := 0 s := 0 while i < n do i := i + 1 s := s + (2*i -1) calculates n^2. This question is ...
8
votes
5answers
2k views

How to structure a loop that repeats until success and handles failures

I am a self-taught programmer. I started programming about 1.5 years ago. Now I have started to have programming classes in school. We have had programming classes for 1/2 year and will have another ...
4
votes
5answers
1k views

Are there real-life usage and applications for “do while” loops? [closed]

When I see for and while loops all over production codes and mammoth projects, the last time I saw a do while loop is for a university assignment involving menu-based console input program. About 50 ...
1
vote
3answers
88 views

Using higher order functions to apply m out of M filter's and then transform data of size n?

Total no: of filters possible is M . User can select m filters where m <= M . A typical example is files from a folder , he could say modified between so and so date , start with so and so and so ...
4
votes
1answer
390 views

Functional programming for loop side effect

I am trying to get my head around as to why having a local variable or a for loop inside a function is not considered to be pure functional programming. Given this function: int as_int(char *str) { ...
1
vote
3answers
143 views

Use of for loop conditional statement unrelated to iterating variable

Is it considered decent form to write code like this: int done = 0; for (x = 0; !done; x++) { ... something involving 'x', that might end early ... if (!(x < max)) done = 1; } ...
0
votes
1answer
67 views

How can I iterate over two loops of mismatched sizes, keeping them in sync?

I have a database table in which each row is one movie (with fields like 'title', 'director' and 'writer'), and a related table where each row is one screenshot from a movie (with the fields ...
4
votes
2answers
405 views

Loop runtime question

I had an exam today and I feel that I did pretty well, except I could not for the life of me figure out what appears to be an unbelievably simple question. We were asked to give theta notation run ...
-4
votes
4answers
269 views

Why are for loops needed? [duplicate]

It seems to me that a while loop with the appropriate break statements can replace them entirely. I understand that a break statement may not feel as "smooth" as a for loop that does something a ...
1
vote
2answers
659 views

Infinite while loop CPU usage

I'm coding an script in C, which is going to check constantly an array of events, the idea is to check if the Date and time of certain event is equal to de current time and trigger something, i'm ...
5
votes
2answers
446 views

Why do some programming languages have break statements, but not higher-order break statements? [closed]

I may have been exposed to exactly the wrong languages, but though many have loops and break statements, none of the languages I am familiar with have higher-order break statements¹. While a regular ...
39
votes
2answers
5k views

How do I move away from the “for-loop” school of thought?

This is a rather conceptual question, but I was hoping I could get some good advice on this. A lot of the programming I do is with (NumPy) arrays; I often have to match items in two or more arrays ...
-1
votes
1answer
105 views

Finding “spare time” in a day from within a list of events

I have a list of events which is always sorted chronologically. The start time is always followed by the end time. Times are strings formatted as 'HHmmss'. // list of events var events = [ ...
0
votes
1answer
117 views

comparison of an unsigned variable to 0

When I execute the following loop : unsigned m; for( m = 10; m >= 0; --m ){ printf("%d\n",m); } the loop doesn't stop at m==0, it keeps executing interminably, so I thought that ...
0
votes
1answer
242 views

Python case statement? [duplicate]

I made a simple Python script that takes user input in order to generate a series of mathematical responses. In one part I did this: while True: And iterated through the loop until I used an if ...
99
votes
8answers
15k views

Why is polling accepted in web programming?

I am currently working on a Ruby on Rails project which shows a list of images. A must-have for this project is that it shows new posts in realtime without the need of refreshing the web page. After ...
1
vote
2answers
1k views

Is “continue” and “break” in loops antipattern/bad practice in Java? [duplicate]

I see the main purpose of continue in programming, it can get you out from the rest of loop steps like: while(condition1){ ... code ... if(!condition2){ continue; } ... code ... ...
3
votes
4answers
266 views

Indefinite loops where the first time is different

This isn't a serious problem or anything someone has asked me to do, just a seemingly simple thing that I came up with as a mental exercise but has stumped me and which I feel that I should know the ...
2
votes
3answers
13k views

What are the differences between a while loop and a for loop?

What are the differences between a while loop and a for loop? It seems to me that they are the same.
0
votes
2answers
9k views

foreach in list or foreach in list.where [duplicate]

I don't know what to call this question. This is my example: foreach (var item in lstItem.Where(item => stock.ItemCode == item.ItemCode)) { stock.ItemName = item.ItemName; ...
1
vote
1answer
286 views

Is there a purely SQL alternate to looping?

Historically, I probably would have written a script (php or something) to loop through all spreadsheet rows and perform some calculations. I'm looking for a pure SQL solution on the presumption ...
7
votes
5answers
473 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 = ...
2
votes
2answers
263 views

Is there a difference between declaring variables outside or inside a loop? [closed]

Is there any difference if I were to write something like this: int row,col; for(row = 0; row < data.length; row++){ for(col = 0; col < data[row].length;col++){ //do ...
4
votes
2answers
245 views

'Game loop' in a non-game application?

In real-time games, there is always a game loop that runs every few milliseconds, updates the game with new data and repaints the entire screen. Is this something that is seen in other types of ...
-2
votes
4answers
308 views

Are for loops supposed to be read inward or outward? [closed]

for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { cout << arr2d[i][j] << "\t"; } cout << endl; ..... Like that for example. Do you read ...
2
votes
4answers
3k views

Complexity in nested loops

Foreword: In this post, I will make the common confusion between O(n) and Theta(n) as complexity notations. I will write pseudo-code to talk about algorithms, using whatever notation I find to my ...
6
votes
3answers
649 views

Is doing two things on one line always a bad practice?

int *itPins = pins; for(int i = 0; i < count; i++) { ConfigureGpifPinAsGpioOutput(itPins); itPins++; } I have the impression that doing two things in one line is bad practice. This is why ...
0
votes
2answers
227 views

Is there a special name for a condition which will break a loop if it increments a set number of times

Is there a name for including a limitation in a loop structure to prevent it from running if its primary condition becomes unwieldy. For example for (var i = 0; i < len; i++){ ...
4
votes
1answer
412 views

Will loop constructs become obsolete? [closed]

By loop constructs I mean for… and while… constructs with nested statements. Currently I am coding a tool in Python and decided out of curiosity not to use the regular loops - and rely on ...
1
vote
3answers
343 views

How do you avoid looping mistakes? Mistakes that are not detected by systems [closed]

I had this crazy initialisation -- documentList = new ArrayList<Map<String,Integer>>(); which I intended to store a new map everytime in a loop but unfortunately put itself inside the ...