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

3
votes
1answer
114 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) { ...
-2
votes
0answers
58 views

Is the time complexity of this code snippet O(n)? [migrated]

struct lists * list_depth[ht]; for(int i=0; i<ht; i++) list_depth[i]=NULL; insert_to_list(&list_depth[0], root); for( int i = 1; i < ht; i++ ) { struct lists * temp = ...
1
vote
3answers
122 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
0answers
4 views

Javascript code executing before its supposed to [migrated]

I have some javascript code that is being executed before it is supposed to be and I do not know why. Basically, I have a for loop that is supposed to be executed 50,000 times. However, this takes a ...
0
votes
1answer
57 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 ...
0
votes
0answers
5 views

Loop in system call C program [migrated]

I want to call the function wget in a loop in ubuntu, how can I do it? This is what I did, but it didn't work. for (i = 1; i < 500; ++i) { system("wget -O page%i 'http://www.asdf.com/?=%i'", ...
-1
votes
0answers
36 views

Why are vectorized loops slower than unvectorized loops in Julia

I'm new to Julia and noticed that this article mentioned unvectorized code being faster than vectorized code. Why is this? I always thought vectorized loops are faster than unvectorized loops because ...
0
votes
0answers
47 views

Totalising and condensing data values from multiple arrays into a single array [migrated]

My fledgling programming skills have hit a wall. I could get this working, but the code stinks and I'm convinced there must be a more efficient way I just don't know it yet. I have a web service ...
4
votes
2answers
239 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
181 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
238 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 ...
4
votes
2answers
363 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 ...
36
votes
2answers
4k 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
96 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
110 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
132 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 ...
92
votes
8answers
14k 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
369 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
249 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
294 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
1k 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; ...
0
votes
0answers
53 views

CUDA Loop Question - Worthy of Unwinding?

It's a known fact that branching is bad for CUDA. The question I have is a little more specific. I have a (static) loop. Static here means that it follows the exact same path every time, and executes ...
1
vote
1answer
217 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 ...
0
votes
0answers
54 views

Where should I declare variables used in for-loops in Java for maximum efficiency? [duplicate]

I'm talking hypothetically but I'll use an example of what I worked on in a programming class a while ago. There's a better way to actually perform the operation, but that's not the point. An example ...
7
votes
5answers
388 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
167 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 ...
3
votes
2answers
189 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
285 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
2k 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
632 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
216 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
370 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
308 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 ...
1
vote
1answer
152 views

Randomly and uniquely iterating over a range

Say you have a range of values (or anything else) and you want to iterate over the range and stop at some indeterminate point. Because the stopping value could be anywhere in the range, iterating ...
3
votes
5answers
569 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 ...
0
votes
10answers
957 views

Why does the instruction “do” require a “while”? [closed]

Since this statement is so common: while (true) (Java) or while (1) (C) or sometimes for (;;) Why is there not a single instruction for this? I could think that an instruction that could do it ...
0
votes
2answers
349 views

Best Practice - Loop Exit Via Iterator Modification

I had an interesting discussion with my boss today regarding exiting a loop before the terminal condition is met. He had a construct in some of his VB6 code that looked something like this: Dim ...
0
votes
1answer
84 views

Code execution within delimiters

Is there any way I can execute a block of code in a loop, like so: [ some code goes here ] Where the delimiters are "[" and "]", and also allowing for nested blocks, i.e.: [the user can create ...
0
votes
2answers
144 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 < ...
15
votes
4answers
10k views

Why are nested loops considered bad practice?

My lecturer mentioned today that it was possible to "label" loops in Java so that you could refer to them when dealing with nested loops. So I went home and looked up the feature as I didn't know ...
6
votes
4answers
856 views

Is it a performance hit to create threads that loop a lot to check for things?

This is a generic question that I've always wondered. In general, is it intensive for a CPU to create threads that perform "while not true ..." loops or similar? For example, suppose I do: // ...
0
votes
4answers
128 views

Use functions inside a loop declaration

What's the best practice? This : for ($i = 0; $i < count($array); $i++) { //stuff } Or, what I usually do : $count = count($array); for($i = 0; $i < $count; $i++) { //stuff } Is it the ...
9
votes
2answers
2k views

How to unit test method that returns a collection while avoiding logic in the test

I am test-driving a method that is to generate a collection of data objects. I want to verify that the properties of the objects are being set correctly. Some of the properties will be set to the same ...
2
votes
2answers
613 views

Repetitive Drawing in Javascript & Canvas

Creating an HTML5 page using canvas and javascript to draw a set number of musical staves on the page, spaced a pre-determined amount in between. What I have is re-drawn on top of the canvas 10 ...
45
votes
7answers
4k views

Why are semicolons and commas interchanged in for loops?

In many languages (a wide list, from C to JavaScript): commas , separate arguments (e.g. func(a, b, c)), while semicolons ; separate sequential instructions (e.g. instruction1; instruction2; ...
0
votes
1answer
157 views

LOOP program only need inc and zero

I have 4 different commands in LOOP programming language: y=Zero() y=Val(x)=copy x and put it in register y y=Inc(x)=x+1 y=Dec(x)=x-1 Finally I also have loop n times { ... ...
4
votes
3answers
257 views

Is there an idiom for a loop that executes some block of instructions between iterations? (In Ruby in particular)

I often need to do some operations in a loop and some other operations between the iterations. A simple example would be collecting words from an array into a string, spelled backwards and separated ...
3
votes
5answers
2k views

Declaring functions in order to avoid explicit nested loops

My programming professor has told me that it is a good programming practice (at least in C/C++) to declare a function with the inner loop when nesting loops (not for loops, since when, i.e. looping ...
4
votes
1answer
2k views

Loop Invariants in Python

I have a bit of experience with loop invariants but I'm not really clear on them. I'm trying to learn them through an example in Python. Can someone point one out or help me understand? I've ...
3
votes
1answer
617 views

Loop invariant vs Assertions

I have an exam on Programming Concepts this Saturday and I am struggling to find some information to understand some concepts better. What is the difference between loop invariant and assertion? To ...