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

48
votes
6answers
9k views

Who created the idea(s) of the first loop constructs?

while (1) { if (1+1==2) { print "Yes, you paid attention in Preschool!"; } else { print "Wait... I thought 1+1=2"; } } As a developer, we all have to use ...
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; ...
16
votes
4answers
7k 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 ...
10
votes
7answers
3k views

C# foreach improvements?

I run into this often during programming where I want to have a loop count index inside of a foreach and have to create an integer, use it, increment, etc. Wouldn't it be a good idea if there was a ...
9
votes
2answers
1k 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 ...
7
votes
5answers
336 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 = ...
7
votes
3answers
3k views

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

Possible Duplicate: Where do you declare variables? The top of a method or when you need them? Does it make a difference if I declare variables inside or outside a loop in Java? Is this ...
7
votes
1answer
478 views

Loop fusion example using aspect-oriented framework

I had recently read a paper 'Aspect-Oriented Programming' by Gregor Kiczales and others, and found there the loop fusion example. Here is a definition of the loop fusion from the paper …the loop ...
6
votes
4answers
614 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 ...
6
votes
4answers
540 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: // ...
4
votes
4answers
929 views

Decrementing/Incrementing loop variable inside for loop. Is this code smell?

I have to read lines from a text file in sequential order. The file is a custom text format that contains sections. If some sections are out of order, I would like to look for the starting of the next ...
4
votes
1answer
1k 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 ...
4
votes
3answers
250 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 ...
4
votes
1answer
336 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 ...
3
votes
5answers
554 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 ...
3
votes
3answers
532 views

Is there a way to add unique items to an array without doing a ton of comparisons?

Please bare with me, I want this to be as language agnostic as possible becuase of the languages I am working with (One of which is a language called PowerOn). However, most languanges support for ...
3
votes
5answers
1k 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 ...
3
votes
1answer
440 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 ...
3
votes
4answers
220 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
4answers
1k 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 ...
2
votes
2answers
165 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
1answer
187 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 ...
2
votes
2answers
141 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 ...
2
votes
2answers
434 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 ...
1
vote
3answers
229 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.
1
vote
3answers
288 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
8answers
780 views

Why does the instruction “do” require a “while”?

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 ...
1
vote
1answer
123 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 ...
0
votes
4answers
13k views

2 Dimensional Arrays in C++

I started learning arrays in C++ and came over a little side note in the book talking about 2D arrays in breif. I tested it out and i was amazed that it could give the programmer the ability to store ...
0
votes
2answers
213 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++){ ...
0
votes
4answers
125 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 ...
0
votes
1answer
270 views

Break from loop, in a loop, a loop? [on hold]

There is a command in many programming languages to exit loop, break. The problem is break doesn't mean to break or damage something, but mean exit a loop. So, how to say, break from a loop (as ...
0
votes
2answers
278 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
2answers
124 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 < ...
0
votes
2answers
176 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
1answer
80 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
1answer
143 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 { ... ...
0
votes
0answers
4 views

What happens when there is multiple expressions in the condition part of a for loop seperated by commas? [migrated]

I have an infinite loop here, but why? int end = 5; for(int i = 0; i < end, printf("at condition i=%d\n",i); ++i) { printf("inside i=%d\n",i); }
0
votes
0answers
31 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 ...
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 ...
-1
votes
4answers
1k views

How to improve my loop logic in programming?

I know how to do simple loops but I don't know what's going on when many loops are working together. For example: for (i=0; i <= 9; i++){ document.write(linebreak); for (m=0; m <= 9; ...
-2
votes
4answers
279 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 ...