Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

for conditional handling question

 
 
Eric Sosman
Guest
Posts: n/a
 
      01-02-2011
On 1/2/2011 9:23 AM, BartC wrote:
>
>
> "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?


I'll bet all the money I can scrape together *against* consecutive
codes for letters. With my winnings, I'll travel the world and visit
all the great opera houses, hearing Aïda, Götterdämmerung, Bor*s
Godunóv (Борисъ Годуновъ), Thaïs, Die Entführung aus dem Serail ...

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


In light of the O.P.'s code, do you consider him authoritative
on matters computational? (He may become so someday, but at present
his grasp of computer programming appears to me a bit infirm.)

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-02-2011
On Jan 2, 2:40*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> 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;


am I having a bad day? I intended the program to jump to the exit if
is_end() was true... I choose the names to look a bit like STL
iterators


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

>
> <snip>
> --
> Ben.- Hide quoted text -
>
> - Show quoted text -


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

> On Jan 2, 2:40*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
>> Nick Keighley <(E-Mail Removed)> writes:

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

>
> am I having a bad day? I intended the program to jump to the exit if
> is_end() was true... I choose the names to look a bit like STL
> iterators


And so it does, but what, then, is the equivalent 'for' loop? What you
originally posted:

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

can't be right and it looked like a simple typo so I suggested a
correction, but that correction might not have been what you intended.

If you do indeed want the loop to exit when is_end is true
then you should have stared off with:

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

but that looks like an odd choice when explaining a 'for' loop. I would
not illustrate the general meaning of a loop by picking one with a
negated condition (though of course it makes no logical difference to
experienced programmers).

<snip>
--
Ben.
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      01-02-2011
On 2011-01-02, Angus <(E-Mail Removed)> wrote:
> 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).


You are confused.

for (i = 0; i < 3; ++i) {
printf("%d\n", i);
}

will print:
0
1
2

The key to understanding this is that "++i" is the *END* of an iteration.
Thus, "before" each iteration means "before the body of the loop is
executed". However, the increment will then occur before the *next*
test.

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


This won't work, because s[i] is a different character when you test
than it was when you assigned to it.

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


Yes, this is.

Think it through! If we take your reading, then each iteration of the loop
body would occur with a different value than the condition tested for. Thus,
the standard idiom for looping over a linked list wouldn't work:

for (p = head; p != NULL; p = p->next)

This would, under your model, verify that the last member of the list
wasn't a null pointer, then set p to the null pointer at the end of the
list and execute the loop body with that untested value.

The third clause of a for loop happens after the loop body but before
the *next* test.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Angus
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;
>
> or just
>
> * * memcpy(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 26);
>
> --
> Damn the living - It's a lovely life. * * * * * I'm whoever you want me to be.
> Silver silverware - Where is the love? * * * At least I can stay in character.
> Oval swimming pool - Where is the love? * *Annoying Usenet one post at a time.
> Damn the living - It's a lovely life. * * * * You like me! You really like me!



Yes but this is not the real program. Caller will specify a start and
end character.

 
Reply With Quote
 
Angus
Guest
Posts: n/a
 
      01-02-2011
On Jan 2, 10:55*am, Willem <(E-Mail Removed)> wrote:
> 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


Thanks that explains the behaviour well. I will re-write the loop.
 
Reply With Quote
 
sandeep
Guest
Posts: n/a
 
      01-02-2011
Eric Sosman writes:
> 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.


I believe it is more correct to say that e1 and e2 are evaluated whether-
or-not they are empty. It just happens that evaluating an empty
expression has no effect and yields a TRUE value.
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      01-02-2011
sandeep wrote:
) Eric Sosman writes:
)> 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.
)
) I believe it is more correct to say that e1 and e2 are evaluated whether-
) or-not they are empty. It just happens that evaluating an empty
) expression has no effect and yields a TRUE value.

You believe wrongly. As an example:

if () { /* something */ }

The ability to not have expressions is quite specific to for-loops.


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
 
Seebs
Guest
Posts: n/a
 
      01-03-2011
On 2011-01-02, Willem <(E-Mail Removed)> wrote:
> sandeep wrote:
> ) I believe


> You believe wrongly.


I am beginning to detect a pattern.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-03-2011
On Jan 2, 4:17*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> Nick Keighley <(E-Mail Removed)> writes:
> > On Jan 2, 2:40*pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> >> Nick Keighley <(E-Mail Removed)> writes:

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

>
> > am I having a bad day? I intended the program to jump to the exit if
> > is_end() was true... I choose the names to look a bit like STL
> > iterators

>
> And so it does, but what, then, is the equivalent 'for' loop? *What you
> originally posted:
>
> * * for (i = begin(); i = inc(i); is_end(i))
>
> can't be right and it looked like a simple typo so I suggested a
> correction, but that correction might not have been what you intended.


I had the for loop wrong in the first place

> If you do indeed want the loop to exit when is_end is true
> then you should have stared off with:
>
> * * for (i = begin(); !is_end(i); i = inc(i))


*that* is what I meant!

> but that looks like an odd choice when explaining a 'for' loop. *I would
> not illustrate the general meaning of a loop by picking one with a
> negated condition (though of course it makes no logical difference to
> experienced programmers).


I perhaps over generalised. Since begin and end were functions I
decided the inc should be as well.


 
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