tagged [loops]

More Pythonic Way to Run a Process X Times

More Pythonic Way to Run a Process X Times Which is more pythonic? ``` count = 0 while count

27 Nov at 15:24

What is the difference between range and xrange functions in Python 2.X?

What is the difference between range and xrange functions in Python 2.X? Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or...

26 Nov at 09:17

How do I skip an iteration of a `foreach` loop?

How do I skip an iteration of a `foreach` loop? In Perl I can skip a foreach (or any loop) iteration with a `next;` command. Is there a way to skip over an iteration and jump to the next loop in C#? `...

10 Apr at 09:51

do { ... } while (0) — what is it good for?

do { ... } while (0) — what is it good for? I've been seeing that expression for over 10 years now. I've been trying to think what it's good for. Since I see it mostly in #defines, I assume it's good ...

3 Jun at 23:4

C# enums as function parameters?

C# enums as function parameters? Can you pass a standard c# enum as a parameter? For example: By doing this I hope to retrieve all the names within any given enum. What would the Iteration code look l...

29 Jan at 15:40

c++ exit loop based on keyboard input

c++ exit loop based on keyboard input Is it possible to exit a C++ loop based on keyboard input without actually having to input something each iteration? For instance I feel that this is very easy, b...

30 Mar at 04:24

Performance: List.Count vs checking a stored variable

