What is the scope of this while loop?

J

JS

#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end?? For some reason all the examples I
have read with C code there is no brackets to confine the while loop (like
in Java).

JS
 
M

Mike Deskevich

here's the bracketed version. i'm pretty sure i got it right, but you
better double check the output of both versions to make sure they
match.



#include <stdio.h>

main()
{
int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
{
ndigit = 0;
}

while ((c = getchar()) != EOF)
{
if (c >= '0' && c <= '9')
{
++ndigit[c-'0'];
}
else
{
if (c == ' ' || c == '\n' || c == '\t')
{
++nwhite;
}
else
{
++nother;
}
}
}

printf("digits =");

for (i = 0; i < 10; ++i)
{
printf(" %d", ndigit);
}

printf(", white space = %d, other = %d\n",
nwhite, nother);

}
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end??


In C, a while() statement consists of three parts:
1) the 'while' keyword,
2) a condition, enclosed in parenthesis, and
3) a single statement to be executed if the condition tests true

In C, a series of statements, enclosed in brace brackets, is considered to be a
single statement.

Thus:
while (1) printf("Continious loop\n");
and
while (1) { printf("Continious loop\n");
are equivalent.

So, to answer your question, the while() statement in your example code extends
from the while() to the ++nother;

Or in other words, the following is a whole while() statement.

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
For some reason all the examples I
have read with C code there is no brackets to confine the while loop (like
in Java).

The implication is that Java requires that the body of a while loop be enclosed
in brace brackets. So?? What's Java got to do with it? Different languages have
different rules.

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCNen5agVFX4UWr64RAtYqAJ9n1twwQeQ6KCGK39wIEOlIUV9I1wCfbmeI
Nkltw9EMstpO1koUwr6u778=
=O1jF
-----END PGP SIGNATURE-----
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lew Pitcher wrote:
[snip]
In C, a while() statement consists of three parts:
1) the 'while' keyword,
2) a condition, enclosed in parenthesis, and
3) a single statement to be executed if the condition tests true

In C, a series of statements, enclosed in brace brackets, is considered to be a
single statement.

Thus:
while (1) printf("Continious loop\n");
and
while (1) { printf("Continious loop\n");

Oops. Correction
while (1) { printf("Continious loop\n"); }

are equivalent.
[snip]

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCNepGagVFX4UWr64RAiXPAKCMfxxydswF1zsaBaOf+0bPXLFezgCfYUNg
+b2aWTcHplyJvfaB9cu37sg=
=v6mw
-----END PGP SIGNATURE-----
 
V

Vig

--
--
Vig
JS said:
#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end?? For some reason all the examples I
have read with C code there is no brackets to confine the while loop (like
in Java).


I believe the while loop extends till the line just prior to the first
printf statement. This is because only the line immediately after a while
loop is in its scope. However, the if statemnt's scope extends back to the
while loop.
 
K

Keith Thompson

JS said:
#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end?? For some reason all the examples I
have read with C code there is no brackets to confine the while loop (like
in Java).


A while() always controls a single statement. Typically that single
statement is a compound statement, which is delimited by '{' and '}',
but it can be any statement.

In this case, the single statement is the if-else statement consisting
of the 7 lines following the while().

Note that the indentation on the last two of those lines is incorrect;
the "else" should be directly under the "if", and the "++nother;"
should be directly under the "++nwhite;". (This isn't incorrect in
the sense that a compiler is going to complain about it, but it's
misleading.)

Personally, I always use braces around sub-statements like this (a
habit I picked up from Perl, which requires them). Mike Deskevich's
fully braced version of your program appears to be correct.
 
C

CBFalconer

JS said:
#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end?? For some reason all the
examples I have read with C code there is no brackets to confine
the while loop (like in Java).


It ends after one statement, which may be a compound statement,
effected via {}. Better indentation and formatting will show
things up clearly, as in:

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9') ++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;
else ++nother;

I don't believe in extra braces when the complete condition/action
can be specified on one line of reasonable length.
 
P

Peter Nilsson

CBFalconer said:
... Better indentation and formatting will show
things up clearly, as in:
.
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9') ++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;
else ++nother;
.
I don't believe in extra braces when the complete condition/action
can be specified on one line of reasonable length.

Does that mean you're writing...

while (blah) continue;

....instead of...

while (blah)
{
continue;
}

....these days! ;)
 
P

pete

Peter said:
... Better indentation and formatting will show
things up clearly, as in:
.
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9') ++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;
else ++nother;
.
I don't believe in extra braces when the complete condition/action
can be specified on one line of reasonable length.

Does that mean you're writing...

while (blah) continue;

...instead of...

while (blah)
{
continue;
}

...these days! ;)

I prefer compound statements with all ifs and loops.
It makes reading simpler.
 
C

CBFalconer

Peter said:
CBFalconer said:
... Better indentation and formatting will show
things up clearly, as in:
.
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9') ++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;
else ++nother;
.
I don't believe in extra braces when the complete condition/action
can be specified on one line of reasonable length.

Does that mean you're writing...

while (blah) continue;

...instead of...

while (blah)
{
continue;
}

...these days! ;)

Definitely. Wouldn't have it any other way :)

int flushln(FILE *f)
{
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top