loops

Condition evaluation in loops ?

string strLine;//not constant int index = 0; while(index < strLine.length()){//strLine is not modified}; how many times strLine.length() is evaluated do we need to put use nLength with nLength assigned to strLine.length() just before loop ...

sun.java2d.loops.ProcessPath$Point

I am profiling an application suddenly using a lot of memory, and i am getting this: sun.java2d.loops.ProcessPath$Point As being allocated almost 11.000.000 times. What is it, and is there a solution to this? ...

Nested Loop / Loop Control tutorial

I'm looking for a good tutorial on writing and designing loops. I understand the basics of loops but nested loops give me a lot of trouble. To give you and idea, the following pattern below was kind of difficult for me to figure out. 1 12 123 1234 12345 123456 ...

C# Looping through an array without throwing exception

Say i have an array of values: string[] text = new string[] {"val1", "val2", "val3", "val4", "val5"}; Then i have a basic loop: for(int i=0;i<=30;i++) { Console.WriteLine(i + " = " + text[i]) } Obviously this will cause an out of bounds exception, so what i want to do is when the counter reaches the upper bound of the array the...

Subtract from an input appended list with a running balance output

Noob I am trying to write a script that gives a running balance. I am messing up on the elementary declared functions of python. I need it too: accept a balance via input append a list of transactions take those out one by one in the order they were input print a running total use pyhtmltable to make the output in html table rea...

How to use std::foreach with parameters/modification

I've found myself writing for(int i=0;i<myvec.size();i++) myvec[i]->DoWhatever(param); a lot, and I'd like to compress this into a foreach statement, but I'm not sure how to get param in there without going super-verbose. I've also got things like for(int i=0;i<myvec.size();i++) if(myvec[i]->IsOK()) myvec[i]->DoWhatever(p...

Which is the preferred condition in loop?

int n = 5; for(int i = 0;i!=n;i++)//condition != { //executing 5times } int n = 5; for(int i = 0;i<n;i++)//condition < { //executing 5times } Which one is preferred? This was example from "Accelerated C++ : practical programming by example / Andrew Koenig, Barbara E. Moo." Just wanted to know why the Author prefers the fi...

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 for(String s : stringArray) { doSomethingWith(s); } to find out how often the loop has already been processed? Aside from using using the old and well-known for(int i=0;i<boundary;i++)-loop, is the construct int i = 0; for(String s : stringArray) { doSomethingWith(s); i++; } the only wa...

How to display single column data in tabular format?

I want to display items as follows, <td1> <td2> <td3> <td4> 1 7 13 19 2 8 14 20 3 9 15 21 4 10 16 22 5 11 17 23 6 12 18 I am getting data (from 1......23) in a single column from database. N...

Replace Nested For Loops... or not

I have a script that loops through a series of four (or less) characters strings. For example: aaaa aaab aaac aaad If have been able to implement it with nested for loops like so: chars = string.digits + string.uppercase + string.lowercase for a in chars: print '%s' % a for b in chars: print '%s%s' % (a, b) ...

Is $_ more efficient than a named variable in Perl's foreach?

I am quite new in Perl and I woud like to know which of the following loops is more efficient: my @numbers = (1,3,5,7,9); foreach my $current (@numbers){ print "$current\n"; } or my @numbers = (1,3,5,7,9); foreach (@numbers){ print "$_\n"; } I want to know this in order to know if the use of $_ is more efficient because is...

Does a method that returns a collection get called in every iteration in a foreach statement in C#?

Hi, I was working with some C# code today in the morning and I had something like: foreach(DataRow row in MyMethod.GetDataTable().Rows) { //do something } So, as I dont have a full understanding of the language framework I would like to know if GetDataTable() gets called each time an iteration is done or if it just gets called once an...

python: restarting a loop

i have: for i in range(2,n): if(something): do something else: do something else i = 2 **restart the loop But that doesn't seem to work. Is there a way to restart that loop? Thanks ...

How to determine if a linked list has a cycle using only two memory locations.

Does anyone know of an algorithm to find if a linked list loops on itself using only two variables to traverse the list. Say you have a linked list of objects, it doesn't matter what type of object. I have a pointer to the head of the linked list in one variable and I am only given one other variable to traverse the list with. So my...

How can Polymorphism replace an if-else statement inside of a loop?

How can polymorphism replace an if-else statement or Switch inside of a loop? In particular can it always replace an if-else? Most of the if-thens I use inside of loops are arithmetic comparisons. This question is spawned from this question. int x; int y; int z; while (x > y) { if (x < z) { x = z; } } How woul...

Python loops with multiple lists?

<edit> Thanks to everyone who has answered so far. The zip and os.path.join are really helpful. Any suggestions on ways to list the counter in front, without doing something like this: zip(range(len(files)), files, directories) </edit> Hi, I'm in the process of learning Python, but I come from a background where the following pseudo...

Is there a way to loop through a sub section of a list in Python

So for a list that has 1000 elements, I want to loop from 400 to 500. How do you do it? I don't see a way by using the for each and for range techniques. ...

Accessing the index in Python for loops

Anyone knows how to access the index itself so for a list like this: ints = [8,23,45,12,78] when I loop through it using a for loop, how do I make access the for loop index, from 1 to 5 in this case? ...

Parallel iteration in C#?

Is there a way to do foreach style iteration over parallel enumerables in C#? For subscriptable lists, I know one could use a regular for loop iterating an int over the index range, but I really prefer foreach to for for a number of reasons. Bonus points if it works in C# 2.0 ...

Traverse a list in reverse in Python

So I can start from len (collection) and end in collection[0]. EDIT: Also sorry forgot to mention, I also want to be able to access the loop index. ...