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

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Post Reply
User avatar
hgm
Posts: 22210
Joined: Fri Mar 10, 2006 9:06 am
Location: Amsterdam
Contact:

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

Post by hgm » Wed Feb 21, 2018 8:34 am

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: 91
Joined: Fri Aug 12, 2016 6:43 pm

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

Post by Fulvio » Wed Feb 21, 2018 10:08 am

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: 20340
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

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

Post by bob » Thu Feb 22, 2018 2:21 am

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: 4221
Joined: Tue Feb 28, 2012 10:56 pm

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

Post by syzygy » Thu Feb 22, 2018 12:51 pm

Code: Select all

one_more_time&#58;
    ...
    if &#40;finished&#41;
        goto done;
    ...
    goto one_more_time;

done&#58;
    ...

Ras
Posts: 935
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

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

Post by Ras » Thu Feb 22, 2018 5:54 pm

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: 935
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

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

Post by Ras » Thu Feb 22, 2018 5:58 pm

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: 173
Joined: Fri Jul 10, 2015 7:23 pm
Location: Russia

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

Post by Kotlov » Thu Feb 22, 2018 6:16 pm

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: 20340
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

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

Post by bob » Fri Feb 23, 2018 4:12 am

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: 20340
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

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

Post by bob » Fri Feb 23, 2018 4:22 am

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: 2799
Joined: Fri May 26, 2006 1:00 am
Location: OH, USA

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

Post by Michael Sherwin » Mon Mar 05, 2018 12:03 am

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?
Regards,
Mike

Post Reply