67
votes
33answers
13k views

JavaScript - Are loops really faster in reverse…?

I've heard this quite a few times. Are JavaScript loops really faster when counting backward? If so, why? I've seen a few test suite examples showing that reversed loops are quicker, but I can't find ...
26
votes
16answers
2k views

Best Loop Idiom for special casing the last element

I run into this case a lot of times when doing simple text processing and print statements where I am looping over a collection and I want to special case the last element (for example every normal ...
17
votes
13answers
2k views

Does the last element in a loop deserve a separate treatment?

When reviewing, I sometimes encounter this kind of loop: i = begin while ( i != end ) { // ... do stuff if ( i == end-1 (the one-but-last element) ) { ... do other stuff } ...
17
votes
8answers
2k views

Does Java recognize infinite loops?

Given the following code sample: public class WeirdStuff { public static int doSomething() { while(true); } public static void main(String[] args) { doSomething(); } } This is a valid ...
15
votes
1answer
305 views

Performance of different C# loops

There are a number of different way to accomplish the same simple loop though the items of an object in c#. This has made me wonder if there is any reason be it performance or ease of use, as to ...
14
votes
31answers
4k views

Test loops at the top or bottom? (while vs. do while) [closed]

When I was taking CS in college (mid 80's), one of the ideas that was constantly repeated was to always write loops which test at the top (while...) rather than at the bottom (do ... while) of the ...
11
votes
8answers
2k views

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto (in C)?

When implementing an infinite loop, is there a difference in using while(1) vs for(;;) vs goto? Thanks, Chenz
10
votes
10answers
13k views

Continue in nested while loops

In this code sample, is there any way to continue on the outer loop from the catch block? while { // outer loop while { // inner loop try { throw; } ...
10
votes
4answers
625 views

IntelliJ suggests replacing the while loop with a for each loop. Why?

ArrayList<Object> list = new ArrayList<Object>(); list.add(12); list.add("Hello"); list.add(true); list.add('c'); Iterator iterator = list.iterator(); ...
10
votes
2answers
61k views

Do while loop in SQL Server 2008

Is there any methode for implement do while loop in SQL server 2008?
10
votes
3answers
12k views

while(list($key, $value) = each($array)) vs. foreach($array as $key => $value)?

Recently I experienced this weird problem: while(list($key, $value) = each($array)) was not listing all array values, where replacing it with... foreach($array as $key => $value) ...worked ...
9
votes
5answers
8k views

Clojure While Loop

I trying clojure i am trying to figure out how to implement the following algorithm, I am reading from an input stream i want to continue reading until it is not a delimiter character. i can do ...
8
votes
4answers
26k views

How do you Make A Repeat-Until Loop in C++?

How do you Make A Repeat-Until Loop in C++? As opposed to a standard While or For loop. I need to check the condition at the end of each iteration, rather than at the beginning.
8
votes
2answers
316 views

Model division of cancer cells on a grid

I have a 5000x5000 grid, and I'm trying to implement a simple model of cancer division in MATLAB. Initially, it picks a random point (x,y) and makes that cell a cancer cell. On the first iteration, it ...
7
votes
8answers
440 views

Create List With Numbers Getting Greater Each Time Python [closed]

How can I make a function that will create a list, increasing the amount of numbers it contains each time to a specified value? For example if the max was 4, the list would contain 1, 2, 2, 3, 3, 3, ...

1 2 3 4 5 54
15 30 50 per page