Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   for conditional handling question (http://www.velocityreviews.com/forums/t741272-for-conditional-handling-question.html)

Angus 01-02-2011 09:47 AM

for conditional handling question
 
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

jacob navia 01-02-2011 10:09 AM

Re: for conditional handling question
 
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.

Willem 01-02-2011 10:55 AM

Re: for conditional handling question
 
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

Nick Keighley 01-02-2011 12:20 PM

Re: for conditional handling question
 
On Jan 2, 9:47*am, Angus <anguscom...@gmail.com> 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



Nick Keighley 01-02-2011 12:22 PM

Re: for conditional handling question
 
On Jan 2, 10:05*am, China Blue Ribbon <chine.b...@yahoo.com> wrote:
> In article <7a71f895-72e6-498b-8eb8-5a6f4a5eb...@v17g2000yqv.googlegroups..com>,
>
> *Angus <anguscom...@gmail.com> 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);


Eric Sosman 01-02-2011 01:49 PM

Re: for conditional handling question
 
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
esosman@ieee-dot-org.invalid

BartC 01-02-2011 01:51 PM

Re: for conditional handling question
 
"Angus" <anguscomber@gmail.com> wrote in message
news:7a71f895-72e6-498b-8eb8-5a6f4a5eb8ab@v17g2000yqv.googlegroups.com...

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


Eric Sosman 01-02-2011 02:13 PM

Re: for conditional handling question
 
On 1/2/2011 7:50 AM, China Blue Ribbon wrote:
> In article<995a71f8-af73-4780-be5e-0f80e4f1854d@n10g2000yqd.googlegroups.com>,
> Nick Keighley<nick_keighley_nospam@hotmail.com> wrote:
>
>> On Jan 2, 10:05 am, China Blue Ribbon<chine.b...@yahoo.com> wrote:
>>> In article
>>> <7a71f895-72e6-498b-8eb8-5a6f4a5eb...@v17g2000yqv.googlegroups.com>,
>>>
>>> Angus<anguscom...@gmail.com> 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
esosman@ieee-dot-org.invalid

BartC 01-02-2011 02:23 PM

Re: for conditional handling question
 


"Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
news:ifq1c5$80l$1@news.eternal-september.org...
> On 1/2/2011 7:50 AM, China Blue Ribbon wrote:
>> In
>> article<995a71f8-af73-4780-be5e-0f80e4f1854d@n10g2000yqd.googlegroups.com>,
>> Nick Keighley<nick_keighley_nospam@hotmail.com> wrote:
>>
>>> On Jan 2, 10:05 am, China Blue Ribbon<chine.b...@yahoo.com> wrote:
>>>> In article
>>>> <7a71f895-72e6-498b-8eb8-5a6f4a5eb...@v17g2000yqv.googlegroups.com>,
>>>>
>>>> Angus<anguscom...@gmail.com> 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


Ben Bacarisse 01-02-2011 02:40 PM

Re: for conditional handling question
 
Nick Keighley <nick_keighley_nospam@hotmail.com> writes:

> On Jan 2, 9:47Â*am, Angus <anguscom...@gmail.com> 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.


All times are GMT. The time now is 04:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57