Performance: List.Count vs checking a stored variable I wonder if this makes any difference: ``` for (int i = 0; i

18 Jun at 13:10

Is there a way to loop through a table variable in TSQL without using a cursor?

Is there a way to loop through a table variable in TSQL without using a cursor? Let's say I have the following simple table variable: Is declaring and using a cursor my only option if I wanted to iter...

15 Sep at 07:18

int object is not iterable while trying to sum the digits of a number?

int object is not iterable while trying to sum the digits of a number? I have this code: but it throws an error: `'int' object is not iterable` I wanted to find out the total by adding each digit, for...

24 Oct at 07:55

While, Do While, For loops in Assembly Language (emu8086)

While, Do While, For loops in Assembly Language (emu8086) I want to convert simple loops in high-level languages into assembly language (for emu8086) say, I have this code: ``` for(int x = 0; x

How to enumerate an enum?

How to enumerate an enum? How can you enumerate an `enum` in C#? E.g. the following code does not compile: And it gives the following compile-time error: > 'Suit' is a 'type' but is used like a 'var

24 Nov at 00:35

How to loop through a plain JavaScript object with the objects as members

How to loop through a plain JavaScript object with the objects as members How can I loop through all members in a JavaScript object, including values that are objects? For example, how could I loop th...

19 Jul at 11:36

C# - How to create a non-detectable infinite loop?

C# - How to create a non-detectable infinite loop? This is just an "I am Curious" question. In C#-in-depth Jon Skeet says about lambda expressions: (Page 233) The footnote then says: (Page 233) I am w...

22 Feb at 19:5

Iterate Multi-Dimensional Array with Nested Foreach Statement

Iterate Multi-Dimensional Array with Nested Foreach Statement I think this might be a pretty simple question, but I haven't been able to figure it out yet. If I've got a 2-dimensional array like so: W...

12 Sep at 12:1

How to create an infinite loop in Windows batch file?

How to create an infinite loop in Windows batch file? This is basically what I want in a batch file. I want to be able to re-run "Do Stuff" whenever I press any key to go past the "Pause". Looks like...

Is there a better way to run a command N times in bash?

Is there a better way to run a command N times in bash? I occasionally run a bash command line like this: To run `some_command` a number of times in a row -- 10 times in this case. Often `some_command...

30 Aug at 01:17

Breaking/exit nested for in vb.net

Breaking/exit nested for in vb.net How do I get out of nested for or loop in vb.net? I tried using exit for but it jumped or breaked only one for loop only. How can I make it for the following:

25 Feb at 11:3

c# parallel foreach loop finding index

c# parallel foreach loop finding index I am trying to read all lines in a text file and planning to display each line info. How can I find the index for each item inside loop? ``` string[] lines = Fil...

21 Feb at 16:33

LINQ: How to skip one then take the rest of a sequence

LINQ: How to skip one then take the rest of a sequence i would like to iterate over the items of a `List`, except the first, preserving the order. Is there an elegant way to do it with LINQ using a st...

12 Mar at 09:47

C# getting all colors from Color

C# getting all colors from Color I want to make a `ComboBox` filled with all the colors from `System.Drawing.Color` But I can't seem to collect all the colors from that collection I've already tried u...

29 Sep at 11:30

For loop to calculate factorials

For loop to calculate factorials Currently I have this set of code and its meant to calculate factorials. ``` int numberInt = int.Parse(factorialNumberTextBox.Text); for (int i = 1; i

16 May at 10:2

How to efficiently remove all null elements from a ArrayList or String Array?

How to efficiently remove all null elements from a ArrayList or String Array? I try with a loop like that But it isn't nice. Can anyone suggest me a better solution? --- Some useful benchmarks to make...

asp.net mvc razor foreach loop adding id to div

asp.net mvc razor foreach loop adding id to div I am trying to add dynamic `id` to `div` inside a `foreach` loop concatenated with value of variable `i`. It throws syntax errors. What might be the iss...

23 Jan at 07:21

C# Iterate over Dictionary sorted by value

C# Iterate over Dictionary sorted by value Is there any way to iterate over a Dictionary, in sorted order, sorted by VALUE not key? I did read abut the "SortedDictionary" object, but sadly, that is so...

14 Jan at 03:39

When should I use IEnumerator for looping in c#?

When should I use IEnumerator for looping in c#? I was wondering if there are any times where it's advantageous to use an IEnumerator over a foreach loop for iterating through a collection? For exampl...

19 Jan at 03:19

Can you 'exit' a loop in PHP?

Can you 'exit' a loop in PHP? I have a loop that is doing some error checking in my PHP code. Originally it looked something like this... This works all

8 Mar at 01:6

do-while loop in R

do-while loop in R I was wondering about how to write do-while-style loop? I found [this post](http://www.mail-archive.com/r-help@r-project.org/msg79174.html): > you can use repeat{} and check conditi...

22 Dec at 14:56

Implementing a loop using a timer in C#

Implementing a loop using a timer in C# I wanted to replace a counter based while loop with the timer based while loop in C#. Example : ``` while(count

2 Jul at 06:34

Calling remove in foreach loop in Java

Calling remove in foreach loop in Java In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance: As an addendum, is it legal to remo...

30 Jul at 20:16

Loop through each row of a range in Excel

Loop through each row of a range in Excel This is one of those things that I'm sure there's a built-in function for (and I may well have been told it in the past), but I'm scratching my head to rememb...

8 Jul at 20:13

How to generate different random numbers in a loop in C++?

How to generate different random numbers in a loop in C++? Is it possible to generate different random number, every time loop runs. For example, i have: ``` for (int t=0;t

15 Apr at 11:14

How to break out of multiple loops at once in C#?

How to break out of multiple loops at once in C#? What if I have nested loops, and I want to break out of all of them at once? In PHP, `break` takes an argument f

21 Aug at 18:29

How can I loop through a C++ map of maps?

How can I loop through a C++ map of maps? How can I loop through a `std::map` in C++? My map is defined as: For example, the above container holds data like this: ``` m["name1"]["value1"] = "data1"; m...

1 Jan at 15:36

bash shell nested for loop

bash shell nested for loop I want to write a nested for loop that has to work in the bash shell prompt. nested for loop in Single line command. For example, In the above example, for loop is executed ...

31 Jan at 05:23

What's the fastest way to loop through an array in JavaScript?

What's the fastest way to loop through an array in JavaScript? I learned from books that you should write for loop like this: ``` for(var i=0, len=arr.length; i

23 May at 03:41

Loop through list with both content and index

Loop through list with both content and index It is very common for me to loop through a python list to get both the contents their indexes. What I usually do is the following: I find this syntax a bi...

16 Jun at 19:40

Bash scripting, multiple conditions in while loop

Bash scripting, multiple conditions in while loop I'm trying to get a simple while loop working in bash that uses two conditions, but after trying many different syntax from various forums, I can't st...

23 Feb at 14:50

Prime numbers between 1 to 100 in C Programming Language

Prime numbers between 1 to 100 in C Programming Language I want to print prime numbers between 1 to 100, I write my code like the following but when I run it, it starts printing 3,7,11,17....91 Why no...

6 Dec at 07:2

Difference between foreach and for loops over an IEnumerable class in C#

Difference between foreach and for loops over an IEnumerable class in C# I have been told that there is a performance difference between the following code blocks. and I am no CLR expect but from what...

4 Sep at 17:20

Is there a way to access an iteration-counter in Java's for-each loop?

Is there a way to access an iteration-counter in Java's for-each loop? Is there a way in Java's for-each loop to find out how often the loop has already been processed? Aside from using the old and we...

27 Jan at 12:28

Iterating through the Alphabet - C# a-caz

Iterating through the Alphabet - C# a-caz I have a question about iterate through the Alphabet. I would like to have a loop that begins with "a" and ends with "z". After that, the loop begins "aa" and...

13 Dec at 09:35

Exit from nested loops at desired level

Exit from nested loops at desired level > [Breaking out of a nested loop](https://stackoverflow.com/questions/324831/breaking-out-of-a-nested-loop) How to exit from nested loops at a specific level....

23 May at 10:30

Check for null in foreach loop

Check for null in foreach loop Is there a nicer way of doing the following: I need a check for null to happen on file.Headers before proceeding with the loop In short it looks a bit ugly to write the...

31 Jul at 06:31

How to iterate over a JavaScript object?

How to iterate over a JavaScript object? I have an object in JavaScript: I want to use a `for` loop to get its properties. And I want to iterate it in parts (not all object properties at once). With a...

Looping through files in a folder Node.JS

Looping through files in a folder Node.JS I am trying to loop through and pick up files in a directory, but I have some trouble implementing it. How to pull in multiple files and then move them to ano...

10 Sep at 21:28

Looping through array and removing items, without breaking for loop

Looping through array and removing items, without breaking for loop I have the following for loop, and when I use `splice()` to remove an item, I then get that 'seconds' is undefined. I could check if...

19 Sep at 19:8

For Loop on Lua

For Loop on Lua My assignment is how to do a for loop. I have figured it out in terms of numbers but cannot figure it out in terms of names. I would like to create a for loop that runs down a list of ...

2 Jan at 01:31

foreach vs someList.ForEach(){}

foreach vs someList.ForEach(){} There are apparently many ways to iterate over a collection. Curious if there are any differences, or why you'd use one way over the other. First type: Other Way: ``` L...

1 Nov at 11:18

Replacement for for... if array iteration

Replacement for for... if array iteration I love list comprehensions in Python, because they concisely represent a transformation of a list. However, in other languages, I frequently find myself writi...

12 Sep at 05:14

C++11 reverse range-based for-loop

C++11 reverse range-based for-loop Is there a container adapter that would reverse the direction of iterators so I can iterate over a container in reverse with range-based for-loop? With explicit iter...

29 Aug at 20:51