for-loop

c++ performance issue beginner (size function in for loop)

hi when having a vector<int> var; for(int i=0; i< var.size();i++) , is the size() function called each time or only once ? from the answers I guess I better use iterators , or just have variable before the loop ...

What's wrong with my for loop?

I am making a game using lite-C (exactly same syntax as C). and I cannot make this loop work. It gives me an error at this line at compilation. for(int i = 0; i < (cantenemigu * 3); i += 3) I have an array with the information of where to create the enemies. the array contains the x,y,z coordinates. cantenemigu is the amount of enemi...

JavaScript: Looping over array

Would it be wrong to conclude that the fastest way to loop over a JavaScript array would be to use the for-in loop? E.g.: for (var index in items) { ... } See http://jsperf.com/loop-test2/2 ...

It it a bad practice to use break in a for loop?

Possible Duplicate: Break statements In the real world Hi, Is it a bad practice to use break statement inside a for loop? Say, I am searching for an value in an array. Compare inside a for loop and when value is found, break; to exit the for loop. Is this a bad practice? I have seen the alternative used : define a variable ...

how to return to top of for loop in javascript

Maybe this is a dumb question but is there a way to return to the top of a loop? Example: for(var i = 0; i < 5; i++) { if(i == 3) return; alert(i); } What you'd expect in other languages is that the alert would trigger 4 times like so: alert(0); alert(1); alert(2); alert(4); but instead, the function is exited imm...

In a loop, do any operations in the end-condition get evaluated in every iteration?

In the following code: for (var i = 0; i < object.length; i++){ .... } does the operation object.length get evaluated every time in the iteration? It would make most sense that the language will evaluate this once and save the result. However, I was reading some code where someone evaluated the operation before the loop star...

python for loop, how to find next value(object)?

HI, I'm trying to use for loop to find the difference between every two object by minus each other. So, how can I find the next value in a for loop? for entry in entries: first = entry # Present value last = ?????? # The last value how to say? diff = last = first ...

How do you Add Values in a Loop?

Adding Values in a Loop ...

Adding values in a for loop

I cant figure out how to add the values after it spits out the numbers. it says: Number: 5 // I typed 5 1 2 3 4 5 The sum is. So i need to add those number 1 2 3 4 5 but cant figure out how. import java.util.Scanner public class AddingValuesWithAForLoop { public static void main( String[] args ) { Scan...

Initiating the same loop with either a while or foreach statement

I have code in php such as the following: while($r = mysql_fetch_array($q)) { // Do some stuff } where $q is a query retrieving a set of group members. However, certain groups have there members saved in memcached and that memcached value is stored in an array as $mem_entry. To run through that, I'd normally do the following foreac...

finding empty folders and listing them

hey great community, I'm attempting to find empty folders in the CVS and list all folders that are empty in a text file. I would look in the HEAD. Would ideally use a TreeMap (string to store mailer name, boolean to store true or false) and a For loop for the scan. boolean should be set to true at first and if a mailer is empty (folder) ...

Actionscript 3: Dynamically adding movieclips constrained to a container

Last Edit: Resolved! Well, i was unable to find the ENTIRE answer here but i finally got what i was after. thanks very much for all of your help and patience. as a side note: i think i may have been having problems with using the int and Number types, upon closer inspection of my solution, i realised that Number was being used and not ...

Visual Studio Visual Basic 2010/ Function & Sub & For Loop

For example, input values are Cost = 1250, Salvage = 150, Life 5. Then the output I want is: $500.00 $300.00 $180.00 $108.00 $12.00 However, the program outputs: $500.00 $500.00 $500.00 $500.00 $500.00 P.S. This is for the DDB Also, how could I add numbers before each values like 1 $500.00 2 $300.00 3 $180.00 4 $108.00 5 $12.00 Thank yo...

Having an issue with nested for loops in R...

Hello, I am currently using apTreeshape to simulate phylogenetic trees using the "Yule-Hardy" Method. What I want to do is randomly generate between 20 and 25 different numbers for three different groupings (small, medium and large trees) and then generate about 40 trees for every random number chosen from within the grouping. I know h...

in python 2.6 how do i change a global variable within a for loop

i want to change the value of a variable within a for loop, but then have the new preserved variable changed when the loop finishes my attempt (kinda simplified from what i'm actually doing) SNP is a list and compar_1 is a list of lists line_1 = empty_row for SNP1 in compar_1: global line_1 if SNP[3] == SNP1[3] compar...

for-loop, increment by double

Hi, I want to use the for loop for my problem, not while. Is it possible to do the following?: for(double i = 0; i < 10.0; i+0.25) I want to add double values. ...

Jquery for loop doesnt stop

for (var i=0;i<ToArray.length;i++) { $.post('jquery/messages.php',{To:ToArray[i],subject:subject,msg:msg},function(data){jAlert(data);}); } User can send short messages to another user(s). The names are commaseparetad and after i split the string i want to send the same message to the different users with a for loop. if som...

Endless for loop

Hi, I have the following loop. for(byte i = 0 ; i < 128; i++){ System.out.println(i + 1 + " " + name); } When I execute my programm it prints all numbers from -128 to 127 in a loop. But the loop seems to be endless. Any ideas? ...

Print a x,y matrix, java

I want to print the followin matrix using for loops: 1 2 3 4 5 6 2 3 4 5 6 1 3 4 5 6 1 2 4 5 6 1 2 3 5 6 1 2 3 4 6 1 2 3 4 5 I use: public static void main ( String [ ] args ) { for(int w = 1; w <= 6; w++){ System.out.println(""); for(int p = w; p <= 6; p++){ System.out.print(p + ""); } ...

Very basic numerical comparison program

For some reason, I can't understand the logic of a program that: Takes in a list of numbers Goes through them (via a for each loop) to find the least number displays the least number via writeline If leastNumber must be initialized to 0, then won't the leastNumber ALWAYS be 0? (I've provided a basic txt file containing a list of var...