for-loop

Unreachable code? WTF? (C#)

int x; int iHateDataRows = dt.Rows.Count + 2; System.Data.DataRow[] dra = new System.Data.DataRow[1000]; for(x = 0; x < iHateDataRows; ++x) So, this is obviously a loop but, my problem is that the '++x' tells me that it is unreachable. I don't know if this is just my IDE going crazy or something (I...

What are your tips for keeping track and avoiding bugs in loops ?

I just found ... AGAIN ... a real time wastage bug as follows for (int i = 0; i < length; i++) { //...Lots of code for (int j = 0; i < length; j++) { //...Lots of code } } Did you notice straight ahead the inner i which SHOULD BE j ? Neither did I. So from now on I am going to use: for (int i = 0; i < length; i...

What can go in the parentheses of a Java for loop?

My question is about the Java for statement, e.g. for (int i = 0; i < 10; ++i) {/* stuff */} What I don't understand is precisely how much code / what kind of code I can put in the parentheses (i.e. where I have int i = 0; i < 10; ++i in my example)- I don't really understand the language used to describe it here: http://java.sun.co...

Perl for loop explanation

I'm looking through perl code and I see this: sub html_filter { my $text = shift; for ($text) { s/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g; s/"/&quot;/g; } return $text; } what does the for loop do in this case and why would you do it this way? ...

using two for loops in python

Hello, I have started to learn python recently and have a question about for loops that I was hoping someone could answer. I want to be able to print all the possible products of two numbers from one to ten. so: 2 by 2, 2 by 3, 2 by 4...2 by 10, 3 by 2, 3 by 3...3 by 10, 4 by 2, 4 by 3 etc...I would have thought the easiest way to do so ...

c++ for loop temporary variable use

Hi, Which of the following is better and why? (Particular to c++) a. int i(0), iMax(vec.length());//vec is a container, say std::vector for(;i < iMax; ++i) { //loop body } b. for( int i(0);i < vec.length(); ++i) { //loop body } I have seen advice for (a) because of the call to length function. This is bothering me. Doesn't any...

For loop using find lacks doesn't properly handle directory names having white space character

Hi everyone.. Well i am really stuck at this one. I have dirs.txt which is as follows: /var/tmp/old files.txt /var/tmp/old backups.bak dirs.txt file is generated by the script itself. When i want to use dirs.txt in a for loop like: for dir in `cat dirs.txt` ; do ls -al $dir done Here is what the command throws: ls: cannot ac...

PHP PDO prepared query won't return results from for loop

Hi, I have the following code: $link = new PDO("mysql:dbname=$databasename;host=127.0.0.1",$username,$password); $query = $link->prepare("SELECT * FROM index WHERE sbeid=:idvar"); for($j = 1; $j < count($array); $j++) { if($array[$j][16] == "TRUE" || $array[$j][16] == "FALSE") { $paramforquery = $array[$j][25]; ...

Which block of code is 'better'?

In order to promote good programming habits and increase the efficiency of my code (Read: "My brother and I are arguing over some code"), I propose this question to experienced programmers: Which block of code is "better"? For those who can't be bothered to read the code, is it worth putting a conditional within a for-loop to decrease t...

Changing variable names with Python for loops

Hi, I was just wondering if anyone knew of a way to change variable names based off of a for loop for something like this: for i in range(3) group+i=self.getGroup(selected, header+i) so that the names of the variables change to accomodate the data. Thanks! ~Sam ...

Simplifying Data with a for loop (Python)

Hi, I was trying to simplify the code: header = [] header.append(header1) header.append(header2) header.append(header3) header.append(header4) header.append(header5) header.append(header6) where: header1 = str(input.head...

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. ...

Returning inside for loop inside lambda crashes in Ruby 1.8

In Ruby 1.8 (my version is 1.8.7-72), this code: foo = lambda do for j in 1..2 return end end foo.call crashes with a LocalJumpError: test2.rb:3: unexpected return (LocalJumpError) from test2.rb:2:in `each' from test2.rb:2 from test2.rb:6:in `call' from test2.rb:6 Why does it do this? However, it seems to ru...

Iterating a JavaScript object's properties using jQuery

Is there a jQuery way to perform iteration over an object's members, such as in: for (var member in obj) { ... } I just don't like this for sticking out from amongst my lovely jQuery notation! ...

for loop to interate over enum in Java?

I have an enum in Java for the cardinal & intermediate directions: public enum Direction { NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST } How can I write a for loop that iterates through each of these enum values? ...

Javascript: hiding prototype methods in for loop ?

So lets say I've added some prototype methods to the Array class: Array.prototype.containsKey = function(obj) { for(var key in this) if (key == obj) return true; return false; } Array.prototype.containsValue = function(obj) { for(var key in this) if (this[key] == obj) return true; return false; } t...

excel vba for each loop takes 2 steps at once

I want to process every element of a for-loop. Taking this code, why is just every second element processed? For Each row In ws.Rows If IsEmpty(row.Cells(row.row, 1)) Then Exit For Else MsgBox row.Cells(row.row, 1).value End If Next Thanks in advance for your answers! ...

Make a for loop with an aray and variable

I am using the code below to draw an object to the stage. However I want to be able to redraw the object every time I press ENTER in my onEnterframe function. So I have to add the draw function again but with a new array. Also every time I push enter the value of the variables in the array should increase. Here's the initial code: maat...

Moving files from one directory to another, using Java

Hello, I have problems with moving files in directories in Java. The problem is that I cannot understand why the program behaves the way it behaves. Below is a (slight) modification of my real program. I traverse directories of a directory. In each of these traversed directories there are text files, which I want to move into two subd...

in plsql, i saw some one use for loop without define the loop index, why? how?

hi everyone, in plsql, i saw some one use for loop without define the loop index, and the database can execute correctly. but i can't find the description of this syntax in oracle documentation. can anyone explain it? great thanks! the following code is an example, notice the *inner_c* is not defined: declare v_current_nr NUMBER; b...