Skip to main content
deleted 182 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

The 5 versions are not equivalent: not all terminate when stdin is closed.

Instead of comparing the versions to one another other, we can pick on the bad practices in each.

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {

The 5 versions are not equivalent: not all terminate when stdin is closed.

Instead of comparing the versions to one another other, we can pick on the bad practices in each.

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {
while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {

The 5 versions are not equivalent: not all don't terminate when stdin is closed.

Instead of comparing the versions to one another other, we can pick on the bad practices in each.

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {

The 5 versions are not equivalent: not all don't terminate when stdin is closed.

Instead of comparing the versions to one another other, we can pick on the bad practices in each.

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {

The 5 versions are not equivalent: not all terminate when stdin is closed.

Instead of comparing the versions to one another other, we can pick on the bad practices in each.

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {
deleted 56 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

The 5 versions are not equivalent. For example, most but: not all don't terminate when stdin is closed, and most but not all skip \n.

Instead of comparing the versions to one another other, we can pick on the bad practices in each.

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {

The 5 versions are not equivalent. For example, most but not all don't terminate when stdin is closed, and most but not all skip \n.

Instead of comparing the versions to one another other, we can pick on the bad practices in each.

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {

The 5 versions are not equivalent: not all don't terminate when stdin is closed.

Instead of comparing the versions to one another other, we can pick on the bad practices in each.

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }

It's better to consistently add braces around the body of statements like while and if. A common mistake is to write something like this:

while (1)
    while(scanf("%c", &d), d != '\n') {
        printf("%d\n", d * d);
    }
    printf("...");

This may mislead a reader to think that the last printf is part of the outer loop, when it isn't. Just put the braces around the body always.

Another negative point in this version is having two statements inside the inner while. This is not easy to read. It's better to write in a way that there is no more than one statement per line. More generally, I recommend to avoid the , operator entirely.

The same point applies to this line in the 3rd version:

while((d = getchar()) != '\n') {

That is, there is an assignment within the conditional statement. Avoid such "clever" writing style. Stick to one statement per line.

And one more variation of the same issue, in the 4th version:

while ((d = getchar())) {
deleted 77 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391
Loading
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391
Loading