Converting C Loop To XOJO

I’m converting an algorithm written in C to XOJO and I’m not sure if I’m converting a For loop correctly. This is the loop in C:

for (u=n-1; pred[u]>=0; u=pred[u]) {
    do stuff;
}

This is what I’ve come up with in XOJO:

u=n-1
Do
  do stuff
  u=pred(u)
Loop Until pred(u)>=0

Am I doing this correctly? Or is there a simpler way?

Thanks,

Ron

If you enclose code in [ code ] blocks (without the spaces) it gets formatted nicely.

Xojo does have for loops: For…Next

I’m not sure exactly what your loop does, but if you’re waiting for a value to equal something you may want a while loop instead.
While…Wend Reference

Your translation is clearly wrong at the algorithm level, and you should know it.

Well, I don’t know it. That’s why I’m asking.

Ok. Do you know C ? I presume you don’t known enough the target language yet (Xojo), but it’s essential that you know the source language ( C ) when doing language translations.

No, it’s the other way around for me. I know Xojo pretty well but I don’t know C very well.

As I understand it, in a For loop in C, there are three parameters: the starting value, the end condition, and the increment. In this case, the increment, u=pred[u], is a function of it’s current value, so a standard XOJO For…Next loop probably is not going to work. So I’m thinking that either a While…Wend or Do…Loop will have to be used here.

My apologies about not knowing to put the code in [ code ] blocks. Let me try again…

Original Code in C:

for (u=n-1; pred[u]>=0; u=pred[u]) {
    do stuff;
    }

My probably wrong XOJO translation:

u=n-1
Do
    do stuff
    u=pred(u)
Loop Until pred(u)>=0

Should not that be…

u=n-1
Do 
   //
   // do stuff
   //
   u=pred(u)
Loop Until pred(u) < 0

I’m guessing here…

PRED[u] is the same as U-1

so then if I’m right then

for (u=n-1; pred[u]>=0; u=pred[u]) {
do stuff;
}

should be this in XOJO

for u=N-1 downto 0
do stuff;
next u

There is a ‘downTo’? Hell, I’ve always used…

for u=(n-1) to 0 step -1 // - do stuff next u

Learn something new every day…

It’s been in there since, I think, REALbasic 1.0 :slight_smile:

Actually, pred[u] is NOT U-1, so it’s not a simple For…Downto…Next loop doesn’t look like it will work.

Then what is it?
If it is a C-function then you need to know what it does…
If it is a function defined in the C program … same thing (it just might be more complicated)

I think Syed may have nailed it. I’ve got the exit condition backwards I think.

pred[u] is an array of integers.

First thing to know is: know your variables, what are u, n , pred?

Then lets suppose things all are ints and a array of ints.

for (u=n-1; pred[u]>=0; u=pred[u]) {
do stuff;
}

Dim pred() As Integer = getThatSupposedArrayOfIntegers() Dim u As Integer = n - 1 While pred(u) >= 0 doStuff() u = pred(u) Wend

Ok, I’ll buy that. It’s really not a lot different than my original translation, only I had the exit condition reversed.

Apparently PRED is not a C function.

I realize you didn’t write it, Ron, but that’s a horrible C loop. No wonder this is causing confusion.

@Michel - right, sorry about that and the confusion that it might have caused. It’s an integer array.

pred is array,

Ron, it’s a very different algorithm. Your version had a terrible bug, it would doStuff() at least once even if it wasn’t desired. :slight_smile: