for-loop

(C) What is the difference between ++i and i++

In C, what is the difference between using ++i and i++. And which should be used in the incrementation block of a for loop? ...

Why does my "3n+1 problem" program not compile ?

I'm trying to solve the 3n+1 problem and I have a for loop that looks like this: for(int i = low; i <= high; ++i) { res = runalg(i); if (res > highestres) { highestres = res; } } Unfortunately I'm getting this error when I try to ...

Can parallel traversals be done in MATLAB just as in Python?

Using the zip function, Python allows for loops to traverse multiple sequences in parallel. for (x,y) in zip(List1, List2): Does MATLAB have an equivalent syntax? If not, what is the best way to iterate over two parallel arrays at the same time using MATLAB? ...

haXe and arrays Dynamic type

Hi, I know it's unlikely but maybe there is someone who knows haXe language. I have a variable of Dynamic type and I know for sure one of it's fields, lets call it an 'a' actually is an array. But when I'm writing var d : Dynamic = getDynamic(); for (t in d.a) { } I get a compilation error on line two, saying 'You can't iterate on a ...

Iterate over and check the byte value of every character in a string - VBA

Iterate over and check the byte value of every character in a string - VBA Code I have: cell_val = CStr(Nz(fld.value, "")) Dim iter As Long For iter = 0 To Len(cell_val) - 1 Step 1 If Asc(Mid(cell_val, iter, 1)) > 127 Then addlog "Export contains ascii character > 127" End If ...

Loop termination conditions

These for-loops are among the first basic examples of formal correctness proofs of algorithms. They have different but equivalent termination conditions: 1 for ( int i = 0; i != N; ++i ) 2 for ( int i = 0; i < N; ++i ) The difference becomes clear in the postconditions: The first one gives the strong guarantee that i == N after...

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 } increment i } Then I ask the question: would you write this? i = begin mid = ( end - begin ) / 2 // (the middle element) while ( i != end ) {...

Loop from 0x0000 to 0xFFFF

I'd like a loop that uses a UInt16 (ushort) to loop through all of its values. However, this doesn't do it: for( ushort i = 0; i < UInt16.MaxValue; i++ ) { // do something } The problem is that the loop will quit when i == 0xFFFF and not "do something". If I change the 'for' statement to "for(ushort i = 0; i <= UInt16.MaxValue; i...

Getting the first item out of a For loop

I am writing my own Joomla component (MVC), its based heavily on the newsflash module, because I want to display the latest 5 content items in a sliding tabbed interface, all the hard work is done, but I am having real difficult getting content out of the for loop. Here is the code I have so far default.php <ul id="handles" class="tabs...

Is there a performance difference between a for loop and a for-each loop?

What, if any, is the performance difference between the following two loops? for(Object o: objectArrayList){ o.DoSomthing(); } and for(int i=0; i<objectArrayList.size(); i++){ objectArrayList.get(i).DoSomthing(); } ...

Can't do shell script with a repeat with i from 1 to n loop

This works (prints, for example, “3 arguments”): to run argv do shell script "echo " & (count argv) & " arguments" end run This doesn't (prints only “Argument 3: three”, and not the previous two arguments): to run argv do shell script "echo " & (count argv) & " arguments" repeat with i from 1 to (count argv) do shell script "ec...

What is the full "for" loop syntax in C (and others in case they are compatible) ?

I have seen some very weird for loops when reading other people's code. I have been trying to search for a full syntax explanation for the for loop in C but it is very hard because the word "for" appears in unrelated sentences making the search almost impossible to Google effectively. This question came to my mind after reading this thr...

Elements order - for (... in ...) loop in javascript

Does the for...in loop in Javascript loops through the hashtables/elements in the order they are declared? Is there a browser which doesn't do it in order? The object I wish to use will be declared once and will never be modified. Suppose I have: var myObject = { A: "Hello", B: "World" }; And I further use them in: for (var item in...

Using foreach (...) syntax while also incrementing an index variable inside the loop

When looking at C# code, I often see patterns like this: DataType[] items = GetSomeItems(); OtherDataType[] itemProps = new OtherDataType[items.Length]; int i = 0; foreach (DataType item in items) { // Do some stuff with item, then finally itemProps[i] = item.Prop; i++; } The for-loop iterates over the objects in items, b...

C#, For Loops, and speed test... Exact same loop faster second time around?

public Int64 ReturnDifferenceA() { User[] arrayList; Int64 firstTicks; IList<User> userList; Int64 secondTicks; System.Diagnostics.Stopwatch watch; userList = Enumerable .Range(0, 1000) .Select(currentItem => new User()).ToList(); arrayList = userList.ToArray(); watch = new Stopwatch(); wa...

Breaking out of a nested loop

If I have a for loop which is nested within another, how can I efficiently come out of both loops (inner and outer) in the quickest possible way? I don't want to have to use a boolean and then have to say go to another method, but rather just to execute the first line of code after the outer loop. What is a quick and nice way of going ...

new dynamic tools analysis for C-code

Could somebody tell me wich new dynamic tools analysis for C-code are there like valdgrind? ...

xsl:for-each loop counter

How do I save the iterations that have occurred in an xsl:for-each? (variables in XSL are immutable) My goal is to find the MAX number of children for any node at a particular level. For example, I might want to print that there are no more than 2 Response nodes for any Question in this survey: <?xml version="1.0" encoding="ISO-8859-1...

Batch how to end an for-loop properly

Hi, for testing purposes i need an recursive directory with some files, that comes to maximum path-length. The Script used for the creation consists only of two for-loops, as followed: for /L %%a in (1 1 255) do @( mkdir %%a && cd %%a && for /L %%b in (1 1 %random%) do @( echo %%b >> %%a.txt ) ) Now I would li...

In .NET which loop runs faster for or foreach

In c#/VB.NET/.NET which loop runs faster for or foreach? Ever since I read that for loop works faster than foreach a long time ago I assumed it stood true for all collections, generic collection all arrays etc. I scoured google and found few articles but most of them are inconclusive (read comments on the articles) and open ended. Wha...