f



Nested While loop inside For loop

I have been coding in matlab for a while and I am confused
about the below code:

It seems like the "For loop" iterator variable "Index" does
not update inside the "While loop". Please help me understand!

I hate to assume this is some error induced on Matlab's end.


---------- CODE ------------

for Index = 390000:length(Stopxy)
    fprintf('Index: %d, \n',Index);
    while (Stopxy_YCoord(Index) < LowToHighThresh)
        fprintf('Index: %d, Yval: %f
\n',Index,Stopxy_YCoord(Index));
        Stopxy_YCoord(Index);
        Index = Index + 1;
    end
    BitTime = [BitTime; Stopxy_XCoord(Index)];
    while (Stopxy_YCoord(Index) > HighToLowThresh)
        fprintf('Index: %d, Yval: %f
\n',Index,Stopxy_YCoord(Index));
        Stopxy_YCoord(Index);
        Index = Index + 1;
    end
    BitTime = [BitTime; Stopxy_XCoord(Index)];
    fprintf('Index: %d, \n',Index);
end

0
jjzachar (5)
7/6/2008 12:18:02 AM
comp.soft-sys.matlab 211264 articles. 26 followers. lunamoonmoon (257) is leader. Post Follow

12 Replies
1893 Views

Similar Articles

[PageSpeed] 47

"Jerry " <jjzachar@uncc.edu> wrote in message
<g4p2vq$q0j$1@fred.mathworks.com>...
> I have been coding in matlab for a while and I am confused
> about the below code:
> 
> It seems like the "For loop" iterator variable "Index" does
> not update inside the "While loop". Please help me understand!
> 
> I hate to assume this is some error induced on Matlab's end.
> 
> 
> ---------- CODE ------------
> 
> for Index = 390000:length(Stopxy)
>     fprintf('Index: %d, \n',Index);
>     while (Stopxy_YCoord(Index) < LowToHighThresh)
>         fprintf('Index: %d, Yval: %f
> \n',Index,Stopxy_YCoord(Index));
>         Stopxy_YCoord(Index);
>         Index = Index + 1;
>     end
>     BitTime = [BitTime; Stopxy_XCoord(Index)];
>     while (Stopxy_YCoord(Index) > HighToLowThresh)
>         fprintf('Index: %d, Yval: %f
> \n',Index,Stopxy_YCoord(Index));
>         Stopxy_YCoord(Index);
>         Index = Index + 1;
>     end
>     BitTime = [BitTime; Stopxy_XCoord(Index)];
>     fprintf('Index: %d, \n',Index);
> end
> 

read the "remarks" section of 'doc for'. The incrementing of
index inside the while loop is being ignored because the for
statment reassigns its own value to index at the start of
each for iteration. 

Im not entirely sure what you're trying to do, but if you're
trying to skip iterations, check out the continue command.  


0
n.a (539)
7/6/2008 12:26:03 AM
"matt dash" <n.a@mail.com> wrote in message
<g4p3er$u1$1@fred.mathworks.com>...
> "Jerry " <jjzachar@uncc.edu> wrote in message
> <g4p2vq$q0j$1@fred.mathworks.com>...
> > I have been coding in matlab for a while and I am confused
> > about the below code:
> > 
> > It seems like the "For loop" iterator variable "Index" does
> > not update inside the "While loop". Please help me
understand!
> > 
> > I hate to assume this is some error induced on Matlab's end.
> > 
> > 
> > ---------- CODE ------------
> > 
> > for Index = 390000:length(Stopxy)
> >     fprintf('Index: %d, \n',Index);
> >     while (Stopxy_YCoord(Index) < LowToHighThresh)
> >         fprintf('Index: %d, Yval: %f
> > \n',Index,Stopxy_YCoord(Index));
> >         Stopxy_YCoord(Index);
> >         Index = Index + 1;
> >     end
> >     BitTime = [BitTime; Stopxy_XCoord(Index)];
> >     while (Stopxy_YCoord(Index) > HighToLowThresh)
> >         fprintf('Index: %d, Yval: %f
> > \n',Index,Stopxy_YCoord(Index));
> >         Stopxy_YCoord(Index);
> >         Index = Index + 1;
> >     end
> >     BitTime = [BitTime; Stopxy_XCoord(Index)];
> >     fprintf('Index: %d, \n',Index);
> > end
> > 
> 
> read the "remarks" section of 'doc for'. The incrementing of
> index inside the while loop is being ignored because the for
> statment reassigns its own value to index at the start of
> each for iteration. 
> 
> Im not entirely sure what you're trying to do, but if you're
> trying to skip iterations, check out the continue command.  
> 
> 
where do I find this "remarks" section? 
The matlab version, I was given to use is: 7.0.1.24704 (R14)
Service Pack 1
0
jjzachar (5)
7/6/2008 12:48:01 AM
matt dash wrote:
> "Jerry " <jjzachar@uncc.edu> wrote in message
> <g4p2vq$q0j$1@fred.mathworks.com>...
>> I have been coding in matlab for a while and I am confused
>> about the below code:
>>
>> It seems like the "For loop" iterator variable "Index" does
>> not update inside the "While loop". Please help me understand!
>>
>> I hate to assume this is some error induced on Matlab's end.
>>
>>
>> ---------- CODE ------------
>>
>> for Index = 390000:length(Stopxy)
>>     fprintf('Index: %d, \n',Index);
>>     while (Stopxy_YCoord(Index) < LowToHighThresh)
>>         fprintf('Index: %d, Yval: %f
>> \n',Index,Stopxy_YCoord(Index));
>>         Stopxy_YCoord(Index);
>>         Index = Index + 1;
>>     end
>>     BitTime = [BitTime; Stopxy_XCoord(Index)];
>>     while (Stopxy_YCoord(Index) > HighToLowThresh)
>>         fprintf('Index: %d, Yval: %f
>> \n',Index,Stopxy_YCoord(Index));
>>         Stopxy_YCoord(Index);
>>         Index = Index + 1;
>>     end
>>     BitTime = [BitTime; Stopxy_XCoord(Index)];
>>     fprintf('Index: %d, \n',Index);
>> end
>>
> 
> read the "remarks" section of 'doc for'. The incrementing of
> index inside the while loop is being ignored because the for
> statment reassigns its own value to index at the start of
> each for iteration. 
> 
> Im not entirely sure what you're trying to do, but if you're
> trying to skip iterations, check out the continue command.  

Or replace the outer For loop with another While

Index = 390000;
while Index<=length(Stopxy)
.....
.....
   Index = Index+1;
end

Dave
0
dbell5608 (63)
7/6/2008 12:48:59 AM
Dave Bell <dbell@TheSPAMFREEBells.net> wrote in message
<ZDUbk.21597$co7.6575@nlpi066.nbdc.sbc.com>...
> matt dash wrote:
> > "Jerry " <jjzachar@uncc.edu> wrote in message
> > <g4p2vq$q0j$1@fred.mathworks.com>...
> >> I have been coding in matlab for a while and I am confused
> >> about the below code:
> >>
> >> It seems like the "For loop" iterator variable "Index" does
> >> not update inside the "While loop". Please help me
understand!
> >>
> >> I hate to assume this is some error induced on Matlab's
end.
> >>
> >>
> >> ---------- CODE ------------
> >>
> >> for Index = 390000:length(Stopxy)
> >>     fprintf('Index: %d, \n',Index);
> >>     while (Stopxy_YCoord(Index) < LowToHighThresh)
> >>         fprintf('Index: %d, Yval: %f
> >> \n',Index,Stopxy_YCoord(Index));
> >>         Stopxy_YCoord(Index);
> >>         Index = Index + 1;
> >>     end
> >>     BitTime = [BitTime; Stopxy_XCoord(Index)];
> >>     while (Stopxy_YCoord(Index) > HighToLowThresh)
> >>         fprintf('Index: %d, Yval: %f
> >> \n',Index,Stopxy_YCoord(Index));
> >>         Stopxy_YCoord(Index);
> >>         Index = Index + 1;
> >>     end
> >>     BitTime = [BitTime; Stopxy_XCoord(Index)];
> >>     fprintf('Index: %d, \n',Index);
> >> end
> >>
> > 
> > read the "remarks" section of 'doc for'. The incrementing of
> > index inside the while loop is being ignored because the for
> > statment reassigns its own value to index at the start of
> > each for iteration. 
> > 
> > Im not entirely sure what you're trying to do, but if you're
> > trying to skip iterations, check out the continue command.  
> 
> Or replace the outer For loop with another While
> 
> Index = 390000;
> while Index<=length(Stopxy)
> ....
> ....
>    Index = Index+1;
> end
> 
> Dave

Yeah ... that is exactly what I was going to do. I wonder
why, Matlab set up the scope of the iterator like that. oh
well! 

Thanks a lot folks!
0
jjzachar (5)
7/6/2008 12:53:03 AM
"Jerry " <jjzachar@uncc.edu> wrote in message 
news:g4p51f$43o$1@fred.mathworks.com...
> Dave Bell <dbell@TheSPAMFREEBells.net> wrote in message

>
> Yeah ... that is exactly what I was going to do. I wonder
> why, Matlab set up the scope of the iterator like that. oh
> well!
>
> Thanks a lot folks!

In a more strongly typed languages (like Ada), it is a COMPILE TIME error to 
even try to touch the loop variable, never mind updating it.

It is a reallly bad programming  to attempt to modify the loop index 
variable even if the language does allow it.

Nasser


0
Nasser
7/6/2008 1:06:01 AM
Nasser Abbasi wrote:
> "Jerry " <jjzachar@uncc.edu> wrote in message 
> news:g4p51f$43o$1@fred.mathworks.com...
>> Dave Bell <dbell@TheSPAMFREEBells.net> wrote in message
> 
>> Yeah ... that is exactly what I was going to do. I wonder
>> why, Matlab set up the scope of the iterator like that. oh
>> well!
>>
>> Thanks a lot folks!
> 
> In a more strongly typed languages (like Ada), it is a COMPILE TIME error to 
> even try to touch the loop variable, never mind updating it.
> 
> It is a reallly bad programming  to attempt to modify the loop index 
> variable even if the language does allow it.
> 
> Nasser

So, I guess you wouldn't much approve of self-modifying machine code?

:{)

Dave
0
dbell5608 (63)
7/6/2008 4:53:41 AM
In article <nUUbk.11302$cW3.5286@nlpi064.nbdc.sbc.com>,
Nasser Abbasi <nma@12000.org> wrote:

>In a more strongly typed languages (like Ada), it is a COMPILE TIME error to 
>even try to touch the loop variable, never mind updating it.

I haven't looked at Ada, but what you write there sounds wrong to me.

Needing to "touch" the loop variable is much more common than
not in loops. Reading the value of the loop variable is "touching"
the variable. If Ada acts the way you indicate, then how could you
program a simple loop such as

  for K=1:20
    A(K) = B(F(K))
  end

I used this example instead of something obvious such as

  for K=1:20
    A(K) = B(K)
  end

against the off-chance that Ada requires this to be done by
simple vector assignment along the lines of

  A(1:20) = B(1:20);


You have implied that updating a loop variable is a compile time
error in Ada because of its typing system. That implies that
loop variables assume the equivilent of a "const" qualifier within
the body of the loop, and yet are not themselves "const" because
the loop control are allowed to update them -- and by arbitrary amounts
too in order that there can be the equivilent of what in C would be

  for (K=1: K <= 20; K += F(K)) { /* loop body */ }

The implication would be the loop variable would have to change types,
being non-const for the purpose of the index update but const
otherwise. This in turn introduces nasty "is it or isn't it"
about constructs such as "for (K=1: K <= 20; K += F(++K))"


Based upon my experience with programming languages, I think it
fairly likely that you are mistaken, and that if Ada prohibits
updating the loop index inside the loop, that it is not because
Ada is strongly typed, but rather that Ada simply declares such
an attempt to be a constraint violation or semantic error -- something
that could be done with a weakly typed language as well.
-- 
   "No one has the right to destroy another person's belief by
   demanding empirical evidence."                -- Ann Landers
0
roberson2 (8602)
7/6/2008 3:41:02 PM
The remarks section i was talking about (2007b) says:

It is recommended that you do not assign to the loop control
variable while in the body of a loop. If you do assign to a
variable that
has the same name as the loop control variable (see k in
the example below), then the value of that variable
alternates between the
value assigned by the for statement at the start of each
loop iteration and the value explicitly assigned to it in
the loop code:

for k=1:2
   disp(sprintf('   At the start of the loop, k = %d', k))
   k = 10;
   disp(sprintf('   Following the assignment, k = %d\n', k))
end

   At the start of the loop, k = 1
   Following the assignment, k = 10

   At the start of the loop, k = 2
   Following the assignment, k = 10
0
n.a (539)
7/7/2008 3:56:01 AM
In article <g4s44h$nhc$1@fred.mathworks.com>, matt dash <n.a@mail.com> wrote:
>The remarks section i was talking about (2007b) says:

>It is recommended that you do not assign to the loop control
>variable while in the body of a loop.

Yes, it appears to me that your initial reply in this thread
was correct about what was happening for Matlab. But the posting
you replied to here was my questioning Nasser Abbasi on Nasser's
statement that:

>>>In a more strongly typed languages (like Ada), it is a COMPILE TIME error to 
>>>even try to touch the loop variable, never mind updating it.

which still appears to be a doubtful statement to me.
-- 
  "The quirks and arbitrariness we observe force us to the
  conclusion that ours is not the only universe." -- Walter Kistler
0
roberson2 (8602)
7/7/2008 4:27:38 AM
"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message 
news:g4s5vq$rn8$1@canopus.cc.umanitoba.ca...
> In article <g4s44h$nhc$1@fred.mathworks.com>, matt dash <n.a@mail.com> 
> wrote:
>>The remarks section i was talking about (2007b) says:
>
>>It is recommended that you do not assign to the loop control
>>variable while in the body of a loop.
>
> Yes, it appears to me that your initial reply in this thread
> was correct about what was happening for Matlab. But the posting
> you replied to here was my questioning Nasser Abbasi on Nasser's
> statement that:
>


>>>>In a more strongly typed languages (like Ada), it is a COMPILE TIME 
>>>>error to
>>>>even try to touch the loop variable, never mind updating it.
>
> which still appears to be a doubtful statement to me.

Walter: I was trying to be funny when I said 'touching' the loop variable.

Of course one can read the loop variable in Ada. It is not allowed to update 
it either directly or indirectly. It is detected as error at compile time. 
Loop variable in ada is handled as a CONSTANT inside the loop.

I'll try to put a simley :) next time when I am trying to be funny.

Nasser






0
Nasser
7/7/2008 5:17:14 AM
"Nasser Abbasi" <nma@12000.org> wrote in message 
news:zFhck.14804$N87.10930@nlpi068.nbdc.sbc.com...
>

> Of course one can read the loop variable in Ada. It is not allowed to 
> update it either directly or indirectly. It is detected as error at 
> compile time. Loop variable in ada is handled as a CONSTANT inside the 
> loop.
>

Ofcourse, I meant the loop variable is CONSTANT per each iteration. (it is 
updated by the code generated by the compiler at end of each iteration) but 
during each one iteration, it is treated as a constant from the point of 
view of being accessed by user code.

Nasser


0
Nasser
7/7/2008 5:26:07 AM
In article <VNhck.14806$N87.6483@nlpi068.nbdc.sbc.com>,
Nasser Abbasi <nma@12000.org> wrote:

>"Nasser Abbasi" <nma@12000.org> wrote in message 
>news:zFhck.14804$N87.10930@nlpi068.nbdc.sbc.com...

>> Of course one can read the loop variable in Ada. It is not allowed to 
>> update it either directly or indirectly. It is detected as error at 
>> compile time. Loop variable in ada is handled as a CONSTANT inside the 
>> loop.

>Ofcourse, I meant the loop variable is CONSTANT per each iteration. (it is 
>updated by the code generated by the compiler at end of each iteration) but 
>during each one iteration, it is treated as a constant from the point of 
>view of being accessed by user code.

I looked it up in the Ada standard. You are correct that the
'for' loop control parameter in Ada is considered to be constant
according to the standard. The 'for' statement introduces
a new scope that the loop control parameter is part of,
coming into existance with at that point (shadowing any
variable with the same name in an outer scope) and disappearing
again at the end of the loop structure (so its final value
cannot be examined.)

The Ada 'for' statement can iterate over every member in a discrete
subrange (e.g., -3..5), or over every member of an array (I didn't
dig into that), but there appears to be no Ada equivilent of the C

  for (K=0; L(); K++) { /* body */ }
or
  for (K=0; K<20; K += F()) { /* body */ }

where L() is a function that returns a value suitable for a logical
test, and F() is a function returning a numeric value.

For example,

  for (K=0; K < 20 && !eof(stdin); K++) { inbuffer[K] = getchar(); }

is valid C, but would appear to have no way of being written
as an Ada 'for' loop. It could, of course, be written as an
Ada 'while' loop, but then the constantness you refer to would
be lost, as the invariability of the loop parameter applies only
for 'for' loops (Ada 05 section 5.5 Note 5, or more clearly,
Ada 83 section 5.5)

  5 A loop parameter is a constant; it cannot be updated within
    the sequence_of_statements of the loop (see 3.3).

However, we can see from Ada 05 section 5.5 exactly when the loop
control is considered to come into existance, and thus see that it is
*not* the case that it is considered to come into existance at the
beginning of each iteration:  it is stated as coming into existance as
part of the elaboration of the loop parameter specification.
The identifier is not logically destroyed and recreated each loop
and the identifier is constant: how then can the identifier be
updated to the next discrete subrange value or next array member?
It cannot. From this we deduce that the official Ada specifications are
either either prescriptive and are broken (inconsistent) or
else that the official Ada specifications are -descriptive- and
are flawed in not correctly describing the actual semantics of the
[hypothetical] Ada reference implementation being defined. And as
best I know, the official Ada standards are heavily -prescriptive-,
*defining* how Ada must work...
-- 
  "The whole history of civilization is strewn with creeds and
  institutions which were invaluable at first, and deadly
  afterwards."                                -- Walter Bagehot
0
roberson2 (8602)
7/7/2008 6:41:45 AM
Reply: