Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > for conditional handling question

Reply
Thread Tools

for conditional handling question

 
 
Angus
Guest
Posts: n/a
 
      01-02-2011
Hello

My understanding is that the conditional expression in a for loop is
evaluated BEFORE each iteration. I understood this to mean that the
evaluation of a counter used would be BEFORE the counter is
incremented (or decremented or however changed).

But in my test code below:

for(; s[i] < end; ++i)

this does not appear to be happening.

test code:
char start = 0;
char end = 0;
int i, j;
char s[27] = {0};

i=j=0;
start = 'A';
end = 'Z';

for(; s[i] < end; ++i)
s[i] = start + i;

I was expecting the char array s to be filled with A-Z and then stop.
ie in the test s[i] < end, when s[i] is Z (decimal 90) then end is
also Z (90) and so for loop should stop. But the for loop does NOT
stop.

If I try
for(; s[i-1] < end; ++i)
s[i] = start + i;

Then the for loop does end at the correct point so I am assuming that
i is incremented before the test. Can someone please confirm what is
the expected behaviour.

Angus
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      01-02-2011
Le 02/01/11 10:47, Angus a écrit :
> Hello
>
> My understanding is that the conditional expression in a for loop is
> evaluated BEFORE each iteration. I understood this to mean that the
> evaluation of a counter used would be BEFORE the counter is
> incremented (or decremented or however changed).
>
> But in my test code below:
>
> for(; s[i]< end; ++i)
>
> this does not appear to be happening.
>


You test for s[i] < 'Z'. Since s[i] contains 27
zeroes, it will be ALWAYS smaller than 'Z'. You go beyond
the bounds of the array.
 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      01-02-2011
Angus wrote:
) Hello
)
) My understanding is that the conditional expression in a for loop is
) evaluated BEFORE each iteration. I understood this to mean that the
) evaluation of a counter used would be BEFORE the counter is
) incremented (or decremented or however changed).

The evaluation of all the code inside the loop body
is also done BEFORE the counter is incremented.

for (S; C; I) { X; Y; }

is done in this order:
S; C; X; Y; I; C; X; Y; I; ... X; Y; I; C;
(The last C is false, all the others are true)

With that in mind, try to think about what your code is doing.

) test code:
) char start = 0;
) char end = 0;
) int i, j;
) char s[27] = {0};
)
) i=j=0;
) start = 'A';
) end = 'Z';
)
) for(; s[i] < end; ++i)
) s[i] = start + i;
....
) Then the for loop does end at the correct point so I am assuming that
) i is incremented before the test. Can someone please confirm what is
) the expected behaviour.

If you connect the ends of a loop, then the end of the loop comes right
before the start of the loop. The increment, which is at the end, is
directly followed by the conditional, which is at the start.

So yes, this is the expected behaviour.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-02-2011
On Jan 2, 9:47*am, Angus <(E-Mail Removed)> wrote:

> My understanding is that the conditional expression in a for loop is
> evaluated BEFORE each iteration.


yes

> *I understood this to mean that the
> evaluation of a counter used would be BEFORE the counter is
> incremented (or decremented or however changed).


no

this:-
for (i = begin(); i = inc(i); is_end(i))
proc();

is roughly equivalent to:-
i = begin();
again:
if (is_end(i))
goto exit;
proc();
i = inc(i);
goto again;
exit:


> But in my test code below:
>
> for(; s[i] < end; ++i)
>
> this does not appear to be happening.
>
> test code:
> * * * * char start = 0;
> * * * * char end = 0;
> * * * * int i, j;
> * * * * char s[27] = {0};
>
> * * * * i=j=0;
> * * * * start = 'A';
> * * * * end = 'Z';
>
> * * * * for(; s[i] < end; ++i)
> * * * * * * * * s[i] = start + i;
>
> I was expecting the char array s to be filled with A-Z and then stop.
> ie in the test s[i] < end, when s[i] is Z (decimal 90) then end is
> also Z (90) and so for loop should stop. *But the for loop does NOT
> stop.
>
> If I try
> * * * * for(; s[i-1] < end; ++i)
> * * * * * * * * s[i] = start + i;
>
> Then the for loop does end at the correct point so I am assuming that
> i is incremented before the test. *Can someone please confirm what is
> the expected behaviour.
>
> Angus


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-02-2011
On Jan 2, 10:05*am, China Blue Ribbon <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)..com>,
>
> *Angus <(E-Mail Removed)> wrote:
>
> Why not just code what you meant instead of writing for Obfusticated C?
>
> * * int c, i; for (c='A', i=0; c<'Z'; c++, i++) s[i] = c;


this assumes ASCII or someother code where the letters are contiguous
and in order


> or just
>
> * * memcpy(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-02-2011
On 1/2/2011 4:47 AM, Angus wrote:
> Hello
>
> My understanding is that the conditional expression in a for loop is
> evaluated BEFORE each iteration.


Yes.

> I understood this to mean that the
> evaluation of a counter used would be BEFORE the counter is
> incremented (or decremented or however changed).


I'm not sure what this means. The middle expression in a `for'
is just an expression, evaluating whatever it evaluates and producing
whatever side-effects it produces.

> But in my test code below:
>
> for(; s[i]< end; ++i)
>
> this does not appear to be happening.
>
> test code:
> char start = 0;
> char end = 0;
> int i, j;
> char s[27] = {0};
>
> i=j=0;
> start = 'A';
> end = 'Z';
>
> for(; s[i]< end; ++i)
> s[i] = start + i;
>
> I was expecting the char array s to be filled with A-Z and then stop.
> ie in the test s[i]< end, when s[i] is Z (decimal 90) then end is
> also Z (90) and so for loop should stop. But the for loop does NOT
> stop.


There's nothing to stop it. Consider the first iteration: for's
middle expression finds `i' is 0, `s[0]' is 0, and that is less than
`end' (the values of all the "standard" characters are strictly
positive. So the loop body executes and deposits 'A' in `s[0]'.
Then the third expression executes, setting `i' to 1. Back to the
middle expression: `i' is 1, `s[1]' is 0, that's less than `end', and
the loop body executes again (the precise effect this time depends on
the character coding; it is not a given that 'A'+1 == 'B'). Then the
third expression sets `i' to 2, the middle expression finds `s[2]' is
zero, and the loop proceeds merrily along.

After the `i=26' iteration things get strange. The third expression
sets `i' to 27, and then the middle expression executes. Since it tries
to inspect the non-existent `s[27]' there's really no telling what might
happen. Most probably -- but not for certain -- the loop will plow
forward through the memory locations that happen to follow `s', and
may eventually stop if it encounters one that happens to hold a value
greater than 'Z', meanwhile scribbling over whatever else it finds
along the way. Maybe it will scribble on something important, maybe
not. Maybe it will scribble on `end' or on `i' and make the behavior
very erratic. Maybe it will crash.

> If I try
> for(; s[i-1]< end; ++i)
> s[i] = start + i;


This is also bad: On the very first test, when `i' is 0, it
tries to inspect the non-existent `s[-1]'. Lord only knows what
it might find there.

> Then the for loop does end at the correct point so I am assuming that
> i is incremented before the test. Can someone please confirm what is
> the expected behaviour.


The statement `for (e1; e2; e3) { body }' is almost equivalent to

{
e1;
while (e2) {
{ body }
e3;
}
}

(The "almost" covers two cases: If `e2' is missing entirely `while(e2)'
is treated as `while(1)', and if "body" executes a `continue', the
for-loop proceeds to `e3' while the while-loop would not. But if `e2'
is non-empty and there's no `continue', the equivalence holds.)

In other words, a "for" statement does:

1: If `e1' is present, evaluate it. (And bring into existence
any variables it happens to declare These variables live
as long as the loop is executing, and cease to exist when
it terminates for any reason.)

2: If `e2' is present, evaluate it. If it evaluates to zero,
terminate the "for" and skip to whatever follows the body.

3: Execute the body. If the body doesn't end the loop (for
example, with `break' or `return' or some such), then ...

