I'm not very happy with the do {} while() statement in C

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: I'm not very happy with the do {} while() statement in C

Post by bob »

hgm wrote:If you don't like 'break', it is easily avoidable at the price of some extra indentation:

Code: Select all

do {
  int success = read_some_input();
  if(success) {
    process_input();
  }
} while(success);
'while(FOREVER)' sounds linguistically broken. I would prefer 'for(EVER)' or 'while(UNBROKEN)'. :lol:
I tried to come up with a better term when I started using that. The runner-up for me was while (ALWAYS) {} The most succinct is while (1) {}

I have even seen some use a #define loop "while (1)" so that you end up with loop {};

Ditto for "repeat".

Really is not a perfect syntax in C to express an infinite loop with an internal exit, unfortunately.
syzygy
Posts: 5566
Joined: Tue Feb 28, 2012 11:56 pm

Re: I'm not very happy with the do {} while() statement in C

Post by syzygy »

Code: Select all

one_more_time:
    ...
    if (finished)
        goto done;
    ...
    goto one_more_time;

done:
    ...
Ras
Posts: 2488
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: I'm not very happy with the do {} while() statement in C

Post by Ras »

hgm wrote:I would prefer 'for(EVER)' or 'while(UNBROKEN)'. :lol:
Here you go:

Code: Select all

#define EVER ;;
...
for(EVER)
{
   do_stuff();
}
Ras
Posts: 2488
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: I'm not very happy with the do {} while() statement in C

Post by Ras »

bob wrote:The most succinct is while (1) {}
Real code sample:

Code: Select all

#define PIGS_DO_NOT_FLY    1u
...
while (PIGS_DO_NOT_FLY)
{
    ...
}
User avatar
Kotlov
Posts: 266
Joined: Fri Jul 10, 2015 9:23 pm
Location: Russia

Re: I'm not very happy with the do {} while() statement in C

Post by Kotlov »

Code: Select all

#define EVER 1

    EVER&EVER;
    for(; EVER&EVER;)
        {
            int You=1;
            //That shines in me
            //like the morning sun
        }
Image
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: I'm not very happy with the do {} while() statement in C

Post by bob »

syzygy wrote:

Code: Select all

one_more_time:
    ...
    if (finished)
        goto done;
    ...
    goto one_more_time;

done:
    ...
I've used that millions of times in years gone by. Particularly in asm type code. But it is not so intuitive when spread over a page or more. It doesn't jump out and say "this is a loop"...
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: I'm not very happy with the do {} while() statement in C

Post by bob »

Ras wrote:
hgm wrote:I would prefer 'for(EVER)' or 'while(UNBROKEN)'. :lol:
Here you go:

Code: Select all

#define EVER ;;
...
for(EVER)
{
   do_stuff();
}
That is not so bad. I am beginning to think that my original approach while(1) {} might have been about the best that can be done, although it does require knowledge of C to realize that is an infinite while. Think I am going to revert to the while (1) and let it go...
Michael Sherwin
Posts: 3196
Joined: Fri May 26, 2006 3:00 am
Location: WY, USA
Full name: Michael Sherwin

Re: I'm not very happy with the do {} while() statement in C

Post by Michael Sherwin »

Fulvio wrote:https://stackoverflow.com/questions/160 ... -zero-in-c

Code: Select all

void Initialize() {
	s32 i, fs120, ts120, fs64, ts64;
	s32 dir[8] = {9, 11, -9, -11, 1, 10, -1, -10};
	s32 din[8] = {8, 12, 19, 21, -8, -12, -19, -21};
	s32 isOnBoard[120] = {
	    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
	    1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1,
	    1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1,
	    1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1,
	    1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

	for (fs64 = 63; fs64 >= 0; fs64--) {
		fs120 = SQUARE120(fs64);

		for &#40;i = 0; i < 8; i++) &#123;
			ts120 = fs120 + din&#91;i&#93;;
			if &#40;isOnBoard&#91;ts120&#93;) &#123;
				dirKn&#91;fs64&#93; |= BIT64&#40;SQUARE64&#40;ts120&#41;);
			&#125;
		&#125;

		for &#40;i = 0; i < 8; i++) &#123;
			ts120 = fs120 + dir&#91;i&#93;;
			if &#40;isOnBoard&#91;ts120&#93;) &#123;
				dirKi&#91;fs64&#93; |= BIT64&#40;SQUARE64&#40;ts120&#41;);
				do &#123;
					dirPtr&#91;i&#93;&#91;fs64&#93; |= BIT64&#40;SQUARE64&#40;ts120&#41;);
					ts120 += dir&#91;i&#93;;
				&#125; while &#40;isOnBoard&#91;ts120&#93;);
			&#125;
		&#125;
	&#125;
&#125;
I did not reply earlier because I was not well enough to work on my code. Today I commented out the statements that set the globals to zero and it is as you said. However, I have a question. In my C primer which I admit was written more than 20 years ago it says that global variables should be initialized to zero or otherwise they may be undefined in some implementations of C. Is that so archaic that I should no longer take that into consideration or leave it in because it doesn't hurt. Or maybe leave them in but commented out just in case?
If you are on a sidewalk and the covid goes beep beep
Just step aside or you might have a bit of heat
Covid covid runs through the town all day
Can the people ever change their ways
Sherwin the covid's after you
Sherwin if it catches you you're through
lauriet
Posts: 199
Joined: Sun Nov 03, 2013 9:32 am

Re: I'm not very happy with the do {} while() statement in C

Post by lauriet »

The "Pascal way" is to use boolean variables. I have never felt the need to use goto or break:

Done := False;
while not done
{
do stuff;
do stuff;
Done := Some test;
if not done then
{
do stuff;
do stuff;
}
}
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: I'm not very happy with the do {} while() statement in C

Post by AlvaroBegue »

Michael Sherwin wrote: Today I commented out the statements that set the globals to zero and it is as you said. However, I have a question. In my C primer which I admit was written more than 20 years ago it says that global variables should be initialized to zero or otherwise they may be undefined in some implementations of C. Is that so archaic that I should no longer take that into consideration or leave it in because it doesn't hurt. Or maybe leave them in but commented out just in case?
You should not have global variables. That's the best way to not have to find out what the initialization rules are. :)

Also, C now (since C99) lets you declare variables anywhere, not just at the beginning of the function. You should get used to declaring the variable when it's first used. The resulting code is much easier to read.