Page 6 of 12

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

Posted: Wed Feb 21, 2018 9:34 am
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:

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

Posted: Wed Feb 21, 2018 11:08 am
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;

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

Posted: Thu Feb 22, 2018 3:21 am
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.

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

Posted: Thu Feb 22, 2018 1:51 pm
by syzygy

Code: Select all

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

done&#58;
    ...

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

Posted: Thu Feb 22, 2018 6:54 pm
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;

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

Posted: Thu Feb 22, 2018 6:58 pm
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;

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

Posted: Thu Feb 22, 2018 7:16 pm
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

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

Posted: Fri Feb 23, 2018 5:12 am
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"...

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

Posted: Fri Feb 23, 2018 5:22 am
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...

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

Posted: Mon Mar 05, 2018 1:03 am
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?