4: ... if `e3' is present, evaluate it.

5: Go back to step [2], with all variables holding whatever
the body and/or `e3' put in them.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      01-02-2011
"Angus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>
> test code:
> char start = 0;
> char end = 0;
> int i, j;
> char s[27] = {0};
>
> i=j=0;
> start = 'A';
> end = 'Z';
>
> for(; s[i] < end; ++i)
> s[i] = start + i;
>
> I was expecting the char array s to be filled with A-Z and then stop.
> ie in the test s[i] < end, when s[i] is Z (decimal 90) then end is
> also Z (90) and so for loop should stop. But the for loop does NOT
> stop.
>
> If I try
> for(; s[i-1] < end; ++i)
> s[i] = start + i;


Those tests look a bit dodgy. If you have to start to think about exactly
how these things work, then perhaps you need a simply way of setting up this
array. For example:

int i,start,end;
char s['Z'-'A'+2]={0};

start='A';
end='Z';

for (i=start; i<=end; ++i) s[i-start]=i;

puts(s);

if you need to use a loop. (Simplest is just char s[]="ABC...Z"

--
Bartc

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-02-2011
On 1/2/2011 7:50 AM, China Blue Ribbon wrote:
> In article<(E-Mail Removed)>,
> Nick Keighley<(E-Mail Removed)> wrote:
>
>> On Jan 2, 10:05 am, China Blue Ribbon<(E-Mail Removed)> wrote:
>>> In article
>>> <(E-Mail Removed)>,
>>>
>>> Angus<(E-Mail Removed)> wrote:
>>>
>>> Why not just code what you meant instead of writing for Obfusticated C?
>>>
>>> int c, i; for (c='A', i=0; c<'Z'; c++, i++) s[i] = c;

>>
>> this assumes ASCII or someother code where the letters are contiguous
>> and in order

>
> The original post had consecutive letters.


The original post had consecutive *character codes*. They started
with the letter 'A', but beyond that the code-character correspondence
was uncertain.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      01-02-2011


"Eric Sosman" <(E-Mail Removed)> wrote in message
news:ifq1c5$80l$(E-Mail Removed)-september.org...
> On 1/2/2011 7:50 AM, China Blue Ribbon wrote:
>> In
>> article<(E-Mail Removed)>,
>> Nick Keighley<(E-Mail Removed)> wrote:
>>
>>> On Jan 2, 10:05 am, China Blue Ribbon<(E-Mail Removed)> wrote:
>>>> In article
>>>> <(E-Mail Removed)>,
>>>>
>>>> Angus<(E-Mail Removed)> wrote:
>>>>
>>>> Why not just code what you meant instead of writing for Obfusticated C?
>>>>
>>>> int c, i; for (c='A', i=0; c<'Z'; c++, i++) s[i] = c;
>>>
>>> this assumes ASCII or someother code where the letters are contiguous
>>> and in order

>>
>> The original post had consecutive letters.

>
> The original post had consecutive *character codes*. They started
> with the letter 'A', but beyond that the code-character correspondence
> was uncertain.


If you had to place a bet, with the same odds both ways, as to whether the
letters were consecutive or not, which would it be? How much money would you
put on it?

(And ignore the fact the OP mentioned that 'Z' had code 90...)

--
Bartc

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-02-2011
Nick Keighley <(E-Mail Removed)> writes:

> On Jan 2, 9:47Â*am, Angus <(E-Mail Removed)> wrote:
>
>> My understanding is that the conditional expression in a for loop is
>> evaluated BEFORE each iteration.

>
> yes
>
>> Â*I understood this to mean that the
>> evaluation of a counter used would be BEFORE the counter is
>> incremented (or decremented or however changed).

>
> no
>
> this:-
> for (i = begin(); i = inc(i); is_end(i))
> proc();


I think you intended to write:

for (i = begin(); is_end(i); i = inc(i))
proc();

> is roughly equivalent to:-
> i = begin();
> again:
> if (is_end(i))


You need !is_end(i) here. Maybe you tripped yourself up by using the
wrong name: is_end is not a good name for a function that returns true
when the loop is to continue!

> goto exit;
> proc();
> i = inc(i);
> goto again;
> exit:


<snip>
--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
python list handling and Lisp list handling Mark Tarver Python 22 04-26-2009 09:36 PM
Is faster handling hexadecimal values than handling chars? Iñaki Baz Castillo Ruby 1 04-15-2008 09:04 AM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Conditional attribute question Nicole Schenk ASP .Net 5 02-02-2005 08:09 AM



Advertisments