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

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

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

Post by hgm »

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:
Fulvio
Posts: 395
Joined: Fri Aug 12, 2016 8:43 pm

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

Post by Fulvio »

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;
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 &#123;
  int success = read_some_input&#40;);
  if&#40;success&#41; &#123;
    process_input&#40;);
  &#125;
&#125; while&#40;success&#41;;
'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: 5557
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&#58;
    ...
    if &#40;finished&#41;
        goto done;
    ...
    goto one_more_time;

done&#58;
    ...
Ras
Posts: 2487
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&#40;EVER&#41;
&#123;
   do_stuff&#40;);
&#125;
Ras
Posts: 2487
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 &#40;PIGS_DO_NOT_FLY&#41;
&#123;
    ...
&#125;
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&#40;; EVER&EVER;)
        &#123;
            int You=1;
            //That shines in me
            //like the morning sun
        &#125;
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&#58;
    ...
    if &#40;finished&#41;
        goto done;
    ...
    goto one_more_time;

done&#58;
    ...
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&#40;EVER&#41;
&#123;
   do_stuff&#40;);
&#125;
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&#40;) &#123;
	s32 i, fs120, ts120, fs64, ts64;
	s32 dir&#91;8&#93; = &#123;9, 11, -9, -11, 1, 10, -1, -10&#125;;
	s32 din&#91;8&#93; = &#123;8, 12, 19, 21, -8, -12, -19, -21&#125;;
	s32 isOnBoard&#91;120&#93; = &#123;
	    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&#125;;

	for &#40;fs64 = 63; fs64 >= 0; fs64--) &#123;
		fs120 = SQUARE120&#40;fs64&#41;;

		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