Read something you would never write

Yesterday I remembered the following C code structure:

#include <stdio.h>
main()
{
//initializations
while(1)
{
printf("Player %d, give number from 1 to 6\n",f);
scanf("%c",&n);
if (n - '0' > 0 && n-'0' < 7)
{
x = n - '0';
break;
}
}
printf("The number tuned to be : %d\n",x);
}

I would prefer the following implementation:

#include <stdio.h>
main()
{
//initializations
printf("Player %d, give number from 1 to 6\n",f);
scanf("%c",&n);
while(n - '0' < 1 || n-'0' > 6)
{
printf("Player %d, give number from 1 to 6\n",f);
scanf("%c",&n);
}
x = n - '0';
printf("The number tuned to be : %d\n",x);
}

I mean why write while(1)? Anyway since the whole logic of the program in hand had this logic I followed it even if I would never have written it that way.